2022-03-14 07:47:35 +00:00
|
|
|
|
# NAPI框架生成代码集成到OpenHarmony的方法
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
|
|
|
|
## 场景说明
|
|
|
|
|
|
2022-03-15 06:18:20 +00:00
|
|
|
|
为了实现工具生成的接口被其他子系统或者应用调用,需将生成的代码编译集成到OpenHarmony系统中,使其生成动态库,供OpenHarmony应用层调用。
|
2022-08-11 08:05:36 +00:00
|
|
|
|
本文介绍如何将工具生成的源码利用OpenHarmony编译系统生成动态库供应用层调用,主要是有以下两种方式,分别为增加ohos.build文件方式和增加bundle.json文件方式。
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
## 4.0 版本
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
### 建立模块位置
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-12-15 02:37:52 +00:00
|
|
|
|
模块目录理论上可在OpenHarmony工程的任一位置,假设OpenHarmony代码库的目录为OHOS_SRC,在OHOS_SRC/foundation目录下,建测试模块目录:napitest。napitest目录结构如下:
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2023-12-15 02:37:52 +00:00
|
|
|
|
napitest
|
2023-11-20 11:53:23 +00:00
|
|
|
|
|-- generatorCode // 工具代码部分
|
|
|
|
|
|-- |-- binding.gyp
|
|
|
|
|
|-- |-- BUILD.gn
|
|
|
|
|
|-- |-- bundle.json
|
|
|
|
|
|-- |-- napitest.cpp
|
|
|
|
|
|-- |-- napitest.h
|
|
|
|
|
|-- |-- napitest_middle.h
|
|
|
|
|
|-- |-- napitest_middle.cpp
|
|
|
|
|
|-- |-- test.sh
|
|
|
|
|
|-- |-- tool_utility.cpp
|
|
|
|
|
|-- |-- tool_utility.h
|
|
|
|
|
|-- |-- napi_gen.log
|
|
|
|
|
|-- serviceCode // 放置业务代码部分
|
2023-12-15 02:37:52 +00:00
|
|
|
|
|-- |-- NodeISayHello.h
|
|
|
|
|
|-- |-- NodeISayHello.cpp
|
2022-08-11 08:05:36 +00:00
|
|
|
|
|
2023-12-15 02:37:52 +00:00
|
|
|
|
其中generatorCode为工具生成的代码,serviceCode 为用户配置的业务代码, bundle.json 为新增的编译配置文件。
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-12-15 02:37:52 +00:00
|
|
|
|
### 编译修改点
|
2022-08-11 08:05:36 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
#### 修改bundle.json文件
|
2022-08-11 08:05:36 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
其中destPath选项中的"//foundation/napitest/generatorCode"指的是napitest目录,":napitest"指的是上面BUILD.gn中的目标ohos_shared_library("napitest")。
|
2022-08-11 08:05:36 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
"name": "@ohos/napitest",
|
|
|
|
|
"description": "napitest provides atomic capabilities",
|
2023-11-20 11:53:23 +00:00
|
|
|
|
"version": "4.0",
|
2022-08-11 08:05:36 +00:00
|
|
|
|
"license": "Apache License 2.0",
|
|
|
|
|
"publishAs": "code-segment",
|
|
|
|
|
"segment": {
|
2023-11-20 11:53:23 +00:00
|
|
|
|
"destPath": "foundation/napitest/generatorCode"
|
2022-08-11 08:05:36 +00:00
|
|
|
|
},
|
|
|
|
|
"dirs": {},
|
|
|
|
|
"scripts": {},
|
|
|
|
|
"component": {
|
2023-05-13 07:01:20 +00:00
|
|
|
|
"name": "napitest",
|
2022-08-11 08:05:36 +00:00
|
|
|
|
"subsystem": "napitest",
|
|
|
|
|
"features": [],
|
|
|
|
|
"adapted_system_type": [
|
|
|
|
|
"standard"
|
|
|
|
|
],
|
|
|
|
|
"rom": "10000KB",
|
|
|
|
|
"ram": "10000KB",
|
|
|
|
|
"deps": {
|
|
|
|
|
"components": [
|
|
|
|
|
"ace_napi",
|
|
|
|
|
"ipc_core",
|
|
|
|
|
"libhilog"
|
|
|
|
|
],
|
|
|
|
|
"third_party": [
|
|
|
|
|
"node"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"build": {
|
|
|
|
|
"sub_component": [
|
2023-11-20 11:53:23 +00:00
|
|
|
|
"//foundation/napitest/generatorCode:napitest"
|
2022-08-11 08:05:36 +00:00
|
|
|
|
],
|
|
|
|
|
"inner_kits": [
|
|
|
|
|
{
|
|
|
|
|
"header": {
|
2023-11-20 11:53:23 +00:00
|
|
|
|
"header_base": "//foundation/napitest/generatorCode",
|
2022-08-11 08:05:36 +00:00
|
|
|
|
"header_files": [
|
2022-09-21 07:29:31 +00:00
|
|
|
|
"tool_utility.h",
|
2023-11-20 11:53:23 +00:00
|
|
|
|
"napitest.h",
|
|
|
|
|
"napitest_middle.h"
|
2022-08-11 08:05:36 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
2023-11-20 11:53:23 +00:00
|
|
|
|
"name": "//foundation/napitest/generatorCode:napitest"
|
2022-08-11 08:05:36 +00:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
#### 增加子系统
|
2022-08-11 08:05:36 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
在源码/build/subsystem_config.json中增加子系统选项。如下所示:
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
|
|
|
|
```
|
2023-11-20 11:53:23 +00:00
|
|
|
|
"napitest": {
|
|
|
|
|
"path": "foundation/napitest/generatorCode",
|
|
|
|
|
"name": "napitest"
|
|
|
|
|
}
|
2023-05-13 07:01:20 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
### 添加功能模块
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码vendor/hihope/rk3568/config.json中增加part选项,其中第一个napitest就是BUILD.gn文件中的subsystem_name,第二个napitest就是BUILD.gn文件中的part_name。
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
|
|
|
|
```
|
2023-11-20 11:53:23 +00:00
|
|
|
|
{
|
|
|
|
|
"subsystem": "napitest",
|
|
|
|
|
"components": [
|
|
|
|
|
{
|
|
|
|
|
"component": "napitest",
|
|
|
|
|
"features": []
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
2022-03-13 21:53:51 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
### 编译验证
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
编译成功后,就会在 /out/产品名/packages/phone/system/lib/module/ 生成libnapitest.z.so,如下所示:
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
/out/rk3568/packages/phone/system/lib/module
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
### 备注
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
若自动配置业务代码不能满足业务场景,用户可以手动配置业务代码,以下为用户手动配置业务代码并集成到OpenHarmony上的方法:
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-11-27 02:59:33 +00:00
|
|
|
|
[4.0版本手动配置业务代码集成方法](https://gitee.com/openharmony/napi_generator/blob/master/docs/ENSEMBLE_METHOD_4.0CFGCODE.md)
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
|
|
|
|
## 3.2 版本
|
|
|
|
|
|
|
|
|
|
### 建立模块位置
|
|
|
|
|
|
2023-12-15 02:37:52 +00:00
|
|
|
|
模块目录理论上可在OpenHarmony工程的任一位置,假设OpenHarmony代码库的目录为OHOS_SRC,在OHOS_SRC/foundation目录下,建测试模块目录:napitest。napitest目录结构如下:
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-12-15 02:37:52 +00:00
|
|
|
|
napitest
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|-- binding.gyp
|
|
|
|
|
|-- BUILD.gn
|
|
|
|
|
|-- bundle.json
|
|
|
|
|
|-- napitest.cpp
|
|
|
|
|
|-- napitest.h
|
2023-11-20 11:53:23 +00:00
|
|
|
|
|-- napitest_middle.h
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|-- napitest_middle.cpp
|
|
|
|
|
|-- test.sh
|
|
|
|
|
|-- tool_utility.cpp
|
|
|
|
|
|-- tool_utility.h
|
|
|
|
|
|
2023-12-15 02:37:52 +00:00
|
|
|
|
其中bundle.json为新建的编译配置文件,其它为工具生成的代码。
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-12-15 02:37:52 +00:00
|
|
|
|
### 编译修改点
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
|
|
|
|
#### 修改bundle.json文件
|
|
|
|
|
|
|
|
|
|
其中destPath选项中的"//foundation/napitest"指的是napitest目录,":napitest"指的是上面BUILD.gn中的目标ohos_shared_library("napitest")。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
"name": "@ohos/napitest",
|
|
|
|
|
"description": "napitest provides atomic capabilities",
|
|
|
|
|
"version": "3.2",
|
|
|
|
|
"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": [
|
|
|
|
|
"ace_napi",
|
|
|
|
|
"ipc_core",
|
|
|
|
|
"libhilog"
|
|
|
|
|
],
|
|
|
|
|
"third_party": [
|
|
|
|
|
"node"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"build": {
|
|
|
|
|
"sub_component": [
|
|
|
|
|
"//foundation/napitest:napitest"
|
|
|
|
|
],
|
|
|
|
|
"inner_kits": [
|
|
|
|
|
{
|
|
|
|
|
"header": {
|
|
|
|
|
"header_base": "//foundation/napitest",
|
|
|
|
|
"header_files": [
|
|
|
|
|
"tool_utility.h",
|
2023-11-20 11:53:23 +00:00
|
|
|
|
"napitest.h",
|
|
|
|
|
"napitest_middle.h"
|
2023-05-13 07:01:20 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"name": "//foundation/napitest:napitest"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 修改napitest.cpp文件
|
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
为方便调试,在napitest.cpp文件中增加业务代码。以修改napitest.cpp文件为例,在以下方法中增加业务代码,
|
|
|
|
|
|
|
|
|
|
在sayHello方法中增加注册的object回调方法的调用:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
...
|
|
|
|
|
// 业务代码调用 onSayHelloStart callback
|
|
|
|
|
napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloStartCallback(info1);
|
|
|
|
|
// 业务代码调用 onSayHelloEnd callback
|
|
|
|
|
napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloEndCallback(info2);
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在sayHi方法中增加register注册的回调方法的调用:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
...
|
|
|
|
|
napitest::napitest_interface::NodeISayHello *ptr = new napitest::napitest_interface::NodeISayHello();
|
|
|
|
|
uint32_t callbackNum = 50;
|
|
|
|
|
ptr->CallbackfuncCallback(callbackNum);
|
|
|
|
|
delete ptr;
|
|
|
|
|
...
|
|
|
|
|
```
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
在sayHelloWithResponse方法中增加Promise回调方法的调用:
|
2023-05-13 07:01:20 +00:00
|
|
|
|
|
|
|
|
|
```
|
2023-11-20 11:53:23 +00:00
|
|
|
|
...
|
|
|
|
|
out.errMsg = "";
|
|
|
|
|
out.response = "rec hello.";
|
|
|
|
|
out.result = 0;
|
|
|
|
|
...
|
2023-05-13 07:01:20 +00:00
|
|
|
|
```
|
2023-05-04 03:57:00 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
在funcTest方法中增加普通函数的业务逻辑:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
...
|
|
|
|
|
if (v) {
|
|
|
|
|
out = "ret is true";
|
|
|
|
|
} else {
|
|
|
|
|
out = "ret is false";
|
|
|
|
|
}
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
增加业务代码之后的文件如下所示:
|
|
|
|
|
|
|
|
|
|
[napitest.cpp](https://gitee.com/openharmony/napi_generator/blob/master/examples/napitest.cpp)
|
|
|
|
|
|
2023-05-13 07:01:20 +00:00
|
|
|
|
#### 增加子系统
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2023-05-13 07:01:20 +00:00
|
|
|
|
在源码/build/subsystem_config.json中增加子系统选项。如下所示:
|
2023-05-04 03:57:00 +00:00
|
|
|
|
|
2023-05-13 07:01:20 +00:00
|
|
|
|
```
|
|
|
|
|
"napitest": {
|
|
|
|
|
"path": "foundation/napitest",
|
|
|
|
|
"name": "napitest"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 添加功能模块
|
|
|
|
|
|
|
|
|
|
在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码vendor/hihope/rk3568/config.json中增加part选项,其中第一个napitest就是BUILD.gn文件中的subsystem_name,第二个napitest就是BUILD.gn文件中的part_name。
|
2023-05-04 03:57:00 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
"subsystem": "napitest",
|
|
|
|
|
"components": [
|
|
|
|
|
{
|
2023-05-13 07:01:20 +00:00
|
|
|
|
"component": "napitest",
|
2023-05-04 03:57:00 +00:00
|
|
|
|
"features": []
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-11 08:05:36 +00:00
|
|
|
|
### 编译验证
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2023-05-04 03:57:00 +00:00
|
|
|
|
编译成功后,就会在 /out/产品名/packages/phone/system/lib/module/ 生成libnapitest.z.so,如下所示:
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2023-05-13 07:01:20 +00:00
|
|
|
|
/out/rk3568/packages/phone/system/lib/module
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
## 3.1 版本
|
|
|
|
|
|
2023-11-27 02:59:33 +00:00
|
|
|
|
[3.1版本集成方法](https://gitee.com/openharmony/napi_generator/blob/master/docs/ENSEMBLE_METHOD_3.1VERSION.md)
|
2023-11-20 11:53:23 +00:00
|
|
|
|
|
2022-08-11 08:05:36 +00:00
|
|
|
|
## 总结
|
|
|
|
|
|
2023-05-13 07:01:20 +00:00
|
|
|
|
3.1版本两种集成方式使用场景说明:
|
2022-08-11 08:05:36 +00:00
|
|
|
|
|
|
|
|
|
ohos.build方式集成:适合3.0前版本使用。
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2022-08-11 08:05:36 +00:00
|
|
|
|
bundle.json方式集成:兼容ohos.build方式,但3.1及以后版本建议使用此种方式集成。
|
2022-03-13 21:53:51 +00:00
|
|
|
|
|
2023-05-13 07:01:20 +00:00
|
|
|
|
3.2版本适合使用bundle.json方式集成。
|
|
|
|
|
|
2023-11-20 11:53:23 +00:00
|
|
|
|
4.0版本适合使用bundle.json方式集成。
|
|
|
|
|
|