自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

跟著小白一起學(xué)鴻蒙--寫(xiě)個(gè)NAPI子系統(tǒng)(七)

系統(tǒng) OpenHarmony
此篇文章,我們主要是熟悉下NAPI框架,并一起寫(xiě)一個(gè)支持NAPI的子系統(tǒng),這樣以后當(dāng)我們想在hap應(yīng)用里加自己功能的時(shí)候就可以方便的添加。

??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

在《#跟著小白一起學(xué)鴻蒙#[六]第一個(gè)hap應(yīng)用》我們熟悉了如何在開(kāi)源鴻蒙開(kāi)發(fā)hap應(yīng)用,后期的文章我們會(huì)寫(xiě)在hap應(yīng)用里調(diào)用系統(tǒng)庫(kù)甚至是動(dòng)態(tài)庫(kù)。此篇文章,我們主要是熟悉下NAPI框架,并一起寫(xiě)一個(gè)支持NAPI的子系統(tǒng),這樣以后當(dāng)我們想在hap應(yīng)用里加自己功能的時(shí)候就可以方便的添加。

NAPI框架簡(jiǎn)介

NAPI(Native API)組件是一套對(duì)外接口基于Node.js N-API規(guī)范開(kāi)發(fā)的原生模塊擴(kuò)展開(kāi)發(fā)框架。類似于Android的JNI,NAPI框架實(shí)現(xiàn)了應(yīng)用層ts/ets/js語(yǔ)言編寫(xiě)的代碼和開(kāi)源鴻蒙的native代碼(c/c++)交互的能力。此框架由Node.js N-API框架擴(kuò)展而來(lái)。

注意:開(kāi)源鴻蒙的標(biāo)準(zhǔn)系統(tǒng)是采用NAPI框架的,輕量系統(tǒng)則是采用jerryscript框架。

#沖刺創(chuàng)作新星# #跟著小白一起學(xué)鴻蒙# [七] 寫(xiě)個(gè)NAPI子系統(tǒng)-開(kāi)源基礎(chǔ)軟件社區(qū)

詳細(xì)的內(nèi)容介紹在一下鏈接內(nèi)可以看到官方的說(shuō)明:

參考鏈接:https://gitee.com/openharmony/arkui_napi。

NAPI的使用

graph LR
創(chuàng)建d.ts --> 執(zhí)行napi_generator --> 建立子系統(tǒng) --> 引入子系統(tǒng) --> 編譯生成

創(chuàng)建d.ts: @ohos.napitest.d.ts, basic.d.ts。

@ohos.napitest.d.ts是NAPI的聲明文件,在DevEco Studio開(kāi)發(fā)的時(shí)候會(huì)用到d.ts來(lái)檢查語(yǔ)法和提供代碼幫助。

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:一些基礎(chǔ)方法的聲明。

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;
}

執(zhí)行napi_generator建立個(gè)文件夾,將上面建立的兩個(gè)d.ts和napi_generator放在一起。

//準(zhǔn)備環(huán)境
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
//當(dāng)看到success則說(shuō)明燒錄成功

//檢視out目錄
├── binding.gyp //工具中間文件
├── BUILD.gn //之后需要用到的gn文件
├── napi_gen.log //工具log
├── napitest.cpp //自動(dòng)生成的接口調(diào)用的實(shí)際代碼
├── napitest.h //自動(dòng)生成的接口調(diào)用的實(shí)際代碼
├── napitest_middle.cpp //自動(dòng)生成的napi適配代碼
├── test.sh //生成js代碼的腳本,官方?jīng)]給說(shuō)明,試了下不可用
├── tool_utility.cpp //自動(dòng)生成的napi適配代碼
└── tool_utility.h //自動(dòng)生成的napi適配代碼

建立子系統(tǒng)。

在鴻蒙源碼目錄下建立foundation/napitest,將之前生成的文件拷貝到文件夾內(nèi)。

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

在目錄里創(chuàng)建bundle.json,使用一下內(nèi)容。

