OpenHarmony开发者论坛
标题:
OpenHarmony系统之Service代码一键生成工具介绍(二)
[打印本页]
作者:
深开鸿_苟晶晶
时间:
2024-6-20 11:45
标题:
OpenHarmony系统之Service代码一键生成工具介绍(二)
[md]## 前言
在上一篇博客中我们介绍了napi框架生成工具可执行程序的使用方法以及如何集成到OpenHarmony 4.0 release系统并进行测试;本篇文章将介绍如何将napi框架生成工具生成的代码集成到OpenHarmony 4.1 release。
## 使用说明
如何安装并使用napi框架生成工具,请参考上一篇文章:
[OpenHarmony系统之Napi框架生成工具介绍](
https://www.51cto.com/article/778404.html
)
## 集成
将napi框架生成工具生成的代码集成到OpenHarmony 4.1 release
### 建立模块位置
模块目录理论上可在OpenHarmony工程的任一位置,假设OpenHarmony代码库的目录为OHOS_SRC,在OHOS_SRC/foundation目录下,建测试模块目录:napitest。napitest目录结构如下:
```
napitest
|-- binding.gyp
|-- BUILD.gn
|-- bundle.json
|-- napitest.cpp
|-- napitest.h
|-- napitest_middle.h
|-- napitest_middle.cpp
|-- test.sh
|-- tool_utility.cpp
|-- tool_utility.h
```
其中bundle.json为新增的编译配置文件,其它为工具生成的代码。
### 编译修改点
#### 修改bundle.json文件
其中destPath选项中的"//foundation/napitest"指的是napitest目录,":napitest"指的是上面BUILD.gn中的目标ohos_shared_library("napitest")。
```
{
"name": "@ohos/napitest",
"description": "napitest provides atomic capabilities",
"version": "4.1",
"license": "Apache License 2.0",
"publishAs": "code-segment",
"segment": {
"destPath": "foundation/napitest"
},
"dirs": {},
"scripts": {},
"component": {
"name": "napitest",
"subsystem": "napitest",
"features": [],
"adapted_system_type": [
"standard"
],
"rom": "10000KB",
"ram": "10000KB",
"deps": {
"components": [
"napi",
"ipc_core",
"hilog"
],
"third_party": [
"node"
]
},
"build": {
"sub_component": [
"//foundation/napitest:napitest"
],
"inner_kits": [
{
"header": {
"header_base": "//foundation/napitest",
"header_files": [
"tool_utility.h",
"napitest.h",
"napitest_middle.h"
]
},
"name": "//foundation/napitest:napitest"
}
]
}
}
}
```
#### 修改BUILD.gn文件
删除ohos_shared_library("napitest")中的deps,并新增外部依赖napi和hilog,即 external_deps = [ "napi:ace_napi","hilog:libhilog", ]。修改后的BUILD.gn文件内容如下所示:
```
import("//build/ohos.gni")
ohos_shared_library("napitest")
{
sources = [
"napitest_middle.cpp",
"napitest.cpp",
"tool_utility.cpp",
]
include_dirs = [
".",
"//third_party/node/src",
]
external_deps = [ "napi:ace_napi","hilog:libhilog", ]
remove_configs = [ "//build/config/compiler:no_rtti" ]
cflags=[
]
cflags_cc=[
"-frtti",
]
ldflags = [
]
relative_install_dir = "module"
part_name = "napitest"
subsystem_name = "napitest"
}
```
#### 增加子系统
在源码/build/subsystem_config.json中增加子系统选项。如下所示:
```
"napitest": {
"path": "foundation/napitest",
"name": "napitest"
}
```
### 添加功能模块
在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码vendor/hihope/rk3568/config.json中增加part选项,其中第一个napitest就是BUILD.gn文件中的subsystem_name,第二个napitest就是BUILD.gn文件中的part_name。
```
{
"subsystem": "napitest",
"components": [
{
"component": "napitest",
"features": []
}
]
}
```
### 编译验证
编译成功后,就会在 /out/产品名/packages/phone/system/lib/module/ 生成libnapitest.z.so,如下所示:
```
/out/rk3568/packages/phone/system/lib/module
```
## 应用测试
如何编写应用对接口进行测试并查看结果,请参考上一篇文章:
[OpenHarmony系统之Napi框架生成工具介绍](
https://www.51cto.com/article/778404.html
)
[/md]
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5