1. 插件介绍

Flutter OpenHarmony化开发过程中,开发者经常会遇到各种环境配置、编译、运行和开发相关的问题。本指南汇总了这些常见问题及其解决方案,帮助开发者快速定位和解决问题,提高开发效率。

2. 环境配置问题

2.1 建议使用的开发工具版本

在进行Flutter OpenHarmony化开发时,建议使用以下工具版本组合:

environment:
  flutter: "3.7.12-ohos"
  python: "3.8-3.11"
  java: "17"
  node: "18"
  ohpm: "1.6+"
  harmonyos_sdk: "api11"

2.2 断网环境 flutter pub get 执行失败

问题描述:在断网环境下执行 flutter pub get 命令时失败。

解决方案:使用 --offline 参数,完整命令如下:

flutter pub get --offline

2.3 Flutter从Windows复制到Linux/Mac环境后无法运行

问题描述:在Windows上开发的Flutter项目复制到Linux或Mac环境后无法运行,出现非法字符错误。

解决方案:将文件中的换行符由CRLF(\r\n)替换为LF(\n):

# Linux环境执行
sed -i "s/\r//" bin/dart $(find bin -name "*.sh") $(find bin -name "*.version")

# Mac环境执行
sed -i "" "s/\r//" bin/dart $(find bin -name "*.sh") $(find bin -name "*.version")

2.4 配置环境变量和代理

问题描述:Windows环境下 flutter doctor -v 无反应。

解决方案:配置系统环境变量:

# 在系统环境变量中添加
http_proxy=http://user:password@host:8080
https_proxy=http://user:password@host:8080
no_proxy=localhost,::1,127.0.0.1,localaddress,.localdomain.com

3. 依赖管理问题

3.1 使用AtomGit引入自定义修改版本的依赖

问题描述:需要使用特定的鸿蒙化版本的Flutter插件。

解决方案:在 pubspec.yaml 中使用AtomGit依赖:

dependencies:
  flutter:
    sdk: flutter
  path_provider:
    git:
      url: "https://atomgit.com/"
      path: "packages/path_provider/path_provider"
  shared_preferences:
    git:
      url: "https://atomgit.com/"
      path: "packages/shared_preferences/shared_preferences"

3.2 依赖冲突解决

问题描述:执行 flutter pub get 时出现依赖冲突错误。

解决方案:使用 dependency_overrides 消除依赖冲突:

dependencies:
  flutter:
    sdk: flutter
  flutter_cache_manager: ^3.3.1

dependency_overrides:
  path_provider:
    git:
      url: "https://atomgit.com/"
      path: "packages/path_provider/path_provider"
  path_provider_ohos:
    git:
      url: "https://atomgit.com/"
      path: "packages/path_provider/path_provider_ohos"

4. 编译问题

4.1 配置别名简化编译命令

问题描述:每次编译都需要输入长命令,效率低下。

解决方案:配置环境变量和别名:

# 在~/.bash_profile或~/.zshrc中添加
export ENGINE_DIR=~/ohos/engine
export ENGINE_DEBUG=$ENGINE_DIR/src/out/ohos_debug_unopt_arm64
export ENGINE_PROFILE=$ENGINE_DIR/src/out/ohos_profile_arm64
export ENGINE_RELEASE=$ENGINE_DIR/src/out/ohos_release_arm64

alias fbuildD="flutter build hap --local-engine=$ENGINE_DEBUG --debug"
alias fbuildP="flutter build hap --local-engine=$ENGINE_PROFILE --profile"
alias fbuildR="flutter build hap --local-engine=$ENGINE_RELEASE --release"
alias frunD="flutter run -d $(hdc list targets) --local-engine=$ENGINE_DEBUG --debug"
alias frunP="flutter run -d $(hdc list targets) --local-engine=$ENGINE_PROFILE --profile"
alias frunR="flutter run -d $(hdc list targets) --local-engine=$ENGINE_RELEASE --release"

使用方法:

# 创建项目
flutter create hello --platforms ohos
cd hello

# 编译debug版本
fbuildD

# 运行debug版本
frunD

4.2 打包release闪退、dev没问题

