#冲刺创作新星# [七] 写个NAPI子系统 原创 精华

左翼风发
发布于 2022-9-30 08:54
浏览
4收藏

作者:王石

在《[六]第一个hap应用》我们熟悉了如何在OpenHarmony开发hap应用,后期的文章我们会写在hap应用里调用系统库甚至是动态库。此篇文章,我们主要是熟悉下NAPI框架,并一起写一个支持NAPI的子系统,这样以后当我们想在hap应用里加自己功能的时候就可以方便的添加。

NAPI框架简介

NAPI(Native API)组件是一套对外接口基于Node.js N-API规范开发的原生模块扩展开发框架。类似于Android的JNI,NAPI框架实现了应用层ts/ets/js语言编写的代码和OpenHarmony的native代码(c/c++)交互的能力。此框架由Node.js N-API框架扩展而来。

注意:OpenHarmony的标准系统是采用NAPI框架的,轻量系统则是采用jerryscript框架

#冲刺创作新星#  [七] 写个NAPI子系统-鸿蒙开发者社区

详细的内容介绍在一下链接内可以看到官方的说明:

参考链接:https://gitee.com/openharmony/arkui_napi

NAPI的使用

graph LR
创建d.ts --> 执行napi_generator --> 建立子系统 --> 引入子系统 --> 编译生成
  1. 创建d.ts: @ohos.napitest.d.ts, basic.d.ts

    • @ohos.napitest.d.ts是NAPI的声明文件,在DevEco Studio开发的时候会用到d.ts来检查语法和提供代码帮助;
    import {AsyncCallback} from './basic';
    
    /**
     * Provides interfaces to napitest.
     *
     * @since 7
     */
    declare namespace napitest {
      /**
       * Shuts down the system.
       *
       * <p>This method requires the ohos.permission.SHUTDOWN permission.
       *
       * @param reason Indicates the shutdown reason.
       * @systemapi
       * @since 7
       */
      function shutdownDevice(reason: string): void;
    
      /**
       * Restarts the system.
       *
       * <p>This method requires the ohos.permission.REBOOT permission.
       *
       * @param reason Indicates the restart reason. For example, "updater" indicates entering the updater mode
       * after the restart. If the parameter is not specified, the system enters the normal mode after the restart.
       * @since 7
       */
      function rebootDevice(reason: string): void;
    
      /**
       * Checks whether the screen of a device is on or off.
       *
       * @return Returns true if the screen is on; returns false otherwise.
       * @since 7
       */
      function isScreenOn(callback: AsyncCallback<boolean>): void;
      function isScreenOn(): Promise<boolean>;
    }
    export default napitest;
    
    • basic.d.ts:一些基础方法的声明
    export interface Callback<T> {
        (data: T): void;
    }
      
    export interface ErrorCallback<T extends Error = BusinessError> {
        (err: T): void;
    }
    
    export interface AsyncCallback<T> {
        (err: BusinessError, data: T): void;
    }
    
    export interface BusinessError extends Error {
        code: number;
    }
    
  2. 执行napi_generator

    建立个文件夹,将上面建立的两个d.ts和napi_generator放在一起

    //准备环境
    mkdir napitest
    cd napitest
    vim @ohos.napitest.d.ts
    vim basic.d.ts
    //拷贝napi_generator
    cp [路径]/napi_generator-linux .
    chmod +x napi_generator-linux
    //生成napitest代码
    ./napi_generator-linux -f @ohos.napitest.d.ts -o out
    //当看到success则说明烧录成功
    
    //检视out目录
    ├── binding.gyp			//工具中间文件
    ├── BUILD.gn			//之后需要用到的gn文件
    ├── napi_gen.log		//工具log
    ├── napitest.cpp		//自动生成的接口调用的实际代码
    ├── napitest.h			//自动生成的接口调用的实际代码
    ├── napitest_middle.cpp	//自动生成的napi适配代码
    ├── test.sh				//生成js代码的脚本,官方没给说明,试了下不可用
    ├── tool_utility.cpp	//自动生成的napi适配代码
    └── tool_utility.h		//自动生成的napi适配代码
    
  3. 建立子系统

    • 在OpenHarmony源码目录下建立foundation/napitest,将之前生成的文件拷贝到文件夹内
    foundation
    ├── ability
    ├── ai
    ├── arkui
    ├── barrierfree
    ├── bundlemanager
    ├── communication
    ├── deviceprofile
    ├── distributeddatamgr
    ├── distributedhardware
    ├── filemanagement
    ├── graphic
    ├── multimedia
    ├── multimodalinput
    ├── napitest
    │   ├── binding.gyp
    │   ├── BUILD.gn
    │   ├── bundle.json
    │   ├── napi_gen.log
    │   ├── napitest.cpp
    │   ├── napitest.h
    │   ├── napitest_middle.cpp
    │   ├── test.sh
    │   ├── tool_utility.cpp
    │   └── tool_utility.h
    ├── resourceschedule
    
    • 在目录里创建bundle.json,使用一下内容
    {
        "name": "@ohos/napitest",
        "description": "napitest provides atomic capabilities",
        "version": "3.1",
        "license": "Apache License 2.0",
        "publishAs": "code-segment",
        "segment": {
          "destPath": "foundation/napitest"
        },
        "dirs": {},
        "scripts": {},
        "component": {
          //部件名称
          "name": "napitest_interface",
          //子系统名称
          "subsystem": "napitest",
          "features": [],
          "adapted_system_type": [
            "standard"
          ],
          "rom": "10000KB",
          "ram": "10000KB",
          "deps": {
            "components": [
              "ace_napi",
              "ipc_core",
              "libhilog"
            ],
            "third_party": [
              "node"
            ]
          },
          "build": {
            "sub_component": [
              "//foundation/napitest:napitest"
            ],
            "inner_kits": [
              {
                "header": {
                  "header_base": "//foundation/napitest",
                  "header_files": [
                    "tool_utility.h",
                    "napitest.h"
                  ]
                },
                "name": "//foundation/napitest:napitest"
              }
            ]
          }
        }
      }
    
    • 为了和bundle.json对应,将BUILD.gn改成如下:
    import("//build/ohos.gni")
    
    ohos_shared_library("napitest")
    {
        sources = [
            "napitest_middle.cpp",
            "napitest.cpp",
            "tool_utility.cpp",
        ]
        include_dirs = [
            ".",
            "//third_party/node/src",
            "//base/hiviewdfx/hilog/interfaces/native/innerkits/include",
        ]
        deps=[
            "//foundation/arkui/napi:ace_napi",
            "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
        ]
        remove_configs = [ "//build/config/compiler:no_rtti" ]
        cflags=[
        ]
        cflags_cc=[
            "-frtti",
        ]
        ldflags = [
        ]
        
        relative_install_dir = "module"
    	//部件名称
        part_name = "napitest_interface"
    	//子系统名称
        subsystem_name = "napitest"
    }
    
  4. 引入子系统

    • 增加子系统,修改build/subsystem_config.json

      //在文件后增加
      {
        //前面省略的内容
      	...
        //新增内容
        "napitest": {
          "path": "foundation/napitest",
          "name": "napitest"
        }
      }
      
    • 增加编译入口(已目前的master版本为基础,3.2后改过编译路径)

      //修改 vendor/hihope/[PRODUCT_NAME]/config.json 文件增加如下行
      {
      	"subsystem": "napitest",
      	"components": [
      	{
      		"component": "napitest_interface",
      		"features": []
      	}
      	]
      },
      
  5. 编译生成

    ./build.sh --product-name PRODUCT_NAME
    //看到success则为编译成功,可以通过find out/[PRODUCT_NAME] -name *napitest.z.so查看生成文件,比如我的文件路径如下:
    ./out/rk3568/lib.unstripped/napitest/napitest_interface/libnapitest.z.so
    ./out/rk3568/napitest/napitest_interface/libnapitest.z.so
    ./out/rk3568/innerkits/ohos-arm/napitest_interface/napitest/libnapitest.z.so
    ./out/rk3568/packages/phone/system/lib/module/libnapitest.z.so
    //最后一个路径就是系统镜像的路径,所以两种办法
    //1,直接copy到板子的/system/lib路径;参考《 [二]第一个OpenHarmony程序》
    //2,烧录镜像;参考《 [一]运行OpenHarmony》
    
    
    

总结

这样我们就有了自己的subsystem和napi接口,后面的章节我们会讲如何在hap应用里调用系统库

参考链接:https://gitee.com/openharmony/napi_generator/tree/master

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
napi_generator-linux.zip 33.08M 18次下载
已于2022-12-20 10:02:44修改
6
收藏 4
回复
举报
5条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

学好NAPI成为底层大佬

已于2022-9-30 10:00:25修改
回复
2022-9-30 09:59:31
离北况归
离北况归

期待在hap里面调用动态库的内容

回复
2022-9-30 17:56:32
左翼风发
左翼风发

这部分会有两种方式,后面会发两篇文章

1
回复
2022-9-30 18:47:42
物联风景
物联风景

不错不错,还算详细

回复
2022-10-4 17:03:56
Haoc_小源同学
Haoc_小源同学

不错,之前都是自己写的,现在都有生成工具了,对开发者越来越友好了

回复
2022-10-8 15:18:47
回复
    相关推荐