5
头图

看了美团自己做了engune的底层适配,在官方社区也看到了对flutter的适配兼容,自己尝试着在arm\x86系统上进行了编译,编译通过(建议在linux上尝试),但是需要 OpenHarmony(API10)和HarmonyOS Next(API10)系统进行的兼容适配,暂不支持双框架的HarmonyOS系统,更加不支持模拟器系统,所以就没有跑起来,只能看着编译出来的hap包发呆。

1. 准备依赖以及仓库包

1.1. flutter

flutter 官方sdk

1.2. flutter_flutter

基于flutter sdk对于OpenHarmony的兼容拓展,可支持使用flutter tools指令编译和构建OpenHarmony应用程序

1.3. flutter_engine

基于flutter官方engine仓库拓展,可构建支持在OpenHarmony设备上运行的flutter engine程序。

1.4. developtools_hapsigner

Hap包签名工具

1.5. 环境包汇总

1) node环境 推荐>14
2) gradle-7.1
3) Command Line Tools for HarmonyOS Harmony应用开发系列工具
4) Command Line Tools for OpenHarmony OpenHarmony应用开发系列工具
5) python3、make、pkg-config、ninja-build、java11、java17 (linux apt 、mac brew)

1.6. vpn工具

编译flutter源码需要访问谷歌的资源,需要外网访问,梯子需要自行购买

2. 构建环境(以Ubuntu 22.04.3X86为例)

在/home文件夹下新建目录/dev

2.1. 更新apt-get源

sudo apt-get update //更新源
sudo apt install git
# 其他依赖按需下载 比如 unzip vim 等等

2.2. 配置node、gradle环境变量

编辑~/.bashrc文件 在最后填入如下配置,并执行source ~/.bashrc

# grade
export PATH=/home/dev/gradle-7.1/bin:$PATH
# nodejs
export NODE_HOME=/home/dev/node-v14.19.1-linux-x64
export PATH=$NODE_HOME/bin:$PATH

2.3. 配置环境变量ohpm与sdkmanager

下载命令行工具,并配置环境变量ohpm与sdkmanager,下载完成后执行ohpm/bin/init安装ohpm。参照指导文档:文档中心

#配置环境变量ohpm与sdkmanager
export OHPM_HOME=/home/dev/oh-command-line-tools/ohpm/
# sdkmanager
export PATH=/home/dev/oh-command-line-tools/sdkmanager/bin:$PATH
export PATH=$PATH:$OHPM_HOME/bin

2.4. 下载sdk并配置环境变量

dev目录下新建osdk目录以及sdk目录,参考文档中心 使用命令下载OpenHarmony sdk(API9以下),AP10需要从每日构建下载ohos-full-sdk

export OHOS_SDK_HOME=/home/dev/osdk/ 
export HDC_HOME=/home/dev/osdk/9/toolchains
export PATH=$PATH:$HDC_HOME

# 配置HarmonyOS sdk
export HOS_SDK_HOME=/home/dev/sdk/openharmony

目录结构如下

# HarmonyOS  sdk
/sdk
     ├── openharmony                                                  
     │   └── 10
     │       └── js
     │       └── native                                      
     │       └── previewer                     
     │       └── toolchains
     │   └── 9
 # OpenHarmony  sdk
 /osdk
     ├── 10
     │   └── js
     │   └── native                                      
     │   └── previewer                     
     │   └── toolchains
     ├── 9

     ...

2.5. 配置flutter环境变量

git clone https://gitee.com/openharmony-sig/flutter_flutter.git

# flutter
export FLUTTER_HOME=/home/dev/flutter
export PATH=$PATH:$FLUTTER_HOME/bin
export PATH=/home/dev/flutter_flutter/bin:$PATH
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

2.6. 签名工具签名

下载签名工具,并配置环境变量SIGN_TOOL_HOME。

git clone https://gitee.com/openharmony/developtools_hapsigner.git

#SIGN_TOOL_HOME
export SIGN_TOOL_HOME=/home/dev/developtools_hapsigner/autosign