问题描述:应用在debug模式下运行正常,但release模式下闪退。

问题分析:使用了debug版本的flutter.har。

解决方案:重新打包release版本的flutter.har并替换:

# 编译release版本的flutter.har
cd $ENGINE_DIR/src
./flutter/tools/gn --ohos --runtime-mode=release --target-cpu=arm64
ninja -C out/ohos_release_arm64

# 替换应用中的flutter.har
cp out/ohos_release_arm64/flutter.har your_app/ohos/libs/

4.3 应用编译成功,安装后出现白屏

问题描述:应用编译成功并安装,但打开后出现白屏。

日志信息

Reason:Signal:SIGSEGV(SEGV_MAPERR)@0x00000086e3272bf8
LastFatalMessage:Thread:547846269584[FATAL:flutter/runtime/dart_vm_initializer.cc] Error while initializing the Dart VM: Wrong full snapshot version, expected '8af474944053df1f0a3be6e6165fa7cf' found 'adb4292f3ec25074ca70abcd2d5c7251'

问题分析:使用的engine产物和运行模式不一致。

解决方案:确保engine产物和运行模式保持一致:

# 使用debug版本引擎运行debug模式
flutter run -d device_id --local-engine=$ENGINE_DEBUG --debug

# 使用release版本引擎运行release模式
flutter run -d device_id --local-engine=$ENGINE_RELEASE --release

4.4 Mac环境release版本编译失败

问题描述:在Mac环境下编译release版本时出现权限错误。

报错日志

ProcessPackageException: ProcessException: Found candidates, but lacked sufficient permissions to excute "/Users/xxx/ohos/src/out/ohos_release_arm64/clang_arm4/dart".

解决方案:添加执行权限:

chmod -R +x /Users/xxx/ohos/src/out/ohos_release_arm64/*

5. 运行问题

5.1 模拟器运行默认计数器应用闪退

问题描述:在模拟器上运行默认的Flutter计数器应用时闪退。

问题分析:FloatingActionButton在某些模拟器上不被支持。

解决方案:在lib/main.dart中注释掉FloatingActionButton:

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      // floatingActionButton: FloatingActionButton(
      //   onPressed: _incrementCounter,
      //   tooltip: 'Increment',
      //   child: const Icon(Icons.add),
      // ),
    );
  }
}

6. 开发问题

6.1 依赖冲突解决

问题描述:使用第三方库时出现依赖冲突。

解决方案:使用 dependency_overrides 解决冲突:

dependencies:
  flutter:
    sdk: flutter
  some_third_party_package: ^1.0.0

dependency_overrides:
  path_provider:
    git:
      url: "https://atomgit.com/"
      path: "packages/path_provider/path_provider"
  another_conflicting_package:
    git:
      url: "https://atomgit.com/"
      path: "packages/another_package"

6.2 应用版本号被重置

问题描述:执行 flutter build hap --release 后,app.json中的versionName字段被重置为1.0.0。

解决方案:在build命令中指定版本号:

flutter build hap --release --build-name=4.0.3 --build-number=10000

7. 问题反馈模板

当遇到无法解决的问题需要反馈时,请提供以下关键信息:

issue_template:
  ide_version: "DevEco-Studio 5.0.3.300"
  device_info:
    name: "HUAWEI Mate 60 Pro"
    version: "3.0.0.22(SP81xxxxxx)"
  flutter_environment: "flutter doctor -v output"
  command: "flutter run -d device_id --debug"
  logs:
    build_log: "flutter build hap --debug > build.log 2>&1"
    hilog: "hdc hilog > hilog.log 2>&1"
    crash_log: "DevEco-Studio -> Log -> FaultLog -> 应用包名 -> 时间点 -> 导出"

8. 总结

本指南汇总了Flutter OpenHarmony化开发过程中的常见问题及其解决方案,涵盖了环境配置、依赖管理、编译、运行和开发等各个方面。通过遵循这些解决方案,开发者可以快速定位和解决问题,提高开发效率。

如果遇到本指南中未涵盖的问题,欢迎加入开源鸿蒙跨平台社区与其他开发者交流讨论。

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net

Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