作者:王石,胡瑞涛
上节回顾
在[六]如何编写hap文件,我们学习了hap文件的结构框架和如何自己开发一个简单的hap程序。接下来我们来学习如何在OpenHarmony环境下编译hap程序。
安装使用说明(OpenHarmony)
在OpenHarmony系统下
-
蓝牙专项应用程序路径为:foundation/communication/bluetooth/test/example/BluetoothTest
-
目录结构
-
在OpenHarmony下的编译,是套用了原本系统中编写好的hap程序编译模板,因此保留了一些模板中的结构,如:MainAbility2 和config.json中的js语句;对程序本身的编译没有影响。
-
在bundle.json中添加编译命令
-
添加BUILD.gn
-
创建signature文件夹,添加签名文件
-
config.json
{
"app": {
"vendor": "samples",
"bundleName": "ohos.samples.bttest",
"version": {
"code": 1000000,
"name": "1.0.0"
},
"apiVersion": {
"compatible": 8,
"target": 8
}
},
"deviceConfig": {},
"module": {
"mainAbility": ".MainAbility",
"deviceType": [
"default",
"phone"
],
"reqPermissions": [
{
"reason": "",
"name": "ohos.permission.DISCOVER_BLUETOOTH"
},
{
"reason": "",
"name": "ohos.permission.USE_BLUETOOTH"
},
{
"name": "ohos.permission.LOCATION"
}
],
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
],
"orientation": "unspecified",
"visible": true,
"srcPath": "MainAbility",
"name": ".MainAbility",
"srcLanguage": "ets",
"icon": "$media:icon",
"description": "$string:MainAbility_desc",
"formsEnabled": false,
"label": "$string:MainAbility_label",
"type": "page",
"launchType": "singleton"
},
{
"orientation": "unspecified",
"visible": true,
"srcPath": "MainAbility2",
"name": ".MainAbility2",
"srcLanguage": "ets",
"icon": "$media:icon",
"description": "$string:MainAbility2_desc",
"formsEnabled": false,
"label": "$string:MainAbility2_label",
"type": "page",
"launchType": "singleton"
}
],
"distro": {
"moduleType": "entry",
"installationFree": false,
"deliveryWithInstall": true,
"moduleName": "entry"
},
"package": "ohos.samples.bttest",
"srcPath": "",
"name": ".entry",
"js": [
{
"mode": {
"syntax": "ets",
"type": "pageAbility"
},
"pages": [
"pages/homePage",
"pages/manualApiTestPage",
...
],
"name": ".MainAbility",
"window": {
"designWidth": 720,
"autoDesignWidth": false
}
},
{
"mode": {
"syntax": "ets",
"type": "pageAbility"
},
"pages": [
"pages/index"
],
"name": ".MainAbility2",
"window": {
"designWidth": 720,
"autoDesignWidth": false
}
}
]
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
-
编译命令
-
生成文件
- 使用 find out -name “BluetoothTest.hap*” 查找生成文件,或者直接查看config.json所写的生成路径。
- 将生成文件拷到本地电脑上,连接板子,使用命令
hdc_std.exe install BluetoothTest
进行安装。
- 使用命令
hdc_std uninstall {安装包名}
进行卸载。
- 安装包名在
entry\src\main\config.json
如:"bundleName": "com.ohos.bttest"
-
补充
在OpenHarmony系统下编译,仍存在高版本对低版本的编译不兼容性问题。即在mater版本下编译的hap无法在beta2版本运行;反之则可以。
-
可能出现的编译报错
- 一些属性必须初始化一个默认值。如:The @State property ‘bgColor’ ‘settingArrow’ ‘settingSummary’ must be specified a default value。
- 注意引用路径中的文件名大小写问题。
- 一些属性名与关键词或类名重复会起冲突。如:Property ‘height’/“onClick”/“enabled” in type ‘EntryComponent’ is not assignable to the same property in base type 'CustomComponent。将其改成例如"isOnClick"/"isEnabled"即可。
- 一些资源,如:@State settingSummary: Resource 必须是resource 不能加 |string 或者赋值为string。Type ‘Resource’ is not assignable to type ‘string’. 反之同理。Type ‘string’ is not assignable to type ‘Resource’。
很详细的流程
干货,支持
从开发到编译,一点点积累学习