进入developtools_hapsigner目录执行gradle build命令编译得到hap-sign-tool.jar,确保其在目录:./hapsigntool/hap_sign_tool/build/libs/hap-sign-tool.jar。
编辑autosign目录下autosign.configcreateAppCertAndProfile.config文件,并修改其中值:

sign.profile.inFile=profile_tmp.json

autosign目录下,执行命令**chmod 777 *.sh** ,新增profile_tmp_template.json文件,编辑如下:

{
           "version-name": "2.0.0",
           "version-code": 2,
           "app-distribution-type": "os_integration",
           "uuid": "5027b99e-5f9e-465d-9508-a9e0134ffe18",
           "validity": {
               "not-before": 1594865258,
               "not-after": 1689473258
           },
           "type": "release",
           "bundle-info": {
               "developer-id": "OpenHarmony",
               "distribution-certificate": "-----BEGIN CERTIFICATE-----\nMIICSTCCAc+gAwIBAgIFAJV7uNUwCgYIKoZIzj0EAwIwYzELMAkGA1UEBhMCQ04x\nFDASBgNVBAoMC09wZW5IYXJtb255MRkwFwYDVQQLDBBPcGVuSGFybW9ueSBUZWFt\nMSMwIQYDVQQDDBpPcGVuSGFybW9ueSBBcHBsaWNhdGlvbiBDQTAeFw0yMjAxMjkw\nNTU0MTRaFw0yMzAxMjkwNTU0MTRaMGgxCzAJBgNVBAYTAkNOMRQwEgYDVQQKDAtP\ncGVuSGFybW9ueTEZMBcGA1UECwwQT3Blbkhhcm1vbnkgVGVhbTEoMCYGA1UEAwwf\nT3Blbkhhcm1vbnkgQXBwbGljYXRpb24gUmVsZWFzZTBZMBMGByqGSM49AgEGCCqG\nSM49AwEHA0IABAW8pFu7tHGUuWtddD5wvazc1qN8ts9UPZH4pecbb/bSFWKh7X7R\n/eTVaRrCTSSdovI1dhoV5GjuFsKW+jT2TwSjazBpMB0GA1UdDgQWBBScyywAaAMj\nI7HcuIS42lvZx0Lj+zAJBgNVHRMEAjAAMA4GA1UdDwEB/wQEAwIHgDATBgNVHSUE\nDDAKBggrBgEFBQcDAzAYBgwrBgEEAY9bAoJ4AQMECDAGAgEBCgEAMAoGCCqGSM49\nBAMCA2gAMGUCMFfNidGo6uK6KGT9zT1T5bY1NCHTH3P3muy5X1xudOgxWoOqIbnk\ntmQYB78dxWEHLQIxANfApAlXAD/0hnyNC8RDzfLOPEeay6jU9FXJj3AoR90rwZpR\noN9sYD6Oks4VGRw6yQ==\n-----END CERTIFICATE-----\n",
               "bundle-name": "{{ohosId}}",
               "apl": "normal",
               "app-feature": "hos_normal_app"
           },
           "acls": {
               "allowed-acls": [
                   ""
               ]
           },
           "permissions": {
               "restricted-permissions": []
           },
           "issuer": "pki_internal"
       }

2.7. 编译Flutter Engine

git clone https://gitee.com/openharmony-sig/flutter_engine.git

# 安装基础库
sudo apt install python3
sudo apt install make
sudo apt install pkg-config
sudo apt install ninja-build

/home/dev 目录下新建 engine 文件夹,engine 内新建 .gclient 文件,编辑文件,
若同步代码提示无权限,可以先fork到自己的gitee仓库,并替换下面的url

solutions = [
  {
    "managed": False,
    "name": "src/flutter",
    "url": "git@gitee.com:openharmony-sig/flutter_engine.git",
    "custom_deps": {},
    "deps_file": "DEPS",
    "safesync_url": "",
  },
]

同步代码:在engine目录,执行gclient sync;这里会同步engine源码、官方packages仓,还有执行ohos_setup任务;(这里需要进行vpn代理,梯子需要自行科学上网购买)

# 安装 gclient 需要翻墙
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
# gclient 环境变量
export PATH=/home/dev/depot_tools:$PATH