{
"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",
//子系統(tǒng)名稱
"subsystem": "napitest",
"features": [],
"adapted_system_type": [
"standard"
],
"rom": "10000KB",
"ram": "10000KB",
"deps": {
"components": [
"ace_napi",
"ipc_core",
"libhilog"
],
"third_party": [
"node"
]
},
"build": {
"sub_component": [
"http://foundation/napitest:napitest"
],
"inner_kits": [
{
"header": {
"header_base": "http://foundation/napitest",
"header_files": [
"tool_utility.h",
"napitest.h"
]
},
"name": "http://foundation/napitest:napitest"
}
]
}
}
}

為了和bundle.json對(duì)應(yīng),將BUILD.gn改成如下:

import("http://build/ohos.gni")
ohos_shared_library("napitest")
{
sources = [
"napitest_middle.cpp",
"napitest.cpp",
"tool_utility.cpp",
]
include_dirs = [
".",
"http://third_party/node/src",
"http://base/hiviewdfx/hilog/interfaces/native/innerkits/include",
]
deps=[
"http://foundation/arkui/napi:ace_napi",
"http://base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
]
remove_configs = [ "http://build/config/compiler:no_rtti" ]
cflags=[
]
cflags_cc=[
"-frtti",
]
ldflags = [
]
relative_install_dir = "module"
//部件名稱
part_name = "napitest_interface"
//子系統(tǒng)名稱
subsystem_name = "napitest"
}

引入子系統(tǒng)。

增加子系統(tǒng),修改build/subsystem_config.json。

//在文件后增加
{
//前面省略的內(nèi)容
...
//新增內(nèi)容
"napitest": {
"path": "foundation/napitest",
"name": "napitest"
}
}

增加編譯入口(已目前的master版本為基礎(chǔ),3.2后改過(guò)編譯路徑)。

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

編譯生成。

./build.sh --product-name PRODUCT_NAME
//看到success則為編譯成功,可以通過(guò)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
//最后一個(gè)路徑就是系統(tǒng)鏡像的路徑,所以兩種辦法
//1,直接copy到板子的/system/lib路徑;參考《#跟著小白一起學(xué)鴻蒙# [二]第一個(gè)OpenHarmony程序》
//2,燒錄鏡像;參考《#跟著小白一起學(xué)鴻蒙# [一]運(yùn)行OpenHarmony》

總結(jié)

這樣我們就有了自己的subsystem和napi接口,后面的章節(jié)我們會(huì)講如何在hap應(yīng)用里調(diào)用系統(tǒng)庫(kù)。

參考鏈接:https://gitee.com/openharmony/napi_generator/tree/master。

文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載:

https://ost.51cto.com/resource/2308。

??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??。

責(zé)任編輯:jianghua 來(lái)源: 51CTO開(kāi)源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2022-10-31 15:35:02

Wi-Fi藍(lán)牙子系統(tǒng)

2022-10-17 14:29:24

鴻蒙應(yīng)用開(kāi)發(fā)

2022-11-29 16:35:02

Tetris鴻蒙

2022-12-02 14:20:09

Tetris鴻蒙

2023-03-30 09:32:27

2022-11-14 17:01:34

游戲開(kāi)發(fā)畫(huà)布功能

2023-04-04 09:24:11

鴻蒙HiDumper

2022-10-10 14:47:04

藍(lán)牙應(yīng)用鴻蒙

2023-02-27 16:30:32

鴻蒙開(kāi)源協(xié)議分析

2022-08-19 19:02:20

開(kāi)源鴻蒙操作系統(tǒng)

2023-03-30 09:19:54

SELinux安全子系統(tǒng)

2023-01-03 15:09:10

鴻蒙常用工具

2022-12-06 15:39:16

鴻蒙主干代碼

2022-11-24 14:34:41

Hap程序鴻蒙

2023-03-15 16:19:03

BinderIPC工具

2022-10-20 16:40:16

JS應(yīng)用控制LED鴻蒙

2023-04-06 09:18:52

鴻蒙AVPlayerAVRecorder

2022-12-09 15:34:38

2022-11-28 15:42:39

分布式軟總線鴻蒙

2023-02-24 16:02:45

WebSocket網(wǎng)絡(luò)通訊協(xié)議
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)