更新 Flutter 插件项目结构
# 更新 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/har`
- Removing outdated module directories in OHOS plugins after update<p>## Update Procedure<p>Demonstrated using [integration_test](https://gitee.com/openharmony-sig/flutter_flutter) from flutter_flutter:<p>### 1. Restructure integration_test/ohos from project to module format
```sh
cd flutter_flutter/packages/integration_test
mv ohos/ohos ./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>> *Note*: Running `flutter run` at this stage will fail until configuration updates are completed.<p>### 2. Configuration Updates<p>#### 2.1 Update integration_test/ohos/oh-package.json5
*Before*:
```json
{
"name": "ohos",
...
}
```<p>*After* (match plugin name from pubspec.yaml):
```json
{
"name": "integration_test",
"license": "Apache-2.0",
"dependencies": {
"@ohos/flutter_ohos": "file:har/flutter.har"
}
}
```<p>#### 2.2 Update integration_test/ohos/src/main/module.json5
*Before*:
```json
{
"module": {
"name": "ohos",
...
}
}
```<p>*After*:
```json
{
"module": {
"name": "integration_test",
...
}
}
```<p>#### 2.3 Update integration_test/ohos/hvigorfile.ts
*Before*:
```typescript
import { appTasks } from '@ohos/hvigor-ohos-plugin';
export default {
system: appTasks,
plugins:[]
}
```<p>*After*:
```typescript
export { harTasks } from '@ohos/hvigor-ohos-plugin';
```<p>## Verification Steps<p>1. Open `integration_test/example` in DevEco Studio and configure signing
2. Run the example:
```sh
cd integration_test/example
flutter run -d $DEVICE --debug
```<p>### Critical HAR Reference Configuration
HAR files are now located in `ohos/har`<p>#### Update example/ohos/oh-package.json5
```json
{
"dependencies": {
"@ohos/flutter_ohos": "file:./har/flutter.har"
},
"overrides": {
"@ohos/flutter_ohos": "file:./har/flutter.har"
}
}
```<p>#### Update example/ohos/entry/oh-package.json5
*Before*:
```json
{
"dependencies": {
"@ohos/integration_test": "file:./har/integration_test.har"
}
}
```<p>*After*:
```json
{
"dependencies": {
"integration_test": "file:../har/integration_test.har"
}
}
```<p>## Troubleshooting Common Issues<p>### 1. ENOENT: No such file or directory
*Error*:
```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'
```<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
'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'
```<p>*Solution*:
Remove legacy directories from previous structure:
```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
```