1# NativeBundle开发指导 2 3## 场景介绍 4 5开发者可以通过本指导了解在OpenHarmony应用中,如何使用Native Bundle接口获取应用自身相关信息。 6 7## 接口说明 8 9| 接口名 | 描述 | 10| :----------------------------------------------------------- | :--------------------------------------- | 11| [OH_NativeBundle_GetCurrentApplicationInfo](../reference/apis-ability-kit/_bundle.md#oh_nativebundle_getcurrentapplicationinfo) | 获取应用自身相关信息。 | 12| [OH_NativeBundle_GetAppId](../reference/apis-ability-kit/_bundle.md#oh_nativebundle_getappid) | 获取自身应用的appId信息。 | 13| [OH_NativeBundle_GetAppIdentifier](../reference/apis-ability-kit/_bundle.md#oh_nativebundle_getappidentifier) | 获取自身应用的appIdentifier信息。 | 14 15## 开发步骤 16 17**1. 创建工程** 18 19<div style="text-align:center;"> 20 <img src="figures/rawfile1.png"> 21</div> 22 23**2. 添加依赖** 24 25创建完成后,IDE会在工程生成cpp目录,目录有libentry/index.d.ts、hello.cpp、CMakeLists.txt等文件。 26 271. 打开src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加包管理的libbundle_ndk.z.so。 28 29 ```c++ 30 target_link_libraries(entry PUBLIC libace_napi.z.so libbundle_ndk.z.so) 31 ``` 32 332. 打开src/main/cpp/hello.cpp文件,添加头文件。 34 35 ```c++ 36 #include "bundle/native_interface_bundle.h" 37 ``` 38 39**3. 修改源文件** 40 411. 打开src/main/cpp/hello.cpp文件,文件Init会对当前方法进行初始化映射,这里定义对外接口为getCurrentApplicationInfo。 42 43 ```c++ 44 EXTERN_C_START 45 static napi_value Init(napi_env env, napi_value exports) 46 { 47 napi_property_descriptor desc[] = { 48 { "getCurrentApplicationInfo", nullptr, GetCurrentApplicationInfo, nullptr, nullptr, nullptr, napi_default, nullptr} 49 }; 50 51 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 52 return exports; 53 } 54 EXTERN_C_END 55 ``` 56 572. 把src/main/cpp/hello.cpp文件中,增加对应的方法,如下所示: 58 59 ```c++ 60 static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info) 61 ``` 62 633. 在hello.cpp文件中获取Native的包信息对象,并转为js的包信息对象,即可在js测获取应用的信息: 64 65 ```c++ 66 static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info) 67 { 68 // 调用Native接口获取应用信息 69 OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo(); 70 napi_value result = nullptr; 71 napi_create_object(env, &result); 72 // Native接口获取的应用包名转为js对象里的bundleName属性 73 napi_value bundleName; 74 napi_create_string_utf8(env, nativeApplicationInfo.bundleName, NAPI_AUTO_LENGTH, &bundleName); 75 napi_set_named_property(env, result, "bundleName", bundleName); 76 // Native接口获取的指纹信息转为js对象里的fingerprint属性 77 napi_value fingerprint; 78 napi_create_string_utf8(env, nativeApplicationInfo.fingerprint, NAPI_AUTO_LENGTH, &fingerprint); 79 napi_set_named_property(env, result, "fingerprint", fingerprint); 80 81 char* appId = OH_NativeBundle_GetAppId(); 82 // Native接口获取的appId转为js对象里的appId属性 83 napi_value napi_appId; 84 napi_create_string_utf8(env, appId, NAPI_AUTO_LENGTH, &napi_appId); 85 napi_set_named_property(env, result, "appId", napi_appId); 86 87 char* appIdentifier = OH_NativeBundle_GetAppIdentifier(); 88 // Native接口获取的appIdentifier转为js对象里的appIdentifier属性 89 napi_value napi_appIdentifier; 90 napi_create_string_utf8(env, appIdentifier, NAPI_AUTO_LENGTH, &napi_appIdentifier); 91 napi_set_named_property(env, result, "appIdentifier", napi_appIdentifier); 92 // 最后为了防止内存泄漏,手动释放 93 free(nativeApplicationInfo.bundleName); 94 free(nativeApplicationInfo.fingerprint); 95 free(appId); 96 free(appIdentifier); 97 return result; 98 } 99 ``` 100 101**4. js侧调用** 102 1031. 打开src\main\ets\pages\index.ets, 导入"libentry.so"。 104 105 1062. 调用Native接口getCurrentApplicationInfo即可获取应用信息。示例如下: 107 108 ```js 109 import hilog from '@ohos.hilog'; 110 import testNapi from 'libentry.so'; 111 112 @Entry 113 @Component 114 struct Index { 115 @State message: string = 'Hello World'; 116 117 build() { 118 Row() { 119 Column() { 120 Text(this.message) 121 .fontSize(50) 122 .fontWeight(FontWeight.Bold) 123 124 Button(){ 125 Text("GetCurrentApplicationInfo").fontSize(30) 126 }.type(ButtonType.Capsule) 127 .margin({ 128 top: 20 129 }) 130 .backgroundColor('#0D9FFB') 131 .width('70%') 132 .height('5%') 133 .onClick(()=>{ 134 try { 135 let data = testNapi.getCurrentApplicationInfo(); 136 console.info("getCurrentApplicationInfo success, data is " + JSON.stringify(data)); 137 } catch (error) { 138 console.error("getCurrentApplicationInfo failed"); 139 this.message = "getCurrentApplicationInfo failed"; 140 } 141 }) 142 } 143 .width('100%') 144 } 145 .height('100%') 146 } 147 } 148 ``` 149 150关于包管理NDK开发,可参考[Bundle模块介绍](../reference/apis-ability-kit/_bundle.md)。 151