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