開發(fā)流程
在DevEco Studio的模板工程中包含使用Native API的默認(rèn)工程,使用File->New->Create Project創(chuàng)建Native C++模板工程。
在此基礎(chǔ)上進行修改
刪除
entry/src/main/cpp
打開
entry/build-profile.json5
刪除c++ build 配置
{
"apiType": "stageMode",
"buildOption": {
// "externalNativeOptions": {
// "path": "./src/main/rust/CMakeLists.txt",
// "arguments": "",
// "cppFlags": "",
// }
},
"targets": [
{
"name": "default",
"runtimeOS": "HarmonyOS"
},
{
"name": "ohosTest",
}
]
}
創(chuàng)建rust項目
cargo new hello
修改 Cargo.toml
[package]
name = "hello"
version = "0.1.0"
edition = "2021"
[lib]
name = "hello"
crate-type = ["dylib"]
[dependencies]
oh-napi-sys = "0.1"
ctor = "0.1"
lib.rs 添加測試代碼
use std::ffi::{CString};
use std::ptr::{null_mut};
use oh_napi_sys::*;
use ctor::ctor;
extern "C" fn add(env: napi_env, info: napi_callback_info) ->napi_value{
// 期望從ArkTS側(cè)獲取的參數(shù)的數(shù)量,napi_value可理解為ArkTS value在Native方法中的表現(xiàn)形式。
let mut args:[napi_value; 2] = [null_mut();2];
let mut argc = args.len();
unsafe {
napi_get_cb_info(env, info, &mut argc, args.as_mut_ptr(), null_mut(), null_mut());
let mut valuetype0 = napi_valuetype_napi_undefined;
napi_typeof(env, args[0], &mut valuetype0);
let mut valuetype1 = napi_valuetype_napi_undefined;
napi_typeof(env, args[1], &mut valuetype1);
if (valuetype0 != napi_valuetype_napi_number) || (valuetype1 != napi_valuetype_napi_number) {
let mut undefined: napi_value= null_mut();
napi_get_undefined(env, &mut undefined);
return undefined;
}
// 將獲取的ArkTS參數(shù)轉(zhuǎn)換為Native信息,此處ArkTS側(cè)傳入了兩個number,這里將其轉(zhuǎn)換為Native側(cè)可以操作的double類型。
let mut value0 = 0f64;
napi_get_value_double(env, args[0], &mut value0);
let mut value1 = 0f64;
napi_get_value_double(env, args[1], &mut value1);
// Native側(cè)的業(yè)務(wù)邏輯,這里簡單以兩數(shù)相加為例。
let native_sum = value0 + value1;
// 此處將Native側(cè)業(yè)務(wù)邏輯處理結(jié)果轉(zhuǎn)換為ArkTS值,并返回給ArkTS。
let mut sum = null_mut();
napi_create_double(env, native_sum, &mut sum);
return sum;
}
}
type Callback = extern "C" fn(env: napi_env, info: napi_callback_info) -> napi_value;
unsafe fn new_func_descriptor(name: &'static str, f: Callback) ->napi_property_descriptor{
let name= CString::new(name).unwrap();
napi_property_descriptor{
utf8name: CString::into_raw(name),
name: null_mut(),
method: Some(f),
getter: None,
setter: None,
value: null_mut(),
attributes: napi_property_attributes_napi_default,
data: null_mut(),
}
}
// 注冊 module
#[ctor]
fn register_hello_module(){
let name= CString::new( "hello").unwrap();
let mut hello_module = napi_module{
nm_version: 1,
nm_flags: 0,
nm_filename: null_mut(),
nm_register_func: Some(init),
nm_modname: name.as_ptr() as _,
nm_priv: 0 as * mut _,
reserved: [0 as * mut _;4],
};
unsafe {
napi_module_register(&mut hello_module);
}
// Init將在exports上掛上Add/NativeCallArkTS這些Native方法,此處的exports就是開發(fā)者import之后獲取到的ArkTS對象。
unsafe extern "C" fn init(env: napi_env , exports: napi_value )-> napi_value {
let desc =[
new_func_descriptor("add", add),
];
let count = desc.len();
napi_define_properties(env, exports, count, desc.as_ptr());
return exports;
}
}
添加對應(yīng)ts代碼
// entry/src/main/rust/types/libhello/index.d.ts
export const add: (a: number, b: number) => number;
// entry/src/main/rust/types/libhello/oh-package.json5
{
"name": "libhello.so",
"types": "./index.d.ts",
"version": "",
"description": "Please describe the basic information."
}
配置依賴
// entry/oh-package.json5
{
"name": "entry",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
"libhello.so": "file:./src/main/rust/types/libhello"
}
}
在 rust 根目錄下編譯,添加ohos目標(biāo)的教程可以參考 Rust交叉編譯OpenHarmony應(yīng)用動態(tài)庫
cargo build -Zbuild-std --release --target aarch64-unknown-linux-ohos
將編譯好的 libhello.so
拷貝至
entry/libs/arm64-v8a
修改頁面 Index.ets
文章來源:http://www.zghlxwxcb.cn/news/detail-768236.html
import testNapi from 'libhello.so'
// onClick 里添加
hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
編譯app點擊hello world
,日志中看到 Test NAPI 2 + 3 = 5
文章來源地址http://www.zghlxwxcb.cn/news/detail-768236.html
到了這里,關(guān)于用Rust開發(fā)鴻蒙應(yīng)用(ArkTS NAPI)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!