更新 Flutter 插件项目结构

1作者: flfljh大约 1 个月前原帖
# 更新 Flutter 插件项目结构 # HarmonyOS 下一步 ## 更新摘要 - 在 Flutter 插件的 `ohos` 目录中,将 HarmonyOS 项目结构替换为模块结构 - 将所有引用的 HAR 文件整合到 `ohos/har` 下 - 更新后移除 OHOS 插件中过时的模块目录 ## 更新步骤 以 [integration_test](https://gitee.com/openharmony-sig/flutter_flutter) 为例进行演示: ### 1. 将 integration_test/ohos 从项目格式重构为模块格式 ```sh cd flutter_flutter/packages/integration_test mv ohos/ohos ./ohos2 # 备份原始结构 rm -rf ohos # 移除旧结构 mv ohos2 ohos # 重命名为标准模块格式 cd example flutter pub get # 刷新依赖 flutter build hap --debug # 验证构建 ``` > *注意*: 在此阶段运行 `flutter run` 将会失败,直到配置更新完成。 ### 2. 配置更新 #### 2.1 更新 integration_test/ohos/oh-package.json5 *更新前*: ```json { "name": "ohos", ... } ``` *更新后*(与 pubspec.yaml 中的插件名称匹配): ```json { "name": "integration_test", "license": "Apache-2.0", "dependencies": { "@ohos/flutter_ohos": "file:har/flutter.har" } } ``` #### 2.2 更新 integration_test/ohos/src/main/module.json5 *更新前*: ```json { "module": { "name": "ohos", ... } } ``` *更新后*: ```json { "module": { "name": "integration_test", ... } } ``` #### 2.3 更新 integration_test/ohos/hvigorfile.ts *更新前*: ```typescript import { appTasks } from '@ohos/hvigor-ohos-plugin'; export default { system: appTasks, plugins:[] } ``` *更新后*: ```typescript export { harTasks } from '@ohos/hvigor-ohos-plugin'; ``` ## 验证步骤 1. 在 DevEco Studio 中打开 `integration_test/example` 并配置签名 2. 运行示例: ```sh cd integration_test/example flutter run -d $DEVICE --debug ``` ### 关键 HAR 参考配置 HAR 文件现在位于 `ohos/har` 下 #### 更新 example/ohos/oh-package.json5 ```json { "dependencies": { "@ohos/flutter_ohos": "file:./har/flutter.har" }, "overrides": { "@ohos/flutter_ohos": "file:./har/flutter.har" } } ``` #### 更新 example/ohos/entry/oh-package.json5 *更新前*: ```json { "dependencies": { "@ohos/integration_test": "file:./har/integration_test.har" } } ``` *更新后*: ```json { "dependencies": { "integration_test": "file:../har/integration_test.har" } } ``` ## 常见问题排查 ### 1. ENOENT: 没有这样的文件或目录 *错误*: ```log hvigor ERROR: ENOENT: no such file or directory, stat 'xxx/flutter_flutter/packages/integration_test/ohos/build/default/cache/default/default@packageHar/ohos/oh_modules/@ohos/flutter_ohos' ``` *解决方案*: 手动删除错误信息中提到的路径。 ### 2. 操作不允许(符号链接错误) *错误*: ```log hvigor ERROR: ENOENT: operation not permitted, symlink 'xxx/webview_flutter_ohos/ohos/webview_flutter/oh_modules/.ohpm/@ohos+flutter_ohos@file+libs+flutter.har/oh_modules/@ohos/flutter_ohos' -> 'xxx/webview_flutter_ohos/ohos/build/default/cache/default/default@PackageHar/webview_flutter/oh_modules/@ohos/flutter_ohos' ``` *解决方案*: 移除旧结构中的遗留目录: ```sh rm -rf flutter_packages/packages/webview_flutter_ohos/ohos/webview_flutter rm -rf flutter_packages/packages/path_provider_ohos/ohos/path_provider rm -rf flutter_packages/packages/file_selector_ohos/ohos/FileSelector ```
查看原文
# Updating Flutter Plugin Project Structure # HarmonyOs next ## Update Summary<p>- Replacing HarmonyOS project structure with module structure in the `ohos` directory of Flutter plugins - Consolidating all referenced HAR files under `ohos&#x2F;har` - Removing outdated module directories in OHOS plugins after update<p>## Update Procedure<p>Demonstrated using [integration_test](https:&#x2F;&#x2F;gitee.com&#x2F;openharmony-sig&#x2F;flutter_flutter) from flutter_flutter:<p>### 1. Restructure integration_test&#x2F;ohos from project to module format ```sh cd flutter_flutter&#x2F;packages&#x2F;integration_test mv ohos&#x2F;ohos .&#x2F;ohos2 # Backup original structure rm -rf ohos # Remove old structure mv ohos2 ohos # Rename to standard module format cd example flutter pub get # Refresh dependencies flutter build hap --debug # Verify build ```<p>&gt; *Note*: Running `flutter run` at this stage will fail until configuration updates are completed.<p>### 2. Configuration Updates<p>#### 2.1 Update integration_test&#x2F;ohos&#x2F;oh-package.json5 *Before*: ```json { &quot;name&quot;: &quot;ohos&quot;, ... } ```<p>*After* (match plugin name from pubspec.yaml): ```json { &quot;name&quot;: &quot;integration_test&quot;, &quot;license&quot;: &quot;Apache-2.0&quot;, &quot;dependencies&quot;: { &quot;@ohos&#x2F;flutter_ohos&quot;: &quot;file:har&#x2F;flutter.har&quot; } } ```<p>#### 2.2 Update integration_test&#x2F;ohos&#x2F;src&#x2F;main&#x2F;module.json5 *Before*: ```json { &quot;module&quot;: { &quot;name&quot;: &quot;ohos&quot;, ... } } ```<p>*After*: ```json { &quot;module&quot;: { &quot;name&quot;: &quot;integration_test&quot;, ... } } ```<p>#### 2.3 Update integration_test&#x2F;ohos&#x2F;hvigorfile.ts *Before*: ```typescript import { appTasks } from &#x27;@ohos&#x2F;hvigor-ohos-plugin&#x27;; export default { system: appTasks, plugins:[] } ```<p>*After*: ```typescript export { harTasks } from &#x27;@ohos&#x2F;hvigor-ohos-plugin&#x27;; ```<p>## Verification Steps<p>1. Open `integration_test&#x2F;example` in DevEco Studio and configure signing 2. Run the example: ```sh cd integration_test&#x2F;example flutter run -d $DEVICE --debug ```<p>### Critical HAR Reference Configuration HAR files are now located in `ohos&#x2F;har`<p>#### Update example&#x2F;ohos&#x2F;oh-package.json5 ```json { &quot;dependencies&quot;: { &quot;@ohos&#x2F;flutter_ohos&quot;: &quot;file:.&#x2F;har&#x2F;flutter.har&quot; }, &quot;overrides&quot;: { &quot;@ohos&#x2F;flutter_ohos&quot;: &quot;file:.&#x2F;har&#x2F;flutter.har&quot; } } ```<p>#### Update example&#x2F;ohos&#x2F;entry&#x2F;oh-package.json5 *Before*: ```json { &quot;dependencies&quot;: { &quot;@ohos&#x2F;integration_test&quot;: &quot;file:.&#x2F;har&#x2F;integration_test.har&quot; } } ```<p>*After*: ```json { &quot;dependencies&quot;: { &quot;integration_test&quot;: &quot;file:..&#x2F;har&#x2F;integration_test.har&quot; } } ```<p>## Troubleshooting Common Issues<p>### 1. ENOENT: No such file or directory *Error*: ```log hvigor ERROR: ENOENT: no such file or directory, stat &#x27;xxx&#x2F;flutter_flutter&#x2F;packages&#x2F;integration_test&#x2F;ohos&#x2F;build&#x2F;default&#x2F;cache&#x2F;default&#x2F;default@packageHar&#x2F;ohos&#x2F;oh_modules&#x2F;@ohos&#x2F;flutter_ohos&#x27; ```<p>*Solution*: Manually delete the path mentioned in the error message.<p>### 2. Operation not permitted (symlink error) *Error*: ```log hvigor ERROR: ENOENT: operation not permitted, symlink &#x27;xxx&#x2F;webview_flutter_ohos&#x2F;ohos&#x2F;webview_flutter&#x2F;oh_modules&#x2F;.ohpm&#x2F;@ohos+flutter_ohos@file+libs+flutter.har&#x2F;oh_modules&#x2F;@ohos&#x2F;flutter_ohos&#x27; -&gt; &#x27;xxx&#x2F;webview_flutter_ohos&#x2F;ohos&#x2F;build&#x2F;default&#x2F;cache&#x2F;default&#x2F;default@PackageHar&#x2F;webview_flutter&#x2F;oh_modules&#x2F;@ohos&#x2F;flutter_ohos&#x27; ```<p>*Solution*: Remove legacy directories from previous structure: ```sh rm -rf flutter_packages&#x2F;packages&#x2F;webview_flutter_ohos&#x2F;ohos&#x2F;webview_flutter rm -rf flutter_packages&#x2F;packages&#x2F;path_provider_ohos&#x2F;ohos&#x2F;path_provider rm -rf flutter_packages&#x2F;packages&#x2F;file_selector_ohos&#x2F;ohos&#x2F;FileSelector ```