export https_proxy=http://10.41.195.15:7890 http_proxy=http://10.41.195.15:7890 all_proxy=socks5://10.41.195.15:7890

下载sdk: 在每日构建下载ohos-sdk-full,在engine根目录下,新建文件夹 ndk/linux/4.0,解压ohos-sdk-full中的native文件夹并放置到ndk/linux/4.0文件夹中

构建,在engine目录,执行make,即可开始构建支持ohos设备的flutter engine。

这两步时间较长,大约1~2个小时,中途还可能遇到陆陆续续的网络问题,需要杀掉重新开始

2.8. 构建项目并打包

  1. 运行 flutter doctor -v 检查环境变量配置是否正确,FutterOpenharmony应都为[✓]或者[!]标识,只要不是[✗]就可以

    ## flutter doctor
    
    [!]Flutter (Channel master, 3.7.12, on Ubuntu 20.04.3 LTS 5.11.0-37-generic, locale en_US.UTF-8)
     Flutter version 3.7.12 on channel master at /home/parallels/dev/flutter_flutter
     Upstream repository https://gitee.com/openharmony-sig/flutter_flutter.git is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to https://gitee.com/openharmony-sig/flutter_flutter.git to dismiss this error.
     Framework revision 00cc3dce34 (10 days ago), 2023-12-11 13:38:48 +0000
     Engine revision 1a65d409c7
     Dart version 2.19.6
     DevTools version 2.20.1
     Pub download mirror https://pub.flutter-io.cn
     Flutter download mirror https://storage.flutter-io.cn
     If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update
      checks and upgrades.
    
    [✓]OpenHarmony toolchain - develop for OpenHarmony devices
     OpenHarmony Sdk location: /home/dev/osdk/, available api versions has [9, 10]
     hdc version 1.2.0
     ohpm version 1.2.0
     signTool location:/home/dev/developtools_hapsigner/autosign
    .....
  2. 创建工程与编译命令,编译产物在flutter_demo/ohos/entry/build/default/outputs/default/entry-default-signed.hap下。

    # 创建工程
    flutter create --platforms ohos flutter_demo
    
    # 进入工程根目录编译
    flutter build hap --target-platform ohos-arm64 --debug true --local-engine=/home/dev/engine/src/out/ohos_debug_unopt_arm64

3. Mac环境编译 问题记录

系统:M2 ARM
在执行make过程中遇到版本不支持问题

4. windows环境编译 问题记录

遇到问题 为flutter channel windows版本不支持不问题。编译环境对windows不太友好,这里就未做过多尝试

5. linux X86环境编译 问题记录

1) 在执行到最后一步build过程中遇到Unable to find the following components: toolchains:10论坛已有此问题,暂无解决方案
期间尝试更换为9的API,提示需要大于等于10的SDK版本,不生效
下载HarmonyOSOpenHarmony对应的10的sdk并配置环境,不生效
2) 在官方issues下找到了此解决方案

"app": {
    "signingConfigs": [],
    # 移到这个层级
    "compileSdkVersion": 9,
    "compatibleSdkVersion": 9,
    "products": [
      {
        "name": "default",
        "signingConfig": "default",
        # 去掉
        # "compileSdkVersion": 9,
        # "compatibleSdkVersion": 9,
        # 改成 OpenHarmony
        "runtimeOS": "OpenHarmony",
      }
    ]
  }

按照此方案可以编译成功api9的hap包,但是安装app白屏,因为:api9 run ,app 空白页

6. linux ARM环境编译 问题记录

ARM环境不支持的包以及依赖比较多

7. 结论

在x86 linux系统上使用api 9编译hap成功,但无法在运行-白屏(目前适配的flutter是针对OpenHarmony(API10)和HarmonyOS Next(API10)系统进行的兼容适配,暂不支持双框架的HarmonyOS系统,更加不支持模拟器系统。)
在arm系统上编译hap失败。

目前适配处于起步阶段,发展空间较大,期待后续的遥遥领先

8. 参考资料

让 Flutter 在鸿蒙系统上跑起来 - 美团技术团队
Flutter Love 鸿蒙

交流群


雾岛听风
12.1k 声望8.6k 粉丝

丰富自己,胜过取悦别人。