Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> This project is in progress, and the API is not stable.

`openharmony-ability` provides Rust-side lifecycle management and ArkTS-side entry helpers for OpenHarmony applications, similar in spirit to `android-activity`.
`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.

## Architecture

Expand All @@ -17,7 +17,7 @@ OpenHarmony applications are driven by callbacks, so there are two important con

- `crates/ability` — Rust lifecycle/runtime support
- `crates/derive` — `#[ability]` macro
- `ability_rust` — ArkTS package source for `@ohos-rs/ability`
- `native_ability` — ArkTS package source shared by Rust and C/SDL native modules
- `package` — packaged ohpm artifact source
- `demo` — unified Harmony demo project
- `rust_example/demo_native` — unified native demo implementation
Expand All @@ -34,7 +34,7 @@ cargo add napi-derive-ohos
cargo add napi-build-ohos
```

2. Implement your native entry:
2. Implement your native entry (Rust example):

```rust
use ohos_hilog_binding::hilog_info;
Expand All @@ -49,14 +49,14 @@ fn openharmony_app(app: OpenHarmonyApp) {
}
```

3. Use `RustAbility` in ArkTS:
3. Use `NativeAbility` in ArkTS:

```ts
import { RustAbility } from "@ohos-rs/ability";
import { NativeAbility } from "@ohos-rs/ability";
import Want from "@ohos.app.ability.Want";
import { AbilityConstant } from "@kit.AbilityKit";

export default class EntryAbility extends RustAbility {
export default class EntryAbility extends NativeAbility {
public moduleName: string = "demo_native";

async onCreate(
Expand All @@ -78,8 +78,8 @@ ohrs build --arch arm64
## Demo

- Harmony demo project: `demo`
- Native demo module: `rust_example/demo_native/src/lib.rs`
- ArkTS package source: `ability_rust`
- Native demo module (Rust example): `rust_example/demo_native/src/lib.rs`
- ArkTS package source: `native_ability`

## License

Expand Down
4 changes: 2 additions & 2 deletions crates/ability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## Introduce

openharmony-ability is a crate for the OpenHarmony/HarmonyNext ability. It provides a way to create an OpenHarmony/HarmonyNext application with rust.
openharmony-ability is the Rust runtime crate in this repository. It provides lifecycle and runtime helpers for OpenHarmony/HarmonyNext native applications.

## Runtime Context

`RustAbility` passes the ArkTS init context into Rust during `init(context)`. In Rust, `OpenHarmonyApp` can read `moduleName`, `basePath`, `prefPath`, and `preferredLocales` via `init_context()`, `module_name()`, `base_path()`, `pref_path()`, and `preferred_locales()`.
`NativeAbility` passes the ArkTS init context into native code during `init(context)`. In the Rust runtime, `OpenHarmonyApp` can read `moduleName`, `basePath`, `prefPath`, and `preferredLocales` via `init_context()`, `module_name()`, `base_path()`, `pref_path()`, and `preferred_locales()`.

## License

Expand Down
2 changes: 1 addition & 1 deletion crates/derive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn openharmony_app(app: OpenHarmonyApp) {

### Init Context

The generated `init(context)` now forwards ArkTS init data into Rust. Inside your `OpenHarmonyApp`, you can read it through `app.init_context()`, `app.module_name()`, `app.base_path()`, `app.pref_path()`, and `app.preferred_locales()`.
The generated `init(context)` forwards ArkTS init data into native code. In the Rust runtime, you can read it through `app.init_context()`, `app.module_name()`, `app.base_path()`, `app.pref_path()`, and `app.preferred_locales()`.

### webview

Expand Down
4 changes: 2 additions & 2 deletions demo/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
]
},
{
"name": "ability_rust",
"srcPath": "../ability_rust"
"name": "native_ability",
"srcPath": "../native_ability"
}
]
}
12 changes: 6 additions & 6 deletions demo/entry/oh-package-lock.json5

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/entry/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"libentry.so": "file:./src/main/cpp/types/libentry",
"libdemo_native.so": "file:./src/main/cpp/types/libdemo_native",
"libark_web.so": "file:./src/main/cpp/types/libark_web",
"@ohos-rs/ability": "file:../../ability_rust"
"@ohos-rs/ability": "file:../../native_ability"
}
}
4 changes: 2 additions & 2 deletions demo/entry/src/main/ets/entryability/EntryAbility.ets
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RustAbility } from "@ohos-rs/ability";
import { NativeAbility } from "@ohos-rs/ability";
import Want from "@ohos.app.ability.Want";
import { AbilityConstant } from "@kit.AbilityKit";
import window from "@ohos.window";

