1. 鸿蒙的共享包
鸿蒙的 HAR(Harmony Archive)称为静态共享包,也就是一般开发中的静态依赖库,例如 iOS 开发中的 .a 或 framework 文件,可以包含 ArkTS 代码、C++ 库、资源和配置文件。通过 HAR 可以实现多个模块或多个工程共享 ArkUI 组件、资源等相关代码。
与之相对应的就是动态链接库,鸿蒙系统中称为 HSP(Harmony Shared Package)动态共享包。
下面的构建方法探索以 HAR 为主。
鸿蒙的项目是以 Module(组件模块)为基础单元去构建的,Module 分为 Ability 和 Library 两种类型,Ability 类型的 Module 对应于编译后的 HAP(Harmony Ability Package),Library 类型的Module对应于HAR(Harmony Archive)和 HSP(Harmony Shared Package)。
HAR 或者 HSP 不同于 HAP,不能独立安装运行在设备上,只能作为应用模块的依赖项被引用。
2. 构建 HAR
2.1. 新建 Library 模块
可以直接在现有的项目中构建 HAR 模块,也可以在新建的独立空项目中取添加构建 HAR 模块:
模块类型选择 Static Library:
使用默认配置,项目中会出现一个 library 模块文件夹:
这里忽略逻辑开发相关的部分,具体的 ArkTS 的组件开发和 C++ 相关库的封装,参考官方教程。
这里以一个简单的 ArkUI 自定义组件为例:
@Component
export struct IconImageButton {
image: string | PixelMap | Resource = ''
@Prop iconWidth: number = 24
@Prop buttonWidth: number = 48
@Prop buttonHeight: number = 48
clickAction: () => void = () => {}
build() {
Button() {
Image(this.image)
.width(this.iconWidth)
}
.width(this.buttonWidth)
.height(this.buttonHeight)
.type(ButtonType.Normal)
.backgroundColor(Color.Transparent)
.onClick(this.clickAction)
}
}
相关代码开发完成后,统一在 Index.ets 文件中做导出对外暴露使用:
export { IconImageButton } from './src/main/ets/components/mainpage/IconImageButton'
2.2. Library 重命名
如果想要修改打出的包名,可以做如下修改:
- 修改 module.json5 中的 name 配置(注意小写)
{
"module": {
"name": "myuilibrary",
"type": "har",
"deviceTypes": [
"default",
"tablet"
]
}
}
- 修改项目层的 build-profile.json5 中的配置
"modules": [
{
"name": "entry",
"srcPath": "./entry",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
},
{
"name": "myuilibrary",
"srcPath": "./library"
}
]
2.3. Library 构建
完善 Library 模块的构建配置文件 oh-package.json5:
{
// name cannot contain capital letters
"name": "myuilibrary",
// 后续更新维护的版本号
"version": "1.0.0",
"description": "Please describe the basic information.",
// Index.ets文件是HAR导出声明文件的入口,HAR需要导出的接口,统一在Index.ets文件中导出
"main": "Index.ets",
// 作者字段必填
"author": "LW",
"license": "Apache-2.0",
"dependencies": {}
}
选中 Library 文件夹,执行构建:
生成的 HAR 在如下目录:
2.4. HAR 发布准备
在库模块中(与src文件夹同一级目录下),添加如下文件:
- 新建 README.md 文件:在 README.md 文件中必须包含包的介绍和引用方式(否则审核会被拒) ,还可以根据包的内容添加更详细介绍。
# MyUILibrary
## 简介
自用的 UI 封装组件库,后续持续更新维护
## 下载安装
```
ohpm install @ohos/myuilibrary
```
- 新建 CHANGELOG.md 文件:填写 HAR 的版本更新记录。
- 添加 LICENSE 文件:LICENSE 许可文件。
然后重新执行构建。
2.5. 公钥、私钥配置
- 利用 ssh-keygen 工具生成本地的公、私钥:
ssh-keygen -m PEM -t RSA -b 4096 -f /Users/xxx/.ssh/id_rsa_ohpm
注意制作过程中,必须设置密码 passphrase ,不可忽略,否则后续发布时,会报如下错误:
ohpm ERROR: Private key without passphrase is not supported.
ohpm ERROR: You must config a encrypted private key using a non-empty passphrase.
ohpm ERROR: Not supported private key.
登录 OpenHarmony三方库中心仓 官网,单击主页右上角的个人中心,进入用户设置,新增 OHPM 公钥:
2.6. 发布 HAR
配置 ohpm 环境变量,启动 ohpm ,设置库发布信息:
# 配置环境
export OHPM_HOME=/Users/xxx/ohpm/ohpm-1.2.5
export PATH=${OHPM_HOME}/bin:${PATH}
# 测试可用性
ohpm -v
1.2.5
# 设置私钥文件路径
ohpm config set key_path /Users/xxx/.ssh/id_rsa_ohpm
# 设置发布码,发布码在中心仓上面截图中的个人信息下面
ohpm config set publish_id xxxxx
执行 publish 命令发布,<HAR路径> 需指定为 .har 文件的具体路径:
ohpm publish <HAR路径>
右键 HAR 文件,复制路径->绝对路径:
发布成功:
ohpm INFO: registry:https://repo.harmonyos.com/ohpm/
......
ohpm INFO:
what is your passphrase of the private key: ***
+myuilibrary 1.0.1
进入中心仓控制台查看,已经进入审核状态:
后续的审核问题,还在逐步踩坑中。
2.7. ohpm 私仓搭建
上述流程发布的都是需要审核的公有仓,私有仓在 HarmonyNext 版本中是支持的,但是工具和文档暂时还未外开放,应该是需要签约才可以使用。
3. ohpm 使用
OHPM CLI 作为鸿蒙生态三方库的包管理工具,支持 OpenHarmony 共享包的发布、安装和依赖管理。对标的就是前端的 npm 工具,也可以说是 npm 的翻版。
在 DevEco Studio 中,ohpm 默认已经安装,在设置-首选项,查看 ohpm 的安装目录:
控制台中配置环境变量:
export OHPM_HOME=/Users/xxx/ohpm/ohpm-1.2.5
export PATH=${OHPM_HOME}/bin:${PATH}
检查可用性:
ohpm -v
更多命令使用以及命令支持的配置项,参阅 ohpm-cli。
3.1. 安装远程依赖库
这里以安装下拉刷新库为例:
ohpm.openharmony.cn/#/cn/detail…
ohpm install @ohos/pulltorefresh
使用:
import { PullToRefresh } from '@ohos/pulltorefresh'
关于 oh-package.json5,项目中有两级,工程级和模块级别,默认安装到了工程级配置文件中:
{
"name": "onlineclass",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
"@ohos/pulltorefresh": "^2.0.5"
},
"devDependencies": {
"@ohos/hypium": "1.0.11"
},
"dynamicDependencies": {}
}
3.2. 安装本地依赖库
这里使用前面制作的 myuilibrary 库,复制 .har 文件路径:
ohpm install /Users/xxx/myuilibrary.har
安装后的 oh-package.json5 文件:
{
"name": "onlineclass",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
"@ohos/pulltorefresh": "^2.0.5",
"myuilibrary": "file:library/build/default/outputs/default/myuilibrary.har"
},
"devDependencies": {
"@ohos/hypium": "1.0.11"
},
"dynamicDependencies": {}
}
使用:
import { IconImageButton } from 'myuilibrary'