Native Bundle Development

When to Use

Use the native bundle APIs to obtain application information.

Available APIs

API Description
OH_NativeBundle_GetCurrentApplicationInfo Obtains the information about the current application.
OH_NativeBundle_GetAppId Obtains the appId information about the current application.
OH_NativeBundle_GetAppIdentifier Obtains the appIdentifier information about the current application.

How to Develop

  1. Create a project.
  1. Add dependencies.

    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.

    1. Open the src/main/cpp/CMakeLists.txt file, and add libbundle_ndk.z.so to target_link_libraries.

      target_link_libraries(entry PUBLIC libace_napi.z.so libbundle_ndk.z.so)
      
    2. Open the src/main/cpp/hello.cpp file, and add the header file.

      #include "bundle/native_interface_bundle.h"
      
  2. Modify the source file.

    When the src/main/cpp/hello.cpp file is opened, Init is called to initialize the API, which is getCurrentApplicationInfo.

    EXTERN_C_START
    static napi_value Init(napi_env env, napi_value exports)
    {
        napi_property_descriptor desc[] = {
            { "getCurrentApplicationInfo", nullptr, GetCurrentApplicationInfo, nullptr, nullptr, nullptr, napi_default, nullptr}
        };
    
        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
        return exports;
    }
    EXTERN_C_END
    
    1. Add the API to the src/main/cpp/hello.cpp file.

      static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info)
      
    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.

      static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info)
      {
          // Call the native API to obtain the application information.
          OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo();
          napi_value result = nullptr;
          napi_create_object(env, &result);
          // Convert the bundle name obtained by calling the native API to the bundleName attribute in the JavaScript object.
          napi_value bundleName;
          napi_create_string_utf8(env, nativeApplicationInfo.bundleName, NAPI_AUTO_LENGTH, &bundleName);
          napi_set_named_property(env, result, "bundleName", bundleName);
          // Convert the fingerprint information obtained by calling the native API to the fingerprint attribute in the JavaScript object.
          napi_value fingerprint;
          napi_create_string_utf8(env, nativeApplicationInfo.fingerprint, NAPI_AUTO_LENGTH, &fingerprint);
          napi_set_named_property(env, result, "fingerprint", fingerprint);
      
          char* appId = OH_NativeBundle_GetAppId();
          // Convert the application ID obtained by calling the native API to the appId attribute in the JavaScript object.
          napi_value napi_appId;
          napi_create_string_utf8(env, appId, NAPI_AUTO_LENGTH, &napi_appId);
          napi_set_named_property(env, result, "appId", napi_appId);
      
          char* appIdentifier = OH_NativeBundle_GetAppIdentifier();
          // Convert the application identifier obtained by calling the native API to the appIdentifier attribute in the JavaScript object.
          napi_value napi_appIdentifier;
          napi_create_string_utf8(env, appIdentifier, NAPI_AUTO_LENGTH, &napi_appIdentifier);
          napi_set_named_property(env, result, "appIdentifier", napi_appIdentifier);
          // To prevent memory leak, manually release the memory.
          free(nativeApplicationInfo.bundleName);
          free(nativeApplicationInfo.fingerprint);
          free(appId);
          free(appIdentifier);
          return result;
      }
      
  3. Call APIs on the JavaScript side.

    1. Open the src\main\ets\pages\index.ets file, and import libentry.so.

    2. Call the native API getCurrentApplicationInfo() to obtain application information. An example is as follows:

      import hilog from '@ohos.hilog';
      import testNapi from 'libentry.so';
      
      @Entry
      @Component
      struct Index {
      @State message: string = 'Hello World';
      
          build() {
              Row() {
              Column() {
                  Text(this.message)
                  .fontSize(50)
                  .fontWeight(FontWeight.Bold)
      
                  Button(){
                  Text("GetCurrentApplicationInfo").fontSize(30)
                  }.type(ButtonType.Capsule)
                  .margin({
                  top: 20
                  })
                  .backgroundColor('#0D9FFB')
                  .width('70%')
                  .height('5%')
                  .onClick(()=>{
                  try {
                      let data = testNapi.getCurrentApplicationInfo();
                      console.info("getCurrentApplicationInfo success, data is " + JSON.stringify(data));
                  } catch (error) {
                      console.error("getCurrentApplicationInfo failed");
                      this.message = "getCurrentApplicationInfo failed";
                  }
                  })
              }
              .width('100%')
              }
              .height('100%')
          }
      }
      

For details about the APIs, see Bundle.