1# 手动配置业务代码后集成到OpenHarmony的方法 2 3## 场景说明 4 5为了实现工具生成的接口被其它子系统或者应用调用,需将生成的代码编译集成到OpenHarmony系统中,使其生成动态库,供OpenHarmony应用层调用。 6本文介绍如何手动配置业务代码并将生成的代码集成到OpenHarmony 4.0 Release。 7 8## 4.0 版本 9 10### 建立模块位置 11 12模块目录理论上可在OpenHarmony工程的任一位置,假设OpenHarmony代码库的目录为OHOS_SRC,在OHOS_SRC/foundation目录下,建测试模块目录:napitest。napitest目录结构如下: 13 14 napitest 15 |-- binding.gyp 16 |-- BUILD.gn 17 |-- bundle.json 18 |-- napitest.cpp 19 |-- napitest.h 20 |-- napitest_middle.h 21 |-- napitest_middle.cpp 22 |-- test.sh 23 |-- tool_utility.cpp 24 |-- tool_utility.h 25 26其中bundle.json为新增的编译配置文件,其它为工具生成的代码。 27 28### 编译修改点 29 30#### 修改bundle.json文件 31 32其中destPath选项中的"//foundation/napitest"指的是napitest目录,":napitest"指的是上面BUILD.gn中的目标ohos_shared_library("napitest")。 33 34``` 35{ 36 "name": "@ohos/napitest", 37 "description": "napitest provides atomic capabilities", 38 "version": "4.0", 39 "license": "Apache License 2.0", 40 "publishAs": "code-segment", 41 "segment": { 42 "destPath": "foundation/napitest" 43 }, 44 "dirs": {}, 45 "scripts": {}, 46 "component": { 47 "name": "napitest", 48 "subsystem": "napitest", 49 "features": [], 50 "adapted_system_type": [ 51 "standard" 52 ], 53 "rom": "10000KB", 54 "ram": "10000KB", 55 "deps": { 56 "components": [ 57 "ace_napi", 58 "ipc_core", 59 "libhilog" 60 ], 61 "third_party": [ 62 "node" 63 ] 64 }, 65 "build": { 66 "sub_component": [ 67 "//foundation/napitest:napitest" 68 ], 69 "inner_kits": [ 70 { 71 "header": { 72 "header_base": "//foundation/napitest", 73 "header_files": [ 74 "tool_utility.h", 75 "napitest.h", 76 "napitest_middle.h" 77 ] 78 }, 79 "name": "//foundation/napitest:napitest" 80 } 81 ] 82 } 83 } 84} 85``` 86 87#### 修改napitest.cpp文件 88 89为方便调试,在napitest.cpp文件中增加业务代码。以修改napitest.cpp文件为例,在以下方法中增加业务代码, 90 91在sayHello方法中增加注册的object回调方法的调用: 92 93``` 94... 95// 业务代码调用 onSayHelloStart callback 96napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloStartCallback(info1); 97// 业务代码调用 onSayHelloEnd callback 98napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloEndCallback(info2); 99... 100``` 101 102在sayHi方法中增加register注册的回调方法的调用: 103 104``` 105... 106napitest::napitest_interface::NodeISayHello *ptr = new napitest::napitest_interface::NodeISayHello(); 107uint32_t callbackNum = 50; 108ptr->CallbackfuncCallback(callbackNum); 109delete ptr; 110... 111``` 112 113在sayHelloWithResponse方法中增加Promise回调方法的调用: 114 115``` 116... 117out.errMsg = ""; 118out.response = "rec hello."; 119out.result = 0; 120... 121``` 122 123在funcTest方法中增加普通函数的业务逻辑: 124 125``` 126... 127if (v) { 128 out = "ret is true"; 129} else { 130 out = "ret is false"; 131} 132... 133``` 134 135增加业务代码之后的文件如下所示: 136 137[napitest.cpp](https://gitee.com/openharmony/napi_generator/blob/master/examples/napitest.cpp) 138 139#### 增加子系统 140 141在源码/build/subsystem_config.json中增加子系统选项。如下所示: 142 143``` 144"napitest": { 145 "path": "foundation/napitest", 146 "name": "napitest" 147 } 148``` 149 150### 添加功能模块 151 152在产品配置中添加上述子系统的功能模块,编译到产品产出文件中,例如在源码vendor/hihope/rk3568/config.json中增加part选项,其中第一个napitest就是BUILD.gn文件中的subsystem_name,第二个napitest就是BUILD.gn文件中的part_name。 153 154``` 155{ 156 "subsystem": "napitest", 157 "components": [ 158 { 159 "component": "napitest", 160 "features": [] 161 } 162 ] 163} 164``` 165 166### 编译验证 167 168编译成功后,就会在 /out/产品名/packages/phone/system/lib/module/ 生成libnapitest.z.so,如下所示: 169 170 /out/rk3568/packages/phone/system/lib/module 171 172