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