1e41f4b71Sopenharmony_ci# Native Bundle Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciUse the native bundle APIs to obtain application information. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Available APIs 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci| API | Description | 10e41f4b71Sopenharmony_ci| :----------------------------------------------------------- | :--------------------------------------- | 11e41f4b71Sopenharmony_ci| [OH_NativeBundle_GetCurrentApplicationInfo](../reference/apis-ability-kit/_bundle.md#oh_nativebundle_getcurrentapplicationinfo) | Obtains the information about the current application. | 12e41f4b71Sopenharmony_ci| [OH_NativeBundle_GetAppId](../reference/apis-ability-kit/_bundle.md#oh_nativebundle_getappid) | Obtains the appId information about the current application.| 13e41f4b71Sopenharmony_ci| [OH_NativeBundle_GetAppIdentifier](../reference/apis-ability-kit/_bundle.md#oh_nativebundle_getappidentifier) | Obtains the appIdentifier information about the current application.| 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## How to Develop 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci1. **Create a project.** 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci<div style="text-align:center;"> 20e41f4b71Sopenharmony_ci <img src="figures/rawfile1.png"> 21e41f4b71Sopenharmony_ci</div> 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci2. **Add dependencies.** 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci After the project is created, the **cpp** directory is created in the project directory. The directory contains files such as **libentry/index.d.ts**, **hello.cpp**, and **CMakeLists.txt**. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci 1. Open the **src/main/cpp/CMakeLists.txt** file, and add **libbundle_ndk.z.so** to **target_link_libraries**. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci ```c++ 30e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libace_napi.z.so libbundle_ndk.z.so) 31e41f4b71Sopenharmony_ci ``` 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci 2. Open the **src/main/cpp/hello.cpp** file, and add the header file. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci ```c++ 36e41f4b71Sopenharmony_ci #include "bundle/native_interface_bundle.h" 37e41f4b71Sopenharmony_ci ``` 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci3. **Modify the source file.** 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci When the **src/main/cpp/hello.cpp** file is opened, **Init** is called to initialize the API, which is **getCurrentApplicationInfo**. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci ```c++ 44e41f4b71Sopenharmony_ci EXTERN_C_START 45e41f4b71Sopenharmony_ci static napi_value Init(napi_env env, napi_value exports) 46e41f4b71Sopenharmony_ci { 47e41f4b71Sopenharmony_ci napi_property_descriptor desc[] = { 48e41f4b71Sopenharmony_ci { "getCurrentApplicationInfo", nullptr, GetCurrentApplicationInfo, nullptr, nullptr, nullptr, napi_default, nullptr} 49e41f4b71Sopenharmony_ci }; 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 52e41f4b71Sopenharmony_ci return exports; 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci EXTERN_C_END 55e41f4b71Sopenharmony_ci ``` 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci 1. Add the API to the **src/main/cpp/hello.cpp** file. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci ```c++ 60e41f4b71Sopenharmony_ci static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info) 61e41f4b71Sopenharmony_ci ``` 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci 2. Obtain the native bundle information object from the **hello.cpp** file and convert it to a JavaScript bundle information object. In this way, you can obtain the application information on the JavaScript side. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci ```c++ 66e41f4b71Sopenharmony_ci static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info) 67e41f4b71Sopenharmony_ci { 68e41f4b71Sopenharmony_ci // Call the native API to obtain the application information. 69e41f4b71Sopenharmony_ci OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo(); 70e41f4b71Sopenharmony_ci napi_value result = nullptr; 71e41f4b71Sopenharmony_ci napi_create_object(env, &result); 72e41f4b71Sopenharmony_ci // Convert the bundle name obtained by calling the native API to the bundleName attribute in the JavaScript object. 73e41f4b71Sopenharmony_ci napi_value bundleName; 74e41f4b71Sopenharmony_ci napi_create_string_utf8(env, nativeApplicationInfo.bundleName, NAPI_AUTO_LENGTH, &bundleName); 75e41f4b71Sopenharmony_ci napi_set_named_property(env, result, "bundleName", bundleName); 76e41f4b71Sopenharmony_ci // Convert the fingerprint information obtained by calling the native API to the fingerprint attribute in the JavaScript object. 77e41f4b71Sopenharmony_ci napi_value fingerprint; 78e41f4b71Sopenharmony_ci napi_create_string_utf8(env, nativeApplicationInfo.fingerprint, NAPI_AUTO_LENGTH, &fingerprint); 79e41f4b71Sopenharmony_ci napi_set_named_property(env, result, "fingerprint", fingerprint); 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci char* appId = OH_NativeBundle_GetAppId(); 82e41f4b71Sopenharmony_ci // Convert the application ID obtained by calling the native API to the appId attribute in the JavaScript object. 83e41f4b71Sopenharmony_ci napi_value napi_appId; 84e41f4b71Sopenharmony_ci napi_create_string_utf8(env, appId, NAPI_AUTO_LENGTH, &napi_appId); 85e41f4b71Sopenharmony_ci napi_set_named_property(env, result, "appId", napi_appId); 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci char* appIdentifier = OH_NativeBundle_GetAppIdentifier(); 88e41f4b71Sopenharmony_ci // Convert the application identifier obtained by calling the native API to the appIdentifier attribute in the JavaScript object. 89e41f4b71Sopenharmony_ci napi_value napi_appIdentifier; 90e41f4b71Sopenharmony_ci napi_create_string_utf8(env, appIdentifier, NAPI_AUTO_LENGTH, &napi_appIdentifier); 91e41f4b71Sopenharmony_ci napi_set_named_property(env, result, "appIdentifier", napi_appIdentifier); 92e41f4b71Sopenharmony_ci // To prevent memory leak, manually release the memory. 93e41f4b71Sopenharmony_ci free(nativeApplicationInfo.bundleName); 94e41f4b71Sopenharmony_ci free(nativeApplicationInfo.fingerprint); 95e41f4b71Sopenharmony_ci free(appId); 96e41f4b71Sopenharmony_ci free(appIdentifier); 97e41f4b71Sopenharmony_ci return result; 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci ``` 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci4. **Call APIs on the JavaScript side.** 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci 1. Open the **src\main\ets\pages\index.ets** file, and import **libentry.so**. 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci 2. Call the native API **getCurrentApplicationInfo()** to obtain application information. An example is as follows: 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci ```js 108e41f4b71Sopenharmony_ci import hilog from '@ohos.hilog'; 109e41f4b71Sopenharmony_ci import testNapi from 'libentry.so'; 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci @Entry 112e41f4b71Sopenharmony_ci @Component 113e41f4b71Sopenharmony_ci struct Index { 114e41f4b71Sopenharmony_ci @State message: string = 'Hello World'; 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci build() { 117e41f4b71Sopenharmony_ci Row() { 118e41f4b71Sopenharmony_ci Column() { 119e41f4b71Sopenharmony_ci Text(this.message) 120e41f4b71Sopenharmony_ci .fontSize(50) 121e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci Button(){ 124e41f4b71Sopenharmony_ci Text("GetCurrentApplicationInfo").fontSize(30) 125e41f4b71Sopenharmony_ci }.type(ButtonType.Capsule) 126e41f4b71Sopenharmony_ci .margin({ 127e41f4b71Sopenharmony_ci top: 20 128e41f4b71Sopenharmony_ci }) 129e41f4b71Sopenharmony_ci .backgroundColor('#0D9FFB') 130e41f4b71Sopenharmony_ci .width('70%') 131e41f4b71Sopenharmony_ci .height('5%') 132e41f4b71Sopenharmony_ci .onClick(()=>{ 133e41f4b71Sopenharmony_ci try { 134e41f4b71Sopenharmony_ci let data = testNapi.getCurrentApplicationInfo(); 135e41f4b71Sopenharmony_ci console.info("getCurrentApplicationInfo success, data is " + JSON.stringify(data)); 136e41f4b71Sopenharmony_ci } catch (error) { 137e41f4b71Sopenharmony_ci console.error("getCurrentApplicationInfo failed"); 138e41f4b71Sopenharmony_ci this.message = "getCurrentApplicationInfo failed"; 139e41f4b71Sopenharmony_ci } 140e41f4b71Sopenharmony_ci }) 141e41f4b71Sopenharmony_ci } 142e41f4b71Sopenharmony_ci .width('100%') 143e41f4b71Sopenharmony_ci } 144e41f4b71Sopenharmony_ci .height('100%') 145e41f4b71Sopenharmony_ci } 146e41f4b71Sopenharmony_ci } 147e41f4b71Sopenharmony_ci ``` 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ciFor details about the APIs, see [Bundle](../reference/apis-ability-kit/_bundle.md). 150