• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Native Bundle Development
2
3## When to Use
4
5Use the native bundle APIs to obtain application information.
6
7## Available APIs
8
9| API                                                      | Description                                    |
10| :----------------------------------------------------------- | :--------------------------------------- |
11| [OH_NativeBundle_GetCurrentApplicationInfo](../reference/apis-ability-kit/_bundle.md#oh_nativebundle_getcurrentapplicationinfo) | Obtains the information about the current application.         |
12| [OH_NativeBundle_GetAppId](../reference/apis-ability-kit/_bundle.md#oh_nativebundle_getappid) | Obtains the appId information about the current application.|
13| [OH_NativeBundle_GetAppIdentifier](../reference/apis-ability-kit/_bundle.md#oh_nativebundle_getappidentifier) | Obtains the appIdentifier information about the current application.|
14
15## How to Develop
16
171. **Create a project.**
18
19<div style="text-align:center;">
20  <img src="figures/rawfile1.png">
21</div>
22
232. **Add dependencies.**
24
25   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**.
26
27   1. Open the **src/main/cpp/CMakeLists.txt** file, and add **libbundle_ndk.z.so** to **target_link_libraries**.
28
29       ```c++
30       target_link_libraries(entry PUBLIC libace_napi.z.so libbundle_ndk.z.so)
31       ```
32
33   2. Open the **src/main/cpp/hello.cpp** file, and add the header file.
34
35       ```c++
36       #include "bundle/native_interface_bundle.h"
37       ```
38
393. **Modify the source file.**
40
41   When the **src/main/cpp/hello.cpp** file is opened, **Init** is called to initialize the API, which is **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
57   1. Add the API to the **src/main/cpp/hello.cpp** file.
58
59       ```c++
60       static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info)
61       ```
62
63   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.
64
65       ```c++
66       static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info)
67       {
68           // Call the native API to obtain the application information.
69           OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo();
70           napi_value result = nullptr;
71           napi_create_object(env, &result);
72           // Convert the bundle name obtained by calling the native API to the bundleName attribute in the JavaScript object.
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           // Convert the fingerprint information obtained by calling the native API to the fingerprint attribute in the JavaScript object.
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           // Convert the application ID obtained by calling the native API to the appId attribute in the JavaScript object.
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           // Convert the application identifier obtained by calling the native API to the appIdentifier attribute in the JavaScript object.
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           // To prevent memory leak, manually release the memory.
93           free(nativeApplicationInfo.bundleName);
94           free(nativeApplicationInfo.fingerprint);
95           free(appId);
96           free(appIdentifier);
97           return result;
98       }
99       ```
100
1014. **Call APIs on the JavaScript side.**
102
103   1. Open the **src\main\ets\pages\index.ets** file, and import **libentry.so**.
104
105   2. Call the native API **getCurrentApplicationInfo()** to obtain application information. An example is as follows:
106
107       ```js
108       import hilog from '@ohos.hilog';
109       import testNapi from 'libentry.so';
110
111       @Entry
112       @Component
113       struct Index {
114       @State message: string = 'Hello World';
115
116           build() {
117               Row() {
118               Column() {
119                   Text(this.message)
120                   .fontSize(50)
121                   .fontWeight(FontWeight.Bold)
122
123                   Button(){
124                   Text("GetCurrentApplicationInfo").fontSize(30)
125                   }.type(ButtonType.Capsule)
126                   .margin({
127                   top: 20
128                   })
129                   .backgroundColor('#0D9FFB')
130                   .width('70%')
131                   .height('5%')
132                   .onClick(()=>{
133                   try {
134                       let data = testNapi.getCurrentApplicationInfo();
135                       console.info("getCurrentApplicationInfo success, data is " + JSON.stringify(data));
136                   } catch (error) {
137                       console.error("getCurrentApplicationInfo failed");
138                       this.message = "getCurrentApplicationInfo failed";
139                   }
140                   })
141               }
142               .width('100%')
143               }
144               .height('100%')
145           }
146       }
147       ```
148
149For details about the APIs, see [Bundle](../reference/apis-ability-kit/_bundle.md).
150
151