export default class EntryAbility extends RustAbility {
export default class EntryAbility extends NativeAbility {
public moduleName: string = "demo_native";
public defaultPage: boolean = false;

Expand Down
2 changes: 1 addition & 1 deletion demo/entry/src/main/resources/zh_CN/element/string.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"string": [
{
"name": "module_desc",
"value": "Rust Ability Demo 入口"
"value": "Native Ability Demo 入口"
},
{
"name": "EntryAbility_desc",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 7 additions & 7 deletions ability_rust/README.md → native_ability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> This project is in progress, and the API is not stable.

`openharmony-ability` is a crate to manage OpenHarmony application's activity with rust, be similar to [android-activity](https://github.com/rust-mobile/android-activity).
`openharmony-ability` is the native integration layer for OpenHarmony applications. It combines Rust-side runtime crates with ArkTS-side entry helpers such as `NativeAbility`, similar in spirit to [android-activity](https://github.com/rust-mobile/android-activity).

## Architecture

Expand All @@ -13,16 +13,16 @@ The architecture of OpenHarmony is similar to Node.js, where we need to manage t

![Architecture](/fixtures/openharmony-ability.png)

We provide some packages or crates to help you develop OpenHarmony application with Rust.
We provide packages and crates to help you build OpenHarmony applications with native code, including Rust integrations and shared ArkTS entry helpers for C/SDL-style modules.

### ArkTS

We need a entry-point to start the application, and we use ArkTS to manage the application's lifecycle.

- [@ohos-rs/ability](../package/README.md)
All of ability need to extend `RustAbility` and all lifecycle need to call `super.xx` to make sure the ability can work normally.
All of ability need to extend `NativeAbility` and all lifecycle need to call `super.xx` to make sure the ability can work normally.

### Rust
### Rust Runtime

- [openharmony-ability](../crates/ability/README.md)
Basic crate to manage the application's lifecycle.
Expand Down Expand Up @@ -67,11 +67,11 @@ We need a entry-point to start the application, and we use ArkTS to manage the a
4. change the `EntryAbility.ets` file to the follow code:

```ts
import { RustAbility } from "@ohos-rs/ability";
import { NativeAbility } from "@ohos-rs/ability";
import Want from "@ohos.app.ability.Want";
import { AbilityConstant } from "@kit.AbilityKit";

export default class EntryAbility extends RustAbility {
export default class EntryAbility extends NativeAbility {
public moduleName: string = "example";

async onCreate(
Expand All @@ -86,7 +86,7 @@ We need a entry-point to start the application, and we use ArkTS to manage the a

5. Set `moduleName` to the bare module name, for example `hello`. The framework will load `libhello.so` internally. You can also pass `string[]` when one ability needs to initialize multiple native modules.

6. Build your rust project and copy the dynamic library to (Open-)Harmony(Next) project.
6. Build your native project and copy the dynamic library to your (Open-)Harmony(Next) project. The example below uses Rust, but the ArkTS `NativeAbility` entry is also reusable for C/SDL-style native modules that expose the same contract.

7. Now, you can enjoy it.

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion ability_rust/index.ets → native_ability/index.ets
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { RustAbility } from "./src/main/ets/ability/RustAbility";
export { NativeAbility } from "./src/main/ets/ability/NativeAbility";
export { DefaultXComponent } from "./src/main/ets/components/DefaultXComponent";
export {
RustWebviewNodeController,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@ohos-rs/ability",
"version": "0.0.1",
"description": "Adaptor for OpenHarmony/HarmonyNext Application with Rust",
"description": "Adaptor for OpenHarmony/HarmonyNext native abilities",
"main": "index.ets",
"author": "richerfu",
"license": "Apache-2.0",
"dependencies": {
"libability_rust.so": "file:./src/main/cpp/types/libability_rust"
"libnative_ability.so": "file:./src/main/cpp/types/libnative_ability"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ endif()
include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include)

add_library(ability_rust SHARED napi_init.cpp)
target_link_libraries(ability_rust PUBLIC libace_napi.z.so)
add_library(native_ability SHARED napi_init.cpp)
target_link_libraries(native_ability PUBLIC libace_napi.z.so)
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ static napi_module demoModule = {
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "ability_rust",
.nm_modname = "native_ability",
.nm_priv = ((void*)0),
.reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterAbility_rustModule(void)
extern "C" __attribute__((constructor)) void RegisterNativeAbilityModule(void)
{
napi_module_register(&demoModule);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "libability_rust.so",
"name": "libnative_ability.so",
"types": "./Index.d.ts",
"version": "1.0.0",
"description": "Please describe the basic information."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Loadable } from "../helper/loadable";

export const STATE_KEY = "ohos.rs.ability.application.state";

export class RustAbility extends UIAbility {
export class NativeAbility extends UIAbility {
public moduleName: string | string[] = "";
public defaultPage: boolean = true;
public mode: "xcomponent" | "webview" = "xcomponent";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
WebviewInitData,
} from "../webview/DefaultWebview";

export const RouteName = "RustAbility";
export const RouteName = "NativeAbility";
@Component
export struct DefaultXComponent {
moduleName: string = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DefaultXComponent } from "./DefaultXComponent";
import { Loadable } from "../helper/loadable";

export const RouteName = "RustAbility";
export const RouteName = "NativeAbility";

@Entry({ routeName: RouteName })
@Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"module": {
"name": "ability_rust",
"name": "native_ability",
"type": "har",
"deviceTypes": [
"default",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"module": {
"name": "ability_rust_test",
"name": "native_ability_test",
"type": "feature",
"deviceTypes": [
"default",
Expand Down
16 changes: 8 additions & 8 deletions package/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @ohos-rs/ability

`@ohos-rs/ability` provides ArkTS-side helpers for loading Rust native modules and forwarding OpenHarmony lifecycle events into Rust.
`@ohos-rs/ability` provides ArkTS-side helpers for loading native modules and forwarding OpenHarmony lifecycle events into native code. The same `NativeAbility` entry can be reused by Rust modules and C/SDL-style native modules.

## Install

Expand All @@ -10,14 +10,14 @@ ohpm install @ohos-rs/ability

## API

### `RustAbility`
### `NativeAbility`

`RustAbility` wraps `UIAbility` and initializes one or more Rust native modules.
`NativeAbility` wraps `UIAbility` and initializes one or more native modules.

```ts
import { RustAbility } from "@ohos-rs/ability";
import { NativeAbility } from "@ohos-rs/ability";

export default class EntryAbility extends RustAbility {
export default class EntryAbility extends NativeAbility {
public moduleName: string = "demo_native";

onCreate() {
Expand All @@ -43,7 +43,7 @@ When using `sync`, add the corresponding library to `build-profile.json5` runtim

### `DefaultXComponent`

`DefaultXComponent` loads the native module and binds the default Rust rendering surface.
`DefaultXComponent` loads the native module and binds the default native rendering surface.

```ts
import { DefaultXComponent } from "@ohos-rs/ability";
Expand All @@ -66,12 +66,12 @@ struct Index {
### Custom Page Example

```ts
import { RustAbility } from "@ohos-rs/ability";
import { NativeAbility } from "@ohos-rs/ability";
import Want from "@ohos.app.ability.Want";
import { AbilityConstant } from "@kit.AbilityKit";
import window from "@ohos.window";

export default class EntryAbility extends RustAbility {
export default class EntryAbility extends NativeAbility {
public moduleName: string = "demo_native";
public defaultPage: boolean = false;

Expand Down
2 changes: 1 addition & 1 deletion package/index.ets
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { RustAbility } from "./src/main/ets/ability/RustAbility";
export { NativeAbility } from "./src/main/ets/ability/NativeAbility";
export { DefaultXComponent } from "./src/main/ets/components/DefaultXComponent";
export * from "./src/main/ets/webview/DefaultWebview";
2 changes: 1 addition & 1 deletion package/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"license": "MIT",
"author": "richerfu",
"name": "@ohos-rs/ability",
"description": "Adaptor for OpenHarmony/HarmonyNext Application with Rust",
"description": "Adaptor for OpenHarmony/HarmonyNext native abilities",
"main": "index.ets",
"version": "0.4.0-beta.4",
"repository": "https://github.com/harmony-contrib/openharmony-ability.git",
Expand Down
2 changes: 1 addition & 1 deletion scripts/pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
rm -rf $SCRIPT_DIR/../package/src/main/ets
rm -rf $SCRIPT_DIR/../dist

cp -rf $SCRIPT_DIR/../ability_rust/src/main/ets/ $SCRIPT_DIR/../package/src/main/ets
cp -rf $SCRIPT_DIR/../native_ability/src/main/ets/ $SCRIPT_DIR/../package/src/main/ets

pushd $SCRIPT_DIR/../ && ohrs artifact --skip-libs