@ohos-cpf/3rdloop 0.0.11 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cli.js +2 -2
- package/package.json +1 -1
- package/vendor/Server/Routes/controllers/LoopEngineController.js +36 -3
- package/vendor/Server/Routes/controllers/OrchestratorController.js +193 -9
- package/vendor/Server/Skills/flutter-build-test/SKILL.md +252 -0
- package/vendor/Server/Skills/flutter-build-test/assets/BUILDENV_TEMPLATE.md +42 -0
- package/vendor/Server/Skills/flutter-build-test/assets/BUILD_REPORT_TEMPLATE.md +64 -0
- package/vendor/Server/Skills/flutter-build-test/assets/README_SECTION_TEMPLATE.md +78 -0
- package/vendor/Server/Skills/flutter-build-test/references/BUILD_TROUBLESHOOTING.md +128 -0
- package/vendor/Server/Skills/flutter-build-test/references/DOC_UPDATE_GUIDE.md +119 -0
- package/vendor/Server/Skills/flutter-build-test/references/FLVM_GUIDE.md +89 -0
- package/vendor/Server/Skills/flutter-build-test/scripts/build-matrix.cjs +374 -0
- package/vendor/Server/Skills/flutter-build-test/scripts/locate-example.cjs +189 -0
- package/vendor/Server/Skills/flutter-build-test/scripts/update-buildenv.cjs +171 -0
- package/vendor/Server/Skills/flutter-code-use/SKILL.md +316 -0
- package/vendor/Server/Skills/flutter-code-use/references/event-channel.md +440 -0
- package/vendor/Server/Skills/flutter-code-use/references/federated.md +295 -0
- package/vendor/Server/Skills/flutter-code-use/references/ffi-binding-translate.md +130 -0
- package/vendor/Server/Skills/flutter-code-use/references/ffi-compile-from-source.md +169 -0
- package/vendor/Server/Skills/flutter-code-use/references/ffi-fetch-at-build.md +161 -0
- package/vendor/Server/Skills/flutter-code-use/references/ffi-prebuilt-bundle.md +175 -0
- package/vendor/Server/Skills/flutter-code-use/references/ffi-rhttp-guide.md +235 -0
- package/vendor/Server/Skills/flutter-code-use/references/ffi-rust-cross-compile.md +514 -0
- package/vendor/Server/Skills/flutter-code-use/references/ffi.md +220 -0
- package/vendor/Server/Skills/flutter-code-use/references/method-channel.md +643 -0
- package/vendor/Server/Skills/flutter-code-use/references/monorepo.md +188 -0
- package/vendor/Server/Skills/flutter-code-use/references/ohos-api-pitfalls.md +717 -0
- package/vendor/Server/Skills/flutter-code-use/references/platform-view.md +448 -0
- package/vendor/Server/Skills/flutter-code-use/references/pure-dart.md +180 -0
- package/vendor/Server/Skills/flutter-code-use/references/texture.md +459 -0
- package/vendor/Server/Skills/flutter-demo-code-generator/SKILL.md +270 -0
- package/vendor/Server/Skills/flutter-demo-code-generator/assets/PAGE_TEMPLATES.md +544 -0
- package/vendor/Server/Skills/flutter-demo-code-generator/references/CODE_STANDARDS.md +328 -0
- package/vendor/Server/Skills/flutter-demo-code-generator/references/DEMO_DOC_PARSING.md +126 -0
- package/vendor/Server/Skills/flutter-demo-code-generator/references/EXAMPLES.md +629 -0
- package/vendor/Server/Skills/flutter-demo-code-generator/scripts/validate-flutter-demo.cjs +268 -0
- package/vendor/Server/Skills/flutter-demo-doc-generator/SKILL.md +227 -0
- package/vendor/Server/Skills/flutter-demo-doc-generator/assets/DEMO_DOC_TEMPLATE.md +78 -0
- package/vendor/Server/Skills/flutter-demo-doc-generator/references/COVERAGE_REPORT_PARSING.md +174 -0
- package/vendor/Server/Skills/flutter-demo-doc-generator/references/EXAMPLES.md +162 -0
- package/vendor/Server/Skills/flutter-demo-doc-generator/references/MCP_TOOL_GUIDE.md +123 -0
- package/vendor/Server/Skills/flutter-demo-doc-generator/references/OUTPUT_FORMAT.md +190 -0
- package/vendor/Server/Skills/flutter-demo-doc-generator/references/QUALITY_CHECKLIST.md +83 -0
- package/vendor/Server/Skills/flutter-demo-doc-generator/scripts/validate-skill.cjs +259 -0
- package/vendor/Server/Skills/flutter-library-demo-coverage/SKILL.md +175 -0
- package/vendor/VERSION +3 -3
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
# FFI Strategy: rust_cross_compile
|
|
2
|
+
|
|
3
|
+
## 适用条件
|
|
4
|
+
|
|
5
|
+
仓库内有 `Cargo.toml`(通常在 `rust/` 或根目录),Dart 侧通过 `dart:ffi` 调用 Rust 编译出的 `.so`。常见配套工具:`flutter_rust_bridge`、`cargokit`、自定义 build script。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 第一部分:工程配置
|
|
10
|
+
|
|
11
|
+
### 1. 创建 ohos 工程
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
flutter create -t plugin_ffi --platforms ohos .
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### 2. Rust OHOS 交叉编译环境
|
|
18
|
+
|
|
19
|
+
OpenHarmony 是 Rust **Tier 2** 官方支持目标,rustup 直接提供预编译标准库。
|
|
20
|
+
|
|
21
|
+
#### 2.1 安装编译目标
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
rustup target add aarch64-unknown-linux-ohos # arm64 真机(必须)
|
|
25
|
+
rustup target add x86_64-unknown-linux-ohos # 模拟器(可选)
|
|
26
|
+
rustup target add armv7-unknown-linux-ohos # 32位设备(可选)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
#### 2.2 获取 OpenHarmony NDK
|
|
30
|
+
|
|
31
|
+
通过 DevEco Studio 5.0+ 安装 SDK,设置环境变量:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
# macOS
|
|
35
|
+
export OHOS_NDK_HOME=/Applications/DevEco-Studio.app/Contents/sdk/default/openharmony
|
|
36
|
+
|
|
37
|
+
# Linux / Windows(按实际路径调整)
|
|
38
|
+
export OHOS_NDK_HOME=/path/to/sdk/default/openharmony
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> 参考:https://ohos.rs/en/docs/basic/quick-start
|
|
42
|
+
|
|
43
|
+
#### 2.3 创建 Clang Wrapper 脚本
|
|
44
|
+
|
|
45
|
+
鸿蒙使用 **musl libc**(非 glibc),必须通过 wrapper 脚本传递 `--sysroot` 和 `-D__MUSL__` 标志。需要为每个目标架构创建 C 和 C++ 两个 wrapper。
|
|
46
|
+
|
|
47
|
+
**aarch64 C 编译器 wrapper**(`aarch64-unknown-linux-ohos-clang.sh`):
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
#!/bin/bash
|
|
51
|
+
exec "$OHOS_NDK_HOME/native/llvm/bin/clang" \
|
|
52
|
+
--target=aarch64-linux-ohos \
|
|
53
|
+
--sysroot="$OHOS_NDK_HOME/native/sysroot" \
|
|
54
|
+
-D__MUSL__ \
|
|
55
|
+
"$@"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**aarch64 C++ 编译器 wrapper**(`aarch64-unknown-linux-ohos-clang++.sh`):
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
#!/bin/bash
|
|
62
|
+
exec "$OHOS_NDK_HOME/native/llvm/bin/clang++" \
|
|
63
|
+
--target=aarch64-linux-ohos \
|
|
64
|
+
--sysroot="$OHOS_NDK_HOME/native/sysroot" \
|
|
65
|
+
-D__MUSL__ \
|
|
66
|
+
"$@"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
x86_64 和 armv7 同理,替换 `--target` 为对应值。armv7 额外需要 `-march=armv7-a -mfloat-abi=softfp -mtune=generic-armv7-a -mthumb`。
|
|
70
|
+
|
|
71
|
+
> 完整模板:https://doc.rust-lang.org/rustc/platform-support/openharmony.html
|
|
72
|
+
|
|
73
|
+
#### 2.4 配置 `.cargo/config.toml`
|
|
74
|
+
|
|
75
|
+
```toml
|
|
76
|
+
[target.aarch64-unknown-linux-ohos]
|
|
77
|
+
linker = "/path/to/aarch64-unknown-linux-ohos-clang.sh"
|
|
78
|
+
ar = "${OHOS_NDK_HOME}/native/llvm/bin/llvm-ar"
|
|
79
|
+
|
|
80
|
+
[target.x86_64-unknown-linux-ohos]
|
|
81
|
+
linker = "/path/to/x86_64-unknown-linux-ohos-clang.sh"
|
|
82
|
+
ar = "${OHOS_NDK_HOME}/native/llvm/bin/llvm-ar"
|
|
83
|
+
|
|
84
|
+
[target.armv7-unknown-linux-ohos]
|
|
85
|
+
linker = "/path/to/armv7-unknown-linux-ohos-clang.sh"
|
|
86
|
+
ar = "${OHOS_NDK_HOME}/native/llvm/bin/llvm-ar"
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### 2.5 验证编译环境
|
|
90
|
+
|
|
91
|
+
在引入完整插件之前,先单独验证工具链:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
# 创建最简项目
|
|
95
|
+
cargo new hello_ohos --lib
|
|
96
|
+
cd hello_ohos
|
|
97
|
+
|
|
98
|
+
# 尝试交叉编译
|
|
99
|
+
cargo build --target aarch64-unknown-linux-ohos --release
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
如果编译通过,说明工具链配置正确。
|
|
103
|
+
|
|
104
|
+
### 3. TLS / 加密后端决策(重要)
|
|
105
|
+
|
|
106
|
+
Rust HTTP/网络相关 crate(如 `reqwest`、`hyper`、`ureq` 等)需要 TLS 后端。OHOS 交叉编译时 TLS 后端选择是**首要决策**:
|
|
107
|
+
|
|
108
|
+
#### 决策树
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
Cargo.toml 中使用 TLS 的 crate?
|
|
112
|
+
├── 否 → 跳过本节
|
|
113
|
+
└── 是 → 检查 TLS 后端 feature
|
|
114
|
+
├── 已显式指定 rustls-tls → ✅ 无需改动,直接可编译
|
|
115
|
+
├── 已显式指定 native-tls / openssl → ⚠️ 需要处理(见下方)
|
|
116
|
+
└── 使用 default-features → 检查默认值
|
|
117
|
+
├── 默认为 rustls → ✅ 无需改动
|
|
118
|
+
└── 默认为 native-tls → ⚠️ 需要改为 rustls 或配置 OpenSSL
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### 方案 A:使用 rustls(推荐,但有已知运行时风险)
|
|
122
|
+
|
|
123
|
+
`rustls` 是纯 Rust 实现的 TLS,无系统依赖,交叉编译零配置:
|
|
124
|
+
|
|
125
|
+
```toml
|
|
126
|
+
# Cargo.toml — 以 reqwest 为例
|
|
127
|
+
[dependencies]
|
|
128
|
+
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] }
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
适用于绝大多数场景。默认使用 `webpki-roots`(Mozilla 根证书列表),跨平台通用。
|
|
132
|
+
|
|
133
|
+
> **⚠️ 已知风险:ring 运行时 TLS 握手失败**
|
|
134
|
+
>
|
|
135
|
+
> `rustls` 默认使用 `ring` 作为加密后端。`ring` 在 `aarch64-unknown-linux-ohos` 上**可以编译通过**,但在真机运行时 HTTPS 请求可能**无限挂起**(TCP/HTTP 正常,TLS 握手无响应)。
|
|
136
|
+
>
|
|
137
|
+
> 根因尚未完全确认,推测与 `ring` 的 aarch64 汇编在 OHOS musl 环境下的运行时行为有关。
|
|
138
|
+
>
|
|
139
|
+
> **缓解方案**(按优先级尝试):
|
|
140
|
+
>
|
|
141
|
+
> 1. **升级 ring 版本**:尝试使用较新版本的 `ring`(0.17.14+),可能已修复
|
|
142
|
+
> 2. **切换到 aws-lc-rs**:reqwest 0.12 支持 `__rustls-aws-lc-rs` 内部 feature,启用后用 `aws-lc-rs` 替代 `ring`
|
|
143
|
+
> ```toml
|
|
144
|
+
> reqwest = { version = "0.12", default-features = false, features = [
|
|
145
|
+
> "rustls-tls-webpki-roots", # 或 rustls-tls
|
|
146
|
+
> "__rustls-aws-lc-rs", # 内部 feature,切换加密后端
|
|
147
|
+
> ] }
|
|
148
|
+
> ```
|
|
149
|
+
> 3. **回退到 native-tls + ohos-openssl**:如果上述方案均不生效,使用方案 B
|
|
150
|
+
>
|
|
151
|
+
> **验证方法**:编译完成后,在真机上分别测试 HTTP 和 HTTPS 请求。如果 HTTP 返回 200 但 HTTPS 挂起,即可确认为此问题。
|
|
152
|
+
|
|
153
|
+
#### 方案 B:必须使用 OpenSSL 时
|
|
154
|
+
|
|
155
|
+
`native-tls` 依赖 `openssl-sys`,而 `openssl-src` 不知道如何为 OHOS 配置 OpenSSL,**直接编译会失败**。必须使用预编译二进制:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
# 使用 ohos-openssl 预编译包(https://github.com/ohos-rs/ohos-openssl)
|
|
159
|
+
export AARCH64_UNKNOWN_LINUX_OHOS_OPENSSL_DIR="/path/to/ohos-openssl/prelude/arm64-v8a/"
|
|
160
|
+
export ARMV7_UNKNOWN_LINUX_OHOS_OPENSSL_DIR="/path/to/ohos-openssl/prelude/armeabi-v7a/"
|
|
161
|
+
export X86_64_UNKNOWN_LINUX_OHOS_OPENSSL_DIR="/path/to/ohos-openssl/prelude/x86_64/"
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
> 注意:OpenSSL 1.1.1 需要使用 ohos-openssl 的 `111` 分支。
|
|
165
|
+
|
|
166
|
+
### 4. flutter_rust_bridge 适配(如有)
|
|
167
|
+
|
|
168
|
+
若插件使用 `flutter_rust_bridge`,官方版本不支持 OHOS,必须替换为社区 fork:
|
|
169
|
+
|
|
170
|
+
#### 4.1 替换依赖
|
|
171
|
+
|
|
172
|
+
```yaml
|
|
173
|
+
# pubspec.yaml
|
|
174
|
+
dependency_overrides:
|
|
175
|
+
flutter_rust_bridge:
|
|
176
|
+
git:
|
|
177
|
+
url: https://github.com/Aloereed/flutter_rust_bridge_ohos.git
|
|
178
|
+
path: frb_dart
|
|
179
|
+
ref: master
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
> **已收录到 `flutter-adapted-library` Skill 数据库**,coding 阶段步骤 3.4 依赖覆写时会自动命中。
|
|
183
|
+
|
|
184
|
+
#### 4.2 重新生成 FFI 胶水代码
|
|
185
|
+
|
|
186
|
+
替换 fork 后**必须**重新生成:
|
|
187
|
+
- `frb_generated.dart`(Dart 侧)
|
|
188
|
+
- `frb_generated.rs`(Rust 侧)
|
|
189
|
+
|
|
190
|
+
使用 OHOS fork 版本的 `flutter_rust_bridge_codegen` 重新生成,否则序列化格式和函数签名可能不匹配。
|
|
191
|
+
|
|
192
|
+
#### 4.3 DynamicLibrary 加载路径
|
|
193
|
+
|
|
194
|
+
确认 `flutter_rust_bridge_ohos` 中 `loadExternalLibrary` 是否已处理 OHOS 平台路径。如未处理,需手动指定:
|
|
195
|
+
|
|
196
|
+
```dart
|
|
197
|
+
if (Platform.isOhos) {
|
|
198
|
+
DynamicLibrary.open("lib{plugin_name}.so");
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
#### 4.4 版本滞后风险
|
|
203
|
+
|
|
204
|
+
fork 版本(2.7.0)滞后于上游(2.11.x)。若插件依赖的上游 flutter_rust_bridge 版本较新,可能存在 API 不兼容:
|
|
205
|
+
- 检查 `Cargo.toml` 和 `pubspec.yaml` 中声明的 flutter_rust_bridge 版本约束
|
|
206
|
+
- 若差异大,在 `risk_items` 中标记
|
|
207
|
+
|
|
208
|
+
#### 4.4.1 版本不兼容时的替代方案:Patch 标准版
|
|
209
|
+
|
|
210
|
+
当 OHOS fork 版本与插件所需版本差距过大(如插件需要 `^2.12.0` 但 fork 仅 `2.7.0`),`dependency_overrides` 降级会导致 `pub get` hang 或 API 不兼容。此时可**直接 patch 标准版**:
|
|
211
|
+
|
|
212
|
+
**Dart 侧 patch**(pub cache 中的标准版 `flutter_rust_bridge`):
|
|
213
|
+
|
|
214
|
+
1. 找到 pub cache 中的库路径:`~/.pub-cache/hosted/pub.dev/flutter_rust_bridge-{version}/`
|
|
215
|
+
2. 清空 `.cargo-checksum.json` 以允许修改(写入空 JSON `{}`)
|
|
216
|
+
3. 在 `lib/src/loader/_io.dart` 的平台判断链中添加 OHOS:
|
|
217
|
+
```dart
|
|
218
|
+
// 找到类似 Platform.isAndroid 的判断链,添加 Platform.isOhos
|
|
219
|
+
if (Platform.isAndroid || Platform.isLinux || Platform.isOhos) {
|
|
220
|
+
return DynamicLibrary.open('lib{name}.so');
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**Rust 侧(解决 `Dart_*` 符号缺失)**:
|
|
225
|
+
|
|
226
|
+
OHOS 的 `libflutter.so` **不导出任何 `Dart_*` 符号**(如 `Dart_InitializeApiDL`)。这会导致运行时 FFI 初始化失败。解决方案是将 `dart_api_dl.c` 静态编译进 Rust `.so`:
|
|
227
|
+
|
|
228
|
+
1. 找到 `flutter_rust_bridge` Rust crate 的 `build.rs`
|
|
229
|
+
2. 添加 OHOS target 的 `dart_api_dl.c` 编译逻辑:
|
|
230
|
+
```rust
|
|
231
|
+
if target_os == "ohos" || target.contains("ohos") {
|
|
232
|
+
cc::Build::new()
|
|
233
|
+
.file("path/to/dart_api_dl.c") // flutter_rust_bridge 自带此文件
|
|
234
|
+
.include("path/to/dart_api_dl_include_dir")
|
|
235
|
+
.compile("dart_api_dl");
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
3. 确保 `dart_api_dl.h` 所在目录也在 include path 中
|
|
239
|
+
|
|
240
|
+
> **关键**:每次 `pub cache repair` 或版本变更后,Dart 侧 patch 会丢失,需要重新应用。建议编写自动化脚本。
|
|
241
|
+
|
|
242
|
+
#### 4.5 `@Native` 注解处理
|
|
243
|
+
|
|
244
|
+
flutter_rust_bridge 生成的代码中可能包含 `@Native` 注解 → 在步骤 4.5(@Native 绑定翻译检查)中处理,参见 `references/ffi-binding-translate.md`。
|
|
245
|
+
|
|
246
|
+
### 5. CargoKit / 构建系统集成(如有)
|
|
247
|
+
|
|
248
|
+
若插件使用 `cargokit` 将 Rust 编译集成到 Flutter 构建流程:
|
|
249
|
+
|
|
250
|
+
#### 5.1 添加 OHOS target 识别
|
|
251
|
+
|
|
252
|
+
cargokit 的 `build_tool/` 需要识别 OHOS 平台,通常需修改 `build_tool/lib/src/target.dart` 添加 OHOS target 映射。
|
|
253
|
+
|
|
254
|
+
#### 5.2 HVigor 构建对接
|
|
255
|
+
|
|
256
|
+
鸿蒙使用 HVigor(非 Gradle)作为构建系统。需要在 HVigor 构建流程中添加 Rust 交叉编译步骤:
|
|
257
|
+
|
|
258
|
+
- 方式 1:在 `hvigorfile.ts` 中添加 pre-build hook,触发 `cargo build --target aarch64-unknown-linux-ohos --release` 并拷贝 .so
|
|
259
|
+
- 方式 2:使用独立脚本先编译 Rust,再执行 `flutter build hap`
|
|
260
|
+
|
|
261
|
+
#### 5.3 无 CargoKit 时的手动流程
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
# 1. 交叉编译 Rust
|
|
265
|
+
cargo build --target aarch64-unknown-linux-ohos --release
|
|
266
|
+
|
|
267
|
+
# 2. 拷贝 .so 到 Flutter OHOS 项目
|
|
268
|
+
cp target/aarch64-unknown-linux-ohos/release/lib{name}.so \
|
|
269
|
+
ohos/libs/arm64-v8a/lib{name}.so
|
|
270
|
+
|
|
271
|
+
# 3. Flutter 构建 HAP
|
|
272
|
+
flutter build hap --debug
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### 6. .so 动态库打包
|
|
276
|
+
|
|
277
|
+
#### 6.1 文件存放路径
|
|
278
|
+
|
|
279
|
+
编译产物 `.so` 必须落入 `ohos/libs/${OHOS_ARCH}/`,通常为 `ohos/libs/arm64-v8a/`。
|
|
280
|
+
|
|
281
|
+
#### 6.2 Release 模式优化
|
|
282
|
+
|
|
283
|
+
```toml
|
|
284
|
+
# Cargo.toml
|
|
285
|
+
[profile.release]
|
|
286
|
+
strip = true # 符号剥离,减小体积
|
|
287
|
+
lto = true # 链接时优化(可选,编译更慢但体积更小)
|
|
288
|
+
opt-level = "z" # 优化体积(可选)
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
#### 6.3 SO 库压缩(可选)
|
|
292
|
+
|
|
293
|
+
在 `module.json5` 中配置,可节省约 65% 包体积:
|
|
294
|
+
|
|
295
|
+
```json5
|
|
296
|
+
{
|
|
297
|
+
"compressNativeLibs": true
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### 7. oh-package.json5
|
|
302
|
+
|
|
303
|
+
```json5
|
|
304
|
+
{
|
|
305
|
+
dependencies: {}
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
> **`@ohos/flutter_ohos` 依赖由 Flutter 构建工具自动注入,无需手动添加。**
|
|
310
|
+
|
|
311
|
+
### 8. 不建议初期启用的功能
|
|
312
|
+
|
|
313
|
+
对某些高级 Rust 功能,建议初期跳过,先跑通基础链路后再开启:
|
|
314
|
+
|
|
315
|
+
- **HTTP/3 / QUIC**:依赖 UDP socket,鸿蒙对 UDP 的权限和行为可能与 Linux 有差异
|
|
316
|
+
- **系统代理**:`reqwest` 通过环境变量 `HTTP_PROXY` / `HTTPS_PROXY` 获取系统代理,鸿蒙可能不通过环境变量暴露代理配置
|
|
317
|
+
- **系统证书信任链**:`rustls` 默认用 `webpki-roots`(Mozilla 根证书),跨平台通用;若需鸿蒙系统自定义证书,需额外处理
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
## 第二部分:编码实现
|
|
322
|
+
|
|
323
|
+
### .so 加载路径
|
|
324
|
+
|
|
325
|
+
与 compile_from_source 相同,使用 `DynamicLibrary.open`:
|
|
326
|
+
|
|
327
|
+
```dart
|
|
328
|
+
import 'dart:ffi';
|
|
329
|
+
import 'dart:io' show Platform;
|
|
330
|
+
|
|
331
|
+
DynamicLibrary _openLibrary() {
|
|
332
|
+
if (Platform.isAndroid || Platform.isLinux || Platform.isOhos) {
|
|
333
|
+
return DynamicLibrary.open('libxxx.so');
|
|
334
|
+
}
|
|
335
|
+
if (Platform.isIOS || Platform.isMacOS) {
|
|
336
|
+
return DynamicLibrary.process();
|
|
337
|
+
}
|
|
338
|
+
if (Platform.isWindows) {
|
|
339
|
+
return DynamicLibrary.open('xxx.dll');
|
|
340
|
+
}
|
|
341
|
+
throw UnsupportedError('Unsupported platform: ${Platform.operatingSystem}');
|
|
342
|
+
}
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### pubspec.yaml 平台声明
|
|
346
|
+
|
|
347
|
+
```yaml
|
|
348
|
+
flutter:
|
|
349
|
+
plugin:
|
|
350
|
+
platforms:
|
|
351
|
+
ohos:
|
|
352
|
+
ffiPlugin: true
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### Rust 侧注意事项
|
|
356
|
+
|
|
357
|
+
1. **确保 Rust 代码不依赖 Android/iOS 专有 API**:如 JNI、Objective-C runtime
|
|
358
|
+
2. **条件编译**:Rust 侧使用 `#[cfg(target_os = "linux")]` 或自定义 cfg 处理 OHOS 差异。注意 OHOS 的 `target_os` 值为 `"linux"`(因为基于 Linux 内核),目前无独立 `target_os = "ohos"`;可用 `#[cfg(target_env = "ohos")]` 区分
|
|
359
|
+
3. **C FFI 边界**:确保 Rust 导出的函数使用 `#[no_mangle] extern "C"`
|
|
360
|
+
4. **链接系统库**:OHOS 可用的 POSIX 接口与 Linux 类似,但部分 API 可能受限
|
|
361
|
+
5. **异步运行时**:`tokio` 在 OHOS 上可正常运行(ohos-rs 示例集已验证),但需注意线程模型差异
|
|
362
|
+
|
|
363
|
+
### 构建流程
|
|
364
|
+
|
|
365
|
+
```bash
|
|
366
|
+
# 1. 交叉编译 Rust
|
|
367
|
+
cargo build --release --target aarch64-unknown-linux-ohos
|
|
368
|
+
|
|
369
|
+
# 2. 拷贝 .so 到 Flutter OHOS 项目(如非 CargoKit 自动处理)
|
|
370
|
+
cp target/aarch64-unknown-linux-ohos/release/lib{name}.so \
|
|
371
|
+
ohos/libs/arm64-v8a/lib{name}.so
|
|
372
|
+
|
|
373
|
+
# 3. Flutter 构建 HAP
|
|
374
|
+
flutter build hap --debug
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
### CI / Headless 构建(无 DevEco Studio)
|
|
378
|
+
|
|
379
|
+
若 CI 环境无 DevEco Studio,可使用 Command Line Tools:
|
|
380
|
+
|
|
381
|
+
```bash
|
|
382
|
+
# 环境变量
|
|
383
|
+
export DEVECO_SDK_HOME=/path/to/command-line-tools/sdk
|
|
384
|
+
export NODE_HOME=/path/to/command-line-tools/tool/node
|
|
385
|
+
export PATH=$PATH:/path/to/command-line-tools/tool/hvigor/bin
|
|
386
|
+
|
|
387
|
+
# HVigor 构建命令
|
|
388
|
+
hvigorw clean
|
|
389
|
+
hvigorw --mode module -p product=default -p module=entry@default assembleHap
|
|
390
|
+
hvigorw -p product=default assembleApp
|
|
391
|
+
|
|
392
|
+
# 部署到真机(hdc 是 OHOS 的 adb 等效工具)
|
|
393
|
+
hdc file send entry/build/default/outputs/default/{name}-default-signed.hap "/data/local/tmp/"
|
|
394
|
+
hdc shell bm install -p /data/local/tmp/
|
|
395
|
+
hdc shell aa start -a EntryAbility -b {bundle_name}
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
> 注意:签名密钥需先在 Windows/Mac 的 DevEco Studio 中生成,CI 中复用。签名配置在 `build-profile.json5` 的 `signingConfigs` 中,包含密码,不要提交到仓库。
|
|
399
|
+
|
|
400
|
+
---
|
|
401
|
+
|
|
402
|
+
## 第三部分:常见编译错误与修复
|
|
403
|
+
|
|
404
|
+
### 1. Rust target 不可用
|
|
405
|
+
|
|
406
|
+
```
|
|
407
|
+
error[E0463]: can't find crate for `std`
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
**原因**:OHOS target 未安装。
|
|
411
|
+
**修复**:`rustup target add aarch64-unknown-linux-ohos`
|
|
412
|
+
|
|
413
|
+
### 2. openssl-sys 编译失败
|
|
414
|
+
|
|
415
|
+
```
|
|
416
|
+
error: failed to run custom build command for `openssl-sys`
|
|
417
|
+
This crate is compiled for ... but OpenSSL was not found for the target
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
**原因**:`native-tls` / `openssl-sys` 不知道如何为 OHOS 编译 OpenSSL。
|
|
421
|
+
**修复**(按优先级):
|
|
422
|
+
1. 切换到 `rustls-tls`(见第一部分 §3 方案 A)
|
|
423
|
+
2. 使用 ohos-openssl 预编译(见第一部分 §3 方案 B)
|
|
424
|
+
|
|
425
|
+
### 3. ring crate 编译失败
|
|
426
|
+
|
|
427
|
+
```
|
|
428
|
+
error: failed to run custom build command for `ring`
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
**原因**:`ring`(`rustls` 的加密后端)交叉编译时找不到 C 编译器。
|
|
432
|
+
**修复**:设置 CC/AR/CFLAGS 环境变量:
|
|
433
|
+
|
|
434
|
+
```bash
|
|
435
|
+
export CC_aarch64_unknown_linux_ohos=$OHOS_NDK_HOME/native/llvm/bin/aarch64-unknown-linux-ohos-clang
|
|
436
|
+
export AR_aarch64_unknown_linux_ohos=$OHOS_NDK_HOME/native/llvm/bin/llvm-ar
|
|
437
|
+
export CFLAGS_aarch64_unknown_linux_ohos="--sysroot=$OHOS_NDK_HOME/native/sysroot"
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
> 注意:也可使用 Clang wrapper 脚本(见 §2.3)作为 CC 值。
|
|
441
|
+
|
|
442
|
+
### 3.5 ring crate 编译通过但 TLS 运行时挂起
|
|
443
|
+
|
|
444
|
+
**症状**:HTTP(非 HTTPS)请求正常返回 200,但 HTTPS 请求无限挂起,无错误日志。
|
|
445
|
+
|
|
446
|
+
**原因**:`ring` 在 `aarch64-unknown-linux-ohos` 上的 aarch64 加密汇编可能存在运行时兼容性问题(根因待确认)。
|
|
447
|
+
|
|
448
|
+
**诊断方法**:
|
|
449
|
+
```bash
|
|
450
|
+
# 在真机上分别测试 HTTP 和 HTTPS
|
|
451
|
+
# 如果 HTTP ✅ 但 HTTPS ❌,即可确认
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
**修复**:见第一部分 §3「方案 A」中的缓解方案(升级 ring / 切换 aws-lc-rs / 回退 native-tls)。
|
|
455
|
+
|
|
456
|
+
### 3.6 OHOS libflutter.so 不导出 Dart_* 符号
|
|
457
|
+
|
|
458
|
+
**症状**:FFI 初始化时 crash 或 `Dart_InitializeApiDL` 符号找不到。
|
|
459
|
+
|
|
460
|
+
**原因**:OHOS 的 `libflutter.so` 不导出任何 `Dart_*` 符号(Android/iOS 会导出),这是 OHOS 平台限制。
|
|
461
|
+
|
|
462
|
+
**修复**:将 `dart_api_dl.c` 静态编译进 Rust `.so`,详见 §4.4.1。
|
|
463
|
+
|
|
464
|
+
### 4. cargokit 不识别 OHOS 平台
|
|
465
|
+
|
|
466
|
+
```
|
|
467
|
+
Unsupported target platform: ohos
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
**修复**:在 cargokit 的 target 映射中添加 OHOS,或使用自定义 build script 替代。具体需修改 `build_tool/lib/src/target.dart`。
|
|
471
|
+
|
|
472
|
+
### 5. flutter_rust_bridge 生成代码含 `@Native`
|
|
473
|
+
|
|
474
|
+
**修复**:触发 binding-translate 流程,参见 `references/ffi-binding-translate.md`。
|
|
475
|
+
|
|
476
|
+
### 6. .so 运行时加载失败
|
|
477
|
+
|
|
478
|
+
```
|
|
479
|
+
Invalid argument(s): Failed to load dynamic library 'libxxx.so'
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
**修复**:
|
|
483
|
+
- 确认 `.so` 存放在 `ohos/libs/arm64-v8a/` 目录
|
|
484
|
+
- 确认 `build-profile.json5` 中有 `nativeLib` 配置
|
|
485
|
+
- 确认编译架构与设备匹配(真机 = arm64-v8a,模拟器 = x86_64)
|
|
486
|
+
|
|
487
|
+
### 7. musl libc 兼容性问题
|
|
488
|
+
|
|
489
|
+
```
|
|
490
|
+
undefined symbol: __xxx_glibc_xxx
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
**原因**:Rust crate 或其 C 依赖使用了 glibc 特有的符号。
|
|
494
|
+
**修复**:
|
|
495
|
+
- 确认 Clang wrapper 传递了 `-D__MUSL__` 标志
|
|
496
|
+
- 检查依赖是否有 musl 兼容性问题,必要时启用对应 feature flag 或提 issue
|
|
497
|
+
|
|
498
|
+
### 8. Rust .so 链接失败
|
|
499
|
+
|
|
500
|
+
**修复**:确认 `.so` 文件路径正确,CMakeLists.txt 的 `install()` 指令已将其打包到 HAP。
|
|
501
|
+
|
|
502
|
+
---
|
|
503
|
+
|
|
504
|
+
## 参考资源
|
|
505
|
+
|
|
506
|
+
| 用途 | 链接 |
|
|
507
|
+
|------|------|
|
|
508
|
+
| Rust 官方 OHOS 交叉编译(Clang wrapper 完整模板) | https://doc.rust-lang.org/rustc/platform-support/openharmony.html |
|
|
509
|
+
| ohos-rs 快速入门 | https://ohos.rs/en/docs/basic/quick-start |
|
|
510
|
+
| ohos-rs 示例集(reqwest、tokio 等 12 个示例) | https://github.com/ohos-rs/example |
|
|
511
|
+
| ohos-openssl 预编译 | https://github.com/ohos-rs/ohos-openssl |
|
|
512
|
+
| flutter_rust_bridge OHOS fork | https://github.com/Aloereed/flutter_rust_bridge_ohos |
|
|
513
|
+
| Servo 鸿蒙 Demo(大型 Rust .so 打包完整案例) | https://github.com/jschwe/ServoDemo |
|
|
514
|
+
| HVigor CLI 构建教程 | https://dev.to/abnerming888/harmonyos-development-hvigorw-compile-and-build-implement-command-packaging-4n0p |
|