配置环境变量

  1. 配置HarmonyOS SDK和环境变量
  • API12, deveco-studio-5.0 或 command-line-tools-5.0
  • 配置 Java17
  • 配置环境变量 (SDK, node, ohpm, hvigor)
 export TOOL_HOME=/Applications/DevEco-Studio.app/Contents # mac环境
 export DEVECO_SDK_HOME=$TOOL_HOME/sdk # command-line-tools/sdk
 export PATH=$TOOL_HOME/tools/ohpm/bin:$PATH # command-line-tools/ohpm/bin
 export PATH=$TOOL_HOME/tools/hvigor/bin:$PATH # command-line-tools/hvigor/bin
 export PATH=$TOOL_HOME/tools/node/bin:$PATH # command-line-tools/tool/node/bin
  1. 通过代码工具下载Flutter仓库代码git clone https://gitee.com/harmonycommando_flutter/flutter.git,指定dev或master分支,并配置环境
 # 依赖缓存
 export PUB_CACHE=D:/PUB
 # 拉取下来的flutter_flutter/bin目录
 export PATH=<flutter_flutter path>/bin:$PATH
 # 国内镜像
 export PUB_HOSTED_URL=https://pub.flutter-io.cn
 export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

 # HamonyOS SDK
 export TOOL_HOME=/Applications/DevEco-Studio.app/Contents # mac环境
 export DEVECO_SDK_HOME=$TOOL_HOME/sdk # command-line-tools/sdk
 export PATH=$TOOL_HOME/tools/ohpm/bin:$PATH # command-line-tools/ohpm/bin
 export PATH=$TOOL_HOME/tools/hvigor/bin:$PATH # command-line-tools/hvigor/bin
 export PATH=$TOOL_HOME/tools/node/bin:$PATH # command-line-tools/tool/node/bin

构建步骤

  1. 运行 flutter doctor -v 检查环境变量配置是否正确,Futter与OpenHarmony应都为ok标识,若两处提示缺少环境,按提示补上相应环境即可。
    img1
  2. 创建工程与编译命令,编译产物在<projectName>/ohos/entry/build/default/outputs/default/entry-default-signed.hap下。

     # 创建工程
     flutter create --platforms ohos <projectName>
    
     # 进入工程根目录编译
     # 示例:flutter build hap --release
     flutter build hap --release
  3. 通过flutter devices指令发现ohos设备之后,使用 hdc -t <deviceId> install <hap file path>进行安装。
  4. 也可直接使用下列指令运行:

    # 示例:flutter run -d <device-id>
    flutter run --debug -d <device-id>
  5. 构建app包命令:

     # 示例:flutter build app --release
     flutter build app --release

    ## 已兼容OpenHarmony开发的指令列表

    |指令名称| 指令描述| 使用说明|
    |:--|:--|:--|
    |doctor|环境检测|flutter doctor|
    |config|环境配置|flutter config --"key" "value"|
    |create|创建新项目|flutter create --platforms ohos,android,ios --org "org" "appName"|
    |create|创建module模板|flutter create -t module "moduleName"|
    |create|创建plugin模板|flutter create -t plugin --platforms ohos,android,ios "pluginName"|
    |create|创建plugin_ffi模板|flutter create -t plugin_ffi --platforms ohos,android,ios "pluginName"|
    |devices|已连接设备查找|flutter devices|
    |install|应用安装|flutter install -t "deviceId" "hap文件路径"|
    |assemble|资源打包|flutter assemble|
    |build|测试应用构建|flutter build hap --debug|
    |build|正式应用构建|flutter build hap --release|
    |run|应用运行|flutter run|
    |attach|调试模式|flutter attach|
    |screenshot|截屏|flutter screenshot|

附:Flutter三方库适配计划

运行到手机上

在项目根目录下执行flutter run就可以运行到手机上了,若连接的是Android手机,则可以直接运行并在手机上查看效果。若连接的是鸿蒙NEXT手机,则需先配置证书,配置证书的地方在ohos/build-profile.json5文件中,证书配置完成后,就直接直接运行并在鸿蒙NEXT手机上查看效果了。
harmony
android
鸿蒙平台ohos下的flutter.har包可以拷贝到其它项目中直接使用。


龙儿筝
1 声望0 粉丝