服务器之家:专注于服务器技术及软件下载分享
分类导航

服务器资讯|IT/互联网|云计算|区块链|软件资讯|操作系统|手机数码|百科知识|免费资源|头条新闻|

服务器之家 - 新闻资讯 - 操作系统 - Ubuntu22搭建支持OpenHarmony的Flutter应用开发环境详细教程

Ubuntu22搭建支持OpenHarmony的Flutter应用开发环境详细教程

2023-10-17 21:00未知服务器之家 操作系统

想了解更多关于开源的内容,请访问: 本站开源基础软件社区 序言 实践过程中的部分视频演示 https://www.bilibili.com/video/BV1Dp4y1w7kP https://www.bilibili.com/video/BV1Fj411t7mM 由于支持OpenHarmony的Flutter发布不久,还存在若干问题和文档遗漏,

Ubuntu22搭建支持OpenHarmony的Flutter应用开发环境详细教程

想了解更多关于开源的内容,请访问:

本站开源基础软件社区

序言

实践过程中的部分视频演示

  • https://www.bilibili.com/video/BV1Dp4y1w7kP
  • https://www.bilibili.com/video/BV1Fj411t7mM
  • 由于支持OpenHarmony的Flutter发布不久,还存在若干问题和文档遗漏,随着时间的推移都在不断完善,本文是我在搭建开发环境和探索研究过程下来的一个详细笔记,希望对大家有所帮助!
  • 文中涉及到的一些源码存放目录和环境变量配置,请根据你实际存放的位置进行相应的调整。
  • 文章附件提供Flutter打包的OpenHarmony应用hap样本,可供参考研究

环境概述

  • Ubuntu 22.04 (虚拟机)
  • Flutter Enginehttps://gitee.com/openharmony-sig/flutter_engine
  • Flutter SDKhttps://gitee.com/openharmony-sig/flutter_flutter
  • ohos-sdk-full 4.0 beta2http://ci.openharmony.cn/workbench/cicd/dailybuild/dailylist

Flutter Engine 环境和编译

安装依赖

  • sudo apt install git curl unzip pkgconf python3-pip
  • depot_tools
mkdir ~/work
cd ~/work
git clone http://www.zzvips.com/uploads/allimg/mq1pznimxbq.git
vim ~/.bashrc
# --- 写入下面这行配置
export PATH="$PATH:/home/ubuntu/work/depot_tools"
# ---
source ~/.bashrc

获取源码

ssh-keygen
cat ~/.ssh/id_rsa.pub
# Gitee - 设置 - SSH公钥 - 添加

cd ~/work
mkdir engine
cd engine
touch .gclient
vim .gclient
# --- 写入如下内容
solutions = [
  {
    "managed": False,
    "name": "src/flutter",
    "url": "git@gitee.com:openharmony-sig/flutter_engine.git",
    "custom_deps": {},
    "deps_file": "DEPS",
    "safesync_url": "",
  },
]
# ---
gclient sync

# 从 http://ci.openharmony.cn/workbench/cicd/dailybuild/dailylist 每日构建中,下载ohos-sdk-full,在engine根目录下,新建文件夹 ndk/linux/4.0 ,解压ohos-sdk-full sdk中的native文件夹到 ndk/linux/4.0 文件夹中;
mkdir -p ndk/linux/4.0
cd ndk/linux/4.0
# 把 native-linux-x64-4.0.9.6-Beta2.zip 放到此目录,并解压
unzip native-linux-x64-4.0.9.6-Beta2.zip

编译构建

开始编译:

cd ~/work/engine
make

出现如下错误:

../../third_party/dart/runtime/bin/socket_base_posix.cc -o obj/third_party/dart/runtime/bin/dart_io_api.socket_base_posix.o
../../third_party/dart/runtime/bin/socket_base_posix.cc:154:12: error: comparison of integers of different signs: 'unsigned long' and 'long' [-Werror,-Wsign-compare]
    cmsg = CMSG_NXTHDR(&msg, cmsg);
           ^~~~~~~~~~~~~~~~~~~~~~~
/home/ubuntu/work/engine/ndk/linux/4.0/native/sysroot/usr/include/sys/socket.h:360:44: note: expanded from macro 'CMSG_NXTHDR'
        __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../third_party/dart/runtime/bin/socket_base_posix.cc:160:15: error: comparison of integers of different signs: 'unsigned long' and 'long' [-Werror,-Wsign-compare]
       cmsg = CMSG_NXTHDR(&msg, cmsg), control_message++) {
              ^~~~~~~~~~~~~~~~~~~~~~~
/home/ubuntu/work/engine/ndk/linux/4.0/native/sysroot/usr/include/sys/socket.h:360:44: note: expanded from macro 'CMSG_NXTHDR'
        __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../third_party/dart/runtime/bin/socket_base_posix.cc:263:33: error: comparison of integers of different signs: 'unsigned long' and 'long' [-Werror,-Wsign-compare]
         i++, message++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
                                ^~~~~~~~~~~~~~~~~~~~~~~
/home/ubuntu/work/engine/ndk/linux/4.0/native/sysroot/usr/include/sys/socket.h:360:44: note: expanded from macro 'CMSG_NXTHDR'
        __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 errors generated.
[122/8624] CXX obj/flutter/third_party/tonic/file_loader/tonic.file_loader_posix.o
ninja: build stopped: subcommand failed.

解决方案,忽略编译警告,重新编译通过修改 /home/ubuntu/work/engine/src/build/config/compiler/BUILD.gn。

# 搜索 default_warning_flags = 修改为下面的代码
# line 602 603
default_warning_flags = ["-Wno-sign-compare"]
default_warning_flags_cc = ["-Wno-sign-compare"]

编译成功输出目录:

/home/ubuntu/work/engine/src/out/ohos_debug_unopt_arm64
/home/ubuntu/work/engine/src/out/ohos_release_arm64

Flutter SDK 环境和配置Clone SDK

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

~/.bashrc 完整环境

可以参考下,根据你实际的存放位置修改路径。

# depot-tools
export PATH="$PATH:/home/ubuntu/work/depot_tools"

# java
export JAVA_HOME=/home/ubuntu/env/jdk-17.0.8
export PATH="$PATH:$JAVA_HOME/bin"

# node.js
export NODE_HOME=/home/ubuntu/env/node-v14.19.1-linux-x64
export PATH="$PATH:$NODE_HOME/bin"

# ohos
export PATH="$PATH:/home/ubuntu/env/oh-command-line-tools/bin"
export OHOS_SDK_HOME=/home/ubuntu/env/ohos-sdk
export OHPM_HOME=/home/ubuntu/env/oh-command-line-tools/ohpm
export SIGN_TOOL_HOME=/home/ubuntu/env/developtools_hapsigner/autosign

# gradle
export GRADLE_HOME=/home/ubuntu/env/gradle-7.1
export PATH="$PATH:$GRADLE_HOME/bin"

# flutter
export PUB_HOSTED_URL=http://www.zzvips.com/uploads/allimg/ytiklz12nwt
export FLUTTER_STORAGE_BASE_URL=http://www.zzvips.com/uploads/allimg/n2qy4qaxc5a.cn
export PATH="$PATH:/home/ubuntu/env/flutter_flutter/bin"

# hdc
export HDC_HOME=/home/ubuntu/env/ohos-sdk/10/toolchains
export PATH="$PATH:$HDC_HOME"

环境细节

  • Command Line Tools for OpenHarmony
  • ohcommandline-tools-linux-2.0.0.2.zip
  • use ohos-full-sdk api10
  • ohpm/in/init 安装 ohpm如果失败多尝试几次
  • developtools_hapsigner
  • 参照developtools_hapsigner的readme,编译得到 hap-sign-tool.jar ,确保其在目录下:./hapsigntool/hap_sign_tool/build/libs/hap-sign-tool.jar(编译需要jdk11+gradle7.1)
  • 进入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"
}
  • 编辑autosign.config和createAppCertAndProfile.config,修改值:sign.profile.inFile=profile_tmp.json。
  • 配置<当前项目flutter_flutter目录>/bin到环境变量PATH,确保which flutter能找到<flutter sdk>/bin/flutter位置
  • 运行flutter docker,检查环境变量配置是否都正确。
  • 打开vscode,安装flutter插件,如果flutter sdk配置正确,可发现OpenHarmony连接设备,可在vscode上运行和调试应用。

补充细节

  • ohos-sdk & hdc,通过 ohsdkmgr 手动下载SDK,添加api10的full-sdk,并配置到环境变量。
  • npm 需要正确配置.npmrc,否则后续hvigor编译ohos项目会出错。
touch ~/.npmrc
vim ~/.npmrc
# 写入如下内容
registry=http://www.zzvips.com/uploads/allimg/hb01ehnebhk.org
@ohos:registry=https://repo.harmonyos.com/npm/
# 或者这样
registry=http://www.zzvips.com/uploads/allimg/pdvx4clkcxo
@ohos:registry=https://repo.harmonyos.com/npm/

项目创建编译等命令

常用命令:

# 环境检测	
flutter doctor

# 环境配置
flutter config --<key> <value>

# 创建新项目
flutter create --platforms ohos,android --org <org> <appName>

# 已连接设备查找
flutter devices

# 应用安装
flutter install

# 资源打包
flutter assemble

# 应用构建
flutter build hap --target-platform ohos-arm --debug true --local-engine=<兼容ohos的engine产物路径>

# 应用运行
flutter run --local-engine=<兼容ohos的engine产物路径>

# 调试模式
flutter attach

实践命令(创建项目并打包hap)。

# create
flutter create --platforms ohos,android myapp
cd myapp
# build debug hap
flutter build hap --target-platform ohos-arm64 --debug true --local-engine=/home/ubuntu/work/engine/src/out/ohos_debug_unopt_arm64
# build release hap
flutter build hap --target-platform ohos-arm64 --local-engine=/home/ubuntu/work/engine/src/out/ohos_release_arm64

flutter build hap -h。

Build an Ohos Hap file from your app.

Global options:
-h, --help                  Print this usage information.
-v, --verbose               Noisy logging, including all shell commands executed.
                            If used with "--help", shows hidden options. If used with "flutter doctor", shows additional diagnostic information. (Use "-vv" to force verbose logging in those cases.)
-d, --device-id             Target device id or name (prefixes allowed).
    --version               Reports the version of this tool.
    --suppress-analytics    Suppress analytics reporting when this command runs.

Usage: flutter build hap [arguments]
-h, --help               Print this usage information.
    --target-platform    The target platform for which the app is compiled.
                         [ohos-arm64 (default), ohos-arm, ohos-x86]
    --debug              Build a debug version of your app.
    --profile            Build a version of your app specialized for performance profiling.
    --release            Build a release version of your app (default mode).

hdc找不到已连接设备的解决方案

在Linux下在非root权限下使用hdc会出现无法找到设备的情况,此问题原因为用户USB操作权限问题,解决方法如下:

sudo chmod -R 777 /dev/bus/usb/

或者su切换到root用户下运行。

这样操作之后 hdc list targets 可查看到已连接的OH设备。

文章相关附件可以点击下面的原文链接前往下载:

/resource/3049

想了解更多关于开源的内容,请访问:

本站开源基础软件社区

延伸 · 阅读

精彩推荐