This project is in progress, and the API is not stable.
openharmony-ability provides native integration helpers for OpenHarmony applications: Rust-side lifecycle management plus ArkTS-side entry helpers that can be shared by Rust and C/SDL native modules.
OpenHarmony applications are driven by callbacks, so there are two important constraints:
- Do not block the main thread.
run_loopdoes not retain user resources for you, so resources that must outlive setup need stable ownership.
crates/ability— Rust lifecycle/runtime supportcrates/derive—#[ability]macronative_ability— ArkTS package source shared by Rust and C/SDL native modulespackage— packaged ohpm artifact sourcedemo— unified Harmony demo projectrust_example/demo_native— unified native demo implementation
- Add Rust dependencies:
cargo add openharmony-ability
cargo add openharmony-ability-derive
cargo add napi-ohos
cargo add napi-derive-ohos
cargo add napi-build-ohos- Implement your native entry (Rust example):
use ohos_hilog_binding::hilog_info;
use openharmony_ability::OpenHarmonyApp;
use openharmony_ability_derive::ability;
#[ability]
fn openharmony_app(app: OpenHarmonyApp) {
app.run_loop(|event| {
hilog_info!(format!("event: {:?}", event.as_str()).as_str());
});
}- Use
NativeAbilityin ArkTS:
import { NativeAbility } from "@ohos-rs/ability";
import Want from "@ohos.app.ability.Want";
import { AbilityConstant } from "@kit.AbilityKit";
export default class EntryAbility extends NativeAbility {
public moduleName: string = "demo_native";
async onCreate(
want: Want,
launchParam: AbilityConstant.LaunchParam
): Promise<void> {
super.onCreate(want, launchParam);
}
}- Build the native module:
cd rust_example/demo_native
ohrs build --arch arm64- Harmony demo project:
demo - Native demo module (Rust example):
rust_example/demo_native/src/lib.rs - ArkTS package source:
native_ability
