English | 简体中文
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.
See the changelog for release notes and per-package changes.
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 modulesplugins/*— standalone ArkTS capability packagesdemo— unified Harmony demo projectrust_example/demo_native— unified native demo implementation
pnpm installThis installs the ohpm-rs CLI from ohpm-cli 0.3.0 and installs ArkTS dependencies
for the workspace packages and demo. Internal ArkTS dependencies use workspace:*
and resolve to the source packages listed in ohpm-workspace.yaml.
Run pnpm run install:ohpm to reinstall ArkTS dependencies after changing a manifest.
The repository commands use Node and Rust tools on Linux and macOS. Native Windows
is currently blocked by ohpm-cli 0.3.0's Unix-only dependency-link implementation;
use WSL with Linux tools and SDK until the CLI supports Windows.
Run npm run pack to create one HAR per workspace package in dist/.
Run npm run format to format Rust and ArkTS sources. Git hooks run formatting and
lint checks through prek; CI runs prek for all files and Rust clippy for each
supported OpenHarmony target.
- 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";
onCreate(
want: Want,
launchParam: AbilityConstant.LaunchParam
): 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
Plugin factories require business-owned protocol configuration:
new LazyPlugin(() => new ClipboardPlugin({ metadataMimeType: "application/x-my-app-metadata-v1" }));
new LazyPlugin(() => new NotificationPlugin({ scheme: "my-app-notification" }));Register the standalone plugin factories in the Ability's bridgePlugins array.
Choose either MIT or Apache-2.0.
