HarmonyOS Native module framework for MoonBit: async tasks, platform modules and HAR packaging.
| 层次 | 内容 | 文档 |
|---|---|---|
| 接口生成 | Int/Double/Bool/String/Bytes/Array/结构体的双向转换,d.ts 生成,清单与 .mbti 核对 | 本文“清单” |
| 鸿蒙运行时 | 进程门、工作线程任务、取消、句柄生命周期、env cleanup、诊断计数 | ASYNC-THREADING.md |
| 平台扩展 | moonohos.native.json 受控依赖;hilog、rawfile、task 平台包 | NATIVE-EXTENSIONS.md |
| 工程分发 | init、build、har、verify;SHA256 与 ELF 校验 | 本文“命令” |
moon install shop1111/moonohos/cmd/moonohos@0.4.0 # 或在源码仓库中 moon build --target native --release cmd/moonohos
moonohos init textkit --package alice/textkit
moonohos build --config textkit\moonohos.json --sdk 'D:\apps\DevEco Studio\sdk' --out _build/dist
moonohos har --dist textkit\_build\dist --module textkit\harmony\textkit --assemble --deveco 'D:\apps\DevEco Studio'textkit/
moon.work # 工作区;--ohos 时加入本地 ohos 平台模块
moonohos.json # 桥接清单(version 2)
core/ # MoonBit 库:同步 greet,异步可取消 count_words(使用 HiLog)
harmony/ # Hvigor 工程
textkit/ # HAR 模块:Index.ets 重导出原生模块{"dependencies": {"@alice/textkit": "file:../textkit/harmony/textkit/build/default/outputs/default/textkit.har"}}import textkit, { CancelToken } from '@alice/textkit';
const token: CancelToken = textkit.createCancelToken();
const words: number = await textkit.count_words(longText, token); // 工作线程执行
token.cancel(); // 页面退出时取消| 步骤 | 0.3 | 0.4 |
|---|---|---|
| 新建工程 | 手写 MoonBit 模块、清单、DevEco 工程 | moonohos init |
| 原生库进入应用 | 复制两个 ABI 的 .so 到 entry/libs/<abi>/ | moonohos har 放入 HAR 模块 |
| 类型声明 | 复制类型包到 entry/src/main/cpp/types/,手写本地依赖 | HAR 内置类型包与 ArkTS 门面 |
| 打包分发 | 无 | --assemble 调用 hvigor 生成 .har |
| 其他应用复用 | 重复以上复制 | 一行 file: 依赖 |
| 耗时计算 | 只能同步调用,阻塞 UI | 异步导出 + 取消 |
| 使用系统能力 | 不支持(拒绝所有 Native stub) | HiLog、RawFile、任务取消;受控扩展 |
{
"version": 2,
"module": "core",
"package": "moonohos/search",
"nativeModule": "moonsearch",
"workspace": "moon.work",
"handles": [{"name": "SearchIndex"}],
"types": [{"name": "Hit", "fields": [{"name": "path", "type": "String"}, {"name": "score", "type": "Double"}]}],
"functions": [
{"name": "build_index", "params": [{"name": "resources", "type": "@rawfile.Manager"}, {"name": "dir", "type": "String"}],
"return": "SearchIndex", "async": true, "raises": true},
{"name": "search", "params": [{"name": "index", "type": "SearchIndex"}, {"name": "query", "type": "String"}, {"name": "limit", "type": "Int"}],
"return": "Array[Hit]", "async": true}
]
}| MoonBit | ArkTS | 说明 |
|---|---|---|
| Int / Double / Bool | number / number / boolean | Int 为有限 32 位整数,Double 必须有限 |
| Unit(返回值) | void | |
| String | string | UTF-16 码元原样复制,支持内嵌 NUL 与未配对代理 |
| Bytes | Uint8Array | 只接受普通、未分离 ArrayBuffer 上的视图,复制 |
| Array[T] | Array<T> | 稠密、非 Sendable,复制 |
| 结构体 | interface | 目标包需 pub(all) struct,字段为自有属性,复制 |
| 句柄(handles) | 带 close()/isClosed() 的对象 | MoonBit 值保留在原生侧,不复制;不能嵌套在数组或结构体中 |
| @包.类型(平台适配) | 如 resourceManager.ResourceManager | 仅作参数;由描述文件中的适配器获取与释放 |
| "async": true | (..., token?: CancelToken) => Promise<T> | 工作线程执行 |
| "raises": true | 抛出/拒绝 code = ERR_MOONOHOS_BUSINESS | MoonBit 函数须声明 raise,消息取错误的 Show 文本 |
& $cli init <dir> [--name <module>] [--package owner/name] [--ohos <path>] [--json]
& $cli build --config moonohos.json --sdk <SDK> [--abi arm64-v8a,x86_64] [--out <dir>] [--dry-run] [--cache] [--json]
& $cli har --dist <package> --module <HAR module> [--assemble --deveco <DevEco Studio>] [--json]
& $cli doctor --sdk <SDK> --json
& $cli check | inspect | plan --config moonohos.json --json
& $cli verify --out <package> --jsondist/
libs/{arm64-v8a,x86_64}/lib<module>.so
types/lib<module>/{index.d.ts,oh-package.json5}
cmake/MoonOHOS.cmake
manifest.json # moonohos-build-v2:文件 SHA256、导出(async/raises)、句柄、Native 依赖
NOTICE.NativeSDK.txt NOTICE.MoonBit.txt README.md.\scripts\build_search.ps1 # CLI 构建 → HAR → 生成 2000 篇合成文档 → HAP
.\scripts\verify_search_device.ps1 # 模拟器上运行脚本化场景两次moon fmt; moon check --target native --deny-warn; moon test --target native --deny-warn; moon info --target native
python scripts\verify_containers.py async_core async_bridge.cpp
python scripts\verify_cli.py --sdk 'D:\apps\DevEco Studio\sdk'
python scripts\verify_integration.py 'D:\apps\DevEco Studio\sdk'
.\scripts\verify_init_flow.ps1
.\scripts\verify_device.ps1 -Hdc 'D:\apps\DevEco Studio\sdk\default\openharmony\toolchains\hdc.exe'#deprecated("`ConfigError::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn ConfigError::to_repr(ConfigError) -> Reprpub struct AdapterDef {
pkg_alias : String
type_name : String
pkg : String
ark_type : String
ark_import : String
acquire : String
release : String
}pub struct AdapterSpec {
type_name : String
ark_type : String
ark_import : String
acquire : String
release : String
}pub struct ContentFingerprint {
domain : String
name : String
digest : String
}pub struct ConversionLimits {
depth : Int
nodes : Int
bytes : Int
}pub struct ElfInfo {
abi : String
dependencies : Array[String]
imports : Array[String]
exports : Array[String]
soname : String?
segments : Array[ElfSegment]
}pub struct ElfSegment {
offset : Int
file_size : Int
memory_size : UInt64
address : UInt64
flags : Int
alignment : UInt64
}pub struct EnvironmentFact {
name : String
actual : String
expected : String
available : Bool
}pub struct GeneratedArtifacts {
wrapper : String
package_file : String
header : String
embed : String
native : String
declarations : String
cmake : String
}pub(all) struct InitOptions {
name : String
package_name : String
ohos : String?
}pub struct InterfaceType {
name : String
generic : Bool
}pub struct PackageFile {
path : String
size : Int
checksum : String
}pub struct PackageInterface {
functions : Array[InterfaceFunction]
records : Array[InterfaceRecord]
types : Array[InterfaceType]
}pub struct PackageManifest {
schema : Int
module_name : String
version : String
abis : Array[String]
files : Array[PackageFile]
libraries : Array[String]
}fn environment_fact(name : String, actual : String, expected : String, available : Bool) -> EnvironmentFactfn forbidden_build_hook(package_file : String) -> String?fn har_facade(module_name : String, declarations : String) -> Stringfn moonohos_version() -> Stringfn parse_native_descriptor(text : String, pkg : String, stubs : Array[String]) -> NativeDescriptor raise ConfigErrorfn relative_path(from : String, to : String) -> Stringfn resolve_adapters(project : Project, descriptors : Array[NativeDescriptor], imports : Map[String, String]) -> Array[AdapterDef] raise ConfigErrorfn staged_workspace(count : Int) -> Stringfn verify_library(info : ElfInfo, manifest : PackageManifest, abi : String) -> Unit raise ConfigErrorInstall
Download zipHarmonyOS Native module framework for MoonBit: async tasks, platform modules and HAR packaging.