HarmonyOS Next开发学习手册——【包管理】NativeBundle开发

场景介绍

开发者可以通过本指导了解在HarmonyOS应用中,如何使用Native Bundle接口获取应用自身相关信息。

接口说明

接口名 描述
OH_NativeBundle_GetCurrentApplicationInfo 获取应用自身相关信息。
OH_NativeBundle_GetAppId 获取自身应用的appId信息。
OH_NativeBundle_GetAppIdentifier 获取自身应用的appIdentifier信息。

开发步骤

1. 创建工程

2. 添加依赖

创建完成后,IDE会在工程生成cpp目录,目录有libentry/index.d.ts、hello.cpp、CMakeLists.txt等文件。

  1. 打开src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加包管理的libbundle_ndk.z.so。
target_link_libraries(entry PUBLIC libace_napi.z.so libbundle_ndk.z.so)
  1. 打开src/main/cpp/hello.cpp文件,添加头文件。
#include "bundle/native_interface_bundle.h"

3. 修改源文件

  1. 打开src/main/cpp/hello.cpp文件,文件Init会对当前方法进行初始化映射,这里定义对外接口为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. 把src/main/cpp/hello.cpp文件中,增加对应的方法,如下所示:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值