跟着小白一起学鸿蒙—如何编译Hap程序(十七)

系统 OpenHarmony
本篇我们来学习如何在鸿蒙环境下编译Hap程序。

想了解更多关于开源的内容,请访问:

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

安装使用说明(Open-Harmony)

在开源鸿蒙系统下

  • 蓝牙专项应用程序路径为:foundation/communication/bluetooth/test/example/BluetoothTest
  • 目录结构
.
├── example
├── BluetoothTest
├── build-profile.json5
├── hvigorfile.js
├── package.json
├── package-lock.json
├── BUILD.gn
├── entry/src/main
├── config.json
├── ets
├── Component
├── MainAbility
├── app.ets
├── controller
├── model
├── pages
└── res
└── image
├── MainAbility2
└── Utils
└── resources
└── base
├── element
└── media
└── signature
├── fuzztest
├── moduletest
└── unittest
  • 在开源鸿蒙下的编译,是套用了原本系统中编写好的hap程序编译模板,因此保留了一些模板中的结构,如:MainAbility2 和config.json中的js语句;对程序本身的编译没有影响。
  • 在bundle.json中添加编译命令。
路径为:foundation/communication/bluetooth/bundle.json
命令为:
"test": [
...
"//foundation/communication/bluetooth/test/example/example_btTest:BluetoothTest"
]
  • 添加BUILD.gn
foundation/communication/bluetooth/test/example/BluetoothTest/BUILD.gn
import("//test/xts/tools/build/suite.gni")
ohos_hap("BluetoothTest") {
hap_profile = "entry/src/main/config.json"
hap_name = "BluetoothTest"
subsystem_name = XTS_SUITENAME
final_hap_path =
"${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/testcases/${hap_name}.hap"
deps = [
":bluetooth_resources",
":bluetooth_ts_assets",
]
certificate_profile = "signature/auto_ohos_default_com.ohos.bttest.p7b"
}

ohos_js_assets("bluetooth_ts_assets") {
source_dir = "entry/src/main/ets"
hap_profile = "entry/src/main/config.json"
ets2abc = true
}

ohos_resources("bluetooth_resources") {
sources = [ "entry/src/main/resources" ]
hap_profile = "entry/src/main/config.json"
}
  • 创建signature文件夹,添加签名文件。

路径为:foundation/communication/bluetooth/test/example/example_btTest/signature
签名文件为:Dev-Eco自动签名生成的文件:auto_ohos_default_com.ohos.bttest.p7b

  • 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
}
}
]
}
}
  • 编译命令

# 全量编译
./build.sh --product-name {product_name}

# 单独编译HAP
./build.sh --product-name {product_name} --build-target BluetoothTest

  • 生成文件
  • 使用 find out -name “BluetoothTest.hap*” 查找生成文件,或者直接查看config.json所写的生成路径。
  • 将生成文件拷到本地电脑上,连接板子,使用命令hdc_std.exe install BluetoothTest进行安装。
  • 使用命令hdc_std uninstall {安装包名} 进行卸载。
  • 安装包名在entry\src\main\config.json 如:"bundleName": "com.ohos.bttest"
  • 补充
    在鸿蒙系统下编译,仍存在高版本对低版本的编译不兼容性问题。即在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’。

想了解更多关于开源的内容,请访问:

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。

责任编辑:jianghua 来源: 51CTO开源基础软件社区
相关推荐

2022-12-06 15:39:16

鸿蒙主干代码

2022-12-09 15:34:38

2022-11-29 16:35:02

Tetris鸿蒙

2022-12-02 14:20:09

Tetris鸿蒙

2023-03-30 09:32:27

2022-11-14 17:01:34

游戏开发画布功能

2022-11-25 16:48:54

鸿蒙Stage HAP

2023-02-27 16:30:32

鸿蒙开源协议分析

2023-03-30 09:19:54

SELinux安全子系统

2022-08-19 19:02:20

开源鸿蒙操作系统

2023-04-04 09:24:11

鸿蒙HiDumper

2022-10-10 14:47:04

蓝牙应用鸿蒙

2022-09-30 14:08:26

Hap应用鸿蒙

2023-01-03 15:09:10

鸿蒙常用工具

2022-08-29 17:25:19

鸿蒙应用开发

2022-10-20 16:40:16

JS应用控制LED鸿蒙

2022-10-09 15:05:50

NAPI框架鸿蒙

2023-03-15 16:19:03

BinderIPC工具

2022-10-31 15:35:02

Wi-Fi蓝牙子系统

2022-11-03 15:47:04

HTTP通信协议
点赞
收藏

51CTO技术栈公众号