鸿蒙HarmonyOS应用开发之NativeBundle开发指导

场景介绍

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

接口说明

开发步骤

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文件中,增加对应的方法,如下所示:
static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info)
  1. 在hello.cpp文件中获取Native的包信息对象,并转为js的包信息对象,即可在js测获取应用的信息:
static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info)
{
    // 调用Native接口获取应用信息
    OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo();
    napi_value result = nullptr;
    napi_create_object(env, &result);
    // Native接口获取的应用包名转为js对象里的bundleName属性
    napi_value bundleName;
    napi_create_string_utf8(env, nativeApplicationInfo.bundleName, NAPI_AUTO_LENGTH, &bundleName);
    napi_set_named_property(env, result, "bundleName", bundleName);
    // Native接口获取的指纹信息转为js对象里的fingerprint属性
    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();
    // Native接口获取的appId转为js对象里的appId属性
    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();
    // Native接口获取的appIdentifier转为js对象里的appIdentifier属性
    napi_value napi_appIdentifier;
    napi_create_string_utf8(env, appIdentifier, NAPI_AUTO_LENGTH, &napi_appIdentifier);
    napi_set_named_property(env, result, "appIdentifier", napi_appIdentifier);
    // 最后为了防止内存泄漏,手动释放
    free(nativeApplicationInfo.bundleName);
    free(nativeApplicationInfo.fingerprint);
    free(appId);
    free(appIdentifier);
    return result;
}

4. js侧调用

  1. 打开src\main\ets\pages\index.ets, 导入"libentry.so"。

  2. 调用Native接口getCurrentApplicationInfo即可获取应用信息。示例如下:

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%')
    }
}

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

相关推荐

  1. 鸿蒙(HarmonyOS)应用开发——保存应用数据

    2024-03-29 17:36:01       48 阅读
  2. 鸿蒙(HarmonyOS)应用开发——应用通知

    2024-03-29 17:36:01       47 阅读
  3. 鸿蒙(HarmonyOS)应用开发——管理组件状态

    2024-03-29 17:36:01       38 阅读
  4. 鸿蒙(HarmonyOS)应用开发——http的使用

    2024-03-29 17:36:01       47 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-29 17:36:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-29 17:36:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-29 17:36:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-29 17:36:01       18 阅读

热门阅读

  1. 【ARM64 常见汇编指令学习 21 -- ARM RET 与 ERET】

    2024-03-29 17:36:01       16 阅读
  2. Qt常用容器之:QVector

    2024-03-29 17:36:01       16 阅读
  3. 游戏客户客户端面经

    2024-03-29 17:36:01       14 阅读
  4. Spring 基于XML的DI

    2024-03-29 17:36:01       18 阅读
  5. CAPL如何使用socket套接字实现TCP通信(断开连接)

    2024-03-29 17:36:01       21 阅读
  6. SSL证书部署完成后仍然不安全是怎么回事?

    2024-03-29 17:36:01       17 阅读
  7. 浏览器中的HttpOnly是什么

    2024-03-29 17:36:01       16 阅读