OpenHarmony开发者论坛

标题: 鸿蒙 Love Flutter [打印本页]

作者: zmtzawqlp    时间: 2023-11-15 10:20
标题: 鸿蒙 Love Flutter
[md]## 前言

在 [大喵](https://juejin.cn/user/817692379985752) 的 [Harmony 开始支持 Flutter ,聊聊 Harmony 和 Flutter 之间的因果 - 掘金 (juejin.cn)](https://juejin.cn/post/7280006531922673719) 提到已经有 针对 OpenHarmony 的 Flutter 版本已经开源。

[OpenHarmony-SIG/flutter_flutter (gitee.com)](https://gitee.com/openharmony-sig/flutter_flutter)

但是只支持在 `linux` 上面编译。
![DM_20231115094712_001.jpg](https://forums-obs.openharmony.c ... ng7zg1bt1y7n1hb.jpg "DM_20231115094712_001.jpg")

而 Windows 上面有 [WSL(Windows Subsystem for Linux)](https://baike.baidu.com/item/wsl/20359185?fr=ge_ala), 我们可以很方便的获得 `linux` 环境。

[OpenHarmony-SIG/flutter_flutter (gitee.com)](https://gitee.com/openharmony-sig/flutter_flutter) 和 [OpenHarmony-SIG/flutter_engine (gitee.com)](https://gitee.com/openharmony-sig/flutter_engine) 的文档写的比较简单,很多东西需要 [openharmony 官方文档中找到的](https://docs.openharmony.cn/)查找 。坑我都踩过了,大家可以按照我的步骤,避免走冤枉路。

先看最后结果,最终安装运行成功。

![1.gif](https://forums-obs.openharmony.c ... ppedccvc9ovcbgd.gif "1.gif")

## WSL

* 设置=》控制面板 =》程序 =》启动或关闭 Windows 功能

![DM_20231115094712_002.jpg](https://forums-obs.openharmony.c ... a8av46p6vozz46l.jpg "DM_20231115094712_002.jpg")

确保 `适用于 Linux 的 Windows 子系统` 和 `虚拟机平台` 开启。

* 去微软商店中搜索 `ubuntu`,我安装的是`Ubuntu 22.04.2 LTS` 版本

![DM_20231115094712_003.jpg](https://forums-obs.openharmony.c ... jx66qx1frfjrc5v.jpg "DM_20231115094712_003.jpg")

* 安装完毕之后,打开 `Ubuntu 22.04.2 LTS` ,第一次打开会让你创建账号和密码。
* 对于习惯图形化的我来说,能少敲命令肯定更愿意少敲。所以打开你的 `vscode` ,安装插件 `WSL` 。顺手你可以把 `Flutter` 插件也安装了。

![DM_20231115094712_004.jpg](https://forums-obs.openharmony.c ... jmvmwo0voaj3c3w.jpg "DM_20231115094712_004.jpg")

这样子,通过  `vscode` 打开 `WSL` 就这样子的。

![DM_20231115094712_006.jpg](https://forums-obs.openharmony.c ... 0p0pk0115q505vk.jpg "DM_20231115094712_006.jpg")![DM_20231115094712_007.jpg](https://forums-obs.openharmony.c ... yite7ia8e7xtqkx.jpg "DM_20231115094712_007.jpg")![DM_20231115094712_005.jpg](https://forums-obs.openharmony.c ... vbvpssjj9njb9k9.jpg "DM_20231115094712_005.jpg")

我们的项目到时候就都放在 `home/zmtzawqlp` 下面。

## 环境

### apt 国内源

这个其实没有配置,因为后面也需要代理翻墙,如果你需要设置的话,按照下面操作。

```
# 备份
sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
# 修改
sudo vim /etc/apt/sources.list
```

内容可以从下面复制

[ubuntu | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror](https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/)

设置好之后,执行下`sudo apt update`

### 安装需要的库

```
# 编译jar 用 11
sudo apt install default-jdk
# 鸿蒙的 ohsdkmgr 命令要用 17, 等后面编译了 jar 之后再切换到 17
# sudo apt install openjdk-17-jdk

# linux
sudo apt install unzip
sudo apt install npm
sudo apt install cmake
sudo apt install clang
sudo apt install pkg-config
sudo apt install libgtk-3-dev
sudo apt install ninja-build
```

去下载 `node-v14.19.1-linux-x64.tar.xz` 和 `gradle-7.1-bin.zip` 解压之后。

你的用户下面有一个 `.bashrc` 文件,`vscode` 里面直接打开,文件最后填入

```
# grade
export PATH=/home/zmtzawqlp/ohos/tools/gradle-7.1/binPATH
# nodejs
export NODE_HOME=/home/zmtzawqlp/ohos/tools/node-v14.19.1-linux-x64
export PATH=$NODE_HOME/binPATH
```

![DM_20231115094712_008.jpg](https://forums-obs.openharmony.c ... 5llhbgmbbtgdg5w.jpg "DM_20231115094712_008.jpg")

保存,并且执行`source .bashrc` 使之生效。

## depot_tools

下载 [depot_tools (commondatastorage.googleapis.com)](https://commondatastorage.google ... al.html#_setting_up)

在 `.bashrc` 中配置

```
# depot_tools
export PATH=/home/zmtzawqlp/ohos/flutter/depot_toolsPATH
```

保存,并且执行`source .bashrc` 使之生效。

## 同步引擎代码

创建 `engine` 目录,创建文件 `.gclient`。

内容如下:

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

`gitee.com:zmtzawqlp/flutter_engine.git` 是我 `fork` 的 [OpenHarmony-SIG/flutter_engine (gitee.com)](https://gitee.com/openharmony-sig/flutter_engine)。 `Gitee SSH Key `避免下载没权限。

在 `engine` 目录下面执行`gclient sync`,最终同步完成之后的文件目录长这样子。

![DM_20231115094712_009.jpg](https://forums-obs.openharmony.c ... xdzdj1xxxrhixj0.jpg "DM_20231115094712_009.jpg")

先放下这个,在编译引擎之后还需要去下载鸿蒙的 `sdk `。

## 鸿蒙 SDK

[HUAWEI DevEco Studio和SDK下载和升级 | HarmonyOS开发者](https://developer.harmonyos.com/ ... studio#download_cli) 这个页面去下载  `Command Line Tools for OpenHarmony` 下面的 `linux` 版本

解压之后,目录是这样的。

![DM_20231115094712_010.jpg](https://forums-obs.openharmony.c ... lkkmvmql143nk55.jpg "DM_20231115094712_010.jpg")

将下面的配置加到  `.bashrc` 中,保存,并且执行`source .bashrc` 使之生效。

```
export OHPM_HOME=/home/zmtzawqlp/ohos/tools/oh-command-line-tools/ohpm/
export OHOS_SDK_HOME=/home/zmtzawqlp/ohos/tools/sdk
export HDC_HOME=/home/zmtzawqlp/ohos/tools/sdk/10/toolchains
export PATH=$PATHHDC_HOME
export PATH=$PATHOHPM_HOME/bin
export PATH=/home/zmtzawqlp/ohos/tools/oh-command-line-tools/sdkmanager/binPATH
```

执行 `ohsdkmgr list`(如果报错 `java` 版本错误,先执行 `sudo apt install openjdk-17-jdk`),你们应该只能看到 9,8,7.  我们这里直接去官方下载 `10`  的 sdk。

![DM_20231115094712_011.jpg](https://forums-obs.openharmony.c ... fu9i6sd9s6bsi56.jpg "DM_20231115094712_011.jpg")

[每日构建 | OpenHarmony CI](http://ci.openharmony.cn/workbench/cicd/dailybuild/dailylist)

![DM_20231115094712_012.jpg](https://forums-obs.openharmony.c ... k1j4qrczmdjtj5f.jpg "DM_20231115094712_012.jpg")

去找 `4.0` 以上的 `sdk`,我这边下载的是 http://download.ci.openharmony.c ... ll_4.0-beta2.tar.gz

下载之后以这个层级放

![DM_20231115094712_013.jpg](https://forums-obs.openharmony.c ... sspswctpgbfcvgp.jpg "DM_20231115094712_013.jpg")

至于原因,因为配置里面默认设置

![DM_20231115094712_014.jpg](https://forums-obs.openharmony.c ... 3csc9swe3ew3bzs.jpg "DM_20231115094712_014.jpg")

分别进入 `sdk/10/ets/build-tools/ets-loader` 和  `sdk/10/js/build-tools/ace-loader` 执行`npm install`。这是因为对于不是通过 `ohsdkmgr` 安装的 `sdk` ,需要我们手动执行。[如何替换full-SDK (openharmony.cn)](https://docs.openharmony.cn/page ... dk-switch-guide.md/) 中有提到。

## 编译引擎

![DM_20231115094712_015.jpg](https://forums-obs.openharmony.c ... c2etenc76kz8ecw.jpg "DM_20231115094712_015.jpg")

把 `sdk` 中 `native` 文件夹复制到引擎文件夹的 `ndk/linux/4.0` 下面,文档中少说了 `linux`,但我看了下 `ohos.sh` 中的配置,应该是 `engine/ndk/linux/4.0`

![DM_20231115094712_016.jpg](https://forums-obs.openharmony.c ... x3tv8r573mt8coz.jpg "DM_20231115094712_016.jpg")

在执行 `make` 命令之前,去下面的文件中

![DM_20231115094712_017.jpg](https://forums-obs.openharmony.c ... j8nt5jstj098880.jpg "DM_20231115094712_017.jpg")

找到 `default_warning_flags` 和 `default_warning_flags_cc` 定义的地方,加上 `-Wno-sign-compare`。

如果不加这个的话编译会失败,报错信如下:

```
--sysroot=/home/zmtzawqlp/ohos/flutter/engine/ndk/linux/4.0/native/sysroot -Wstring-conversion -Wnewline-eof -O0 -g2 -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-private-field -Wnon-virtual-dtor -Wvla -Woverloaded-virtual -Wno-comments -g3 -ggdb3 -fno-rtti -fno-exceptions -Wimplicit-fallthrough -fno-strict-vtable-pointers -O3 -fvisibility=hidden -fvisibility-inlines-hidden -std=c++17 -fno-rtti -nostdinc++ -nostdinc++ -fvisibility=hidden -fno-exceptions -c ../../third_party/dart/runtime/bin/socket_base_posix.cc -o obj/third_party/dart/runtime/bin/gen_snapshot_dart_io_product.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/zmtzawqlp/ohos/flutter/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/zmtzawqlp/ohos/flutter/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/zmtzawqlp/ohos/flutter/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.
[47/7889] CXX obj/third_party/dart/runtime/bin/gen_snapshot_dart_io_product.sync_socket.o
ninja: build stopped: subcommand failed.
xargs: sh: exited with status 255; aborting
make: *** [Makefile:16: ohos] Error 124
```

问了 `ChatGPT` ,这是因为 `C++` 编译中配置了 `-Werror` 将警告当作错误抛出。可以增加 `-Wno-sign-compare` 来忽略该警告。

在错误日志里面,确实能看到 `-Werror` 参数。

```
--sysroot=/home/zmtzawqlp/ohos/flutter/engine/ndk/linux/4.0/native/sysroot -Wstring-conversion -Wnewline-eof -O0 -g2 -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-private-field -Wnon-virtual-dtor -Wvla -Woverloaded-virtual -Wno-comments -g3 -ggdb3 -fno-rtti -fno-exceptions -Wimplicit-fallthrough -fno-strict-vtable-pointers -O3 -fvisibility=hidden -fvisibility-inlines-hidden -std=c++17 -fno-rtti -nostdinc++ -nostdinc++ -fvisibility=hidden -fno-exceptions -c
```

试了下给编译选项加上 `make` 命令加上 `-Wno-sign-compare`,依然报错。看来选项应该在某个地方被重写了。通过日志里面这些编译选项` -Wstring-conversion -Wnewline-eof -O0 -g2 -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-private-field -Wnon-virtual-dtor -Wvla -Woverloaded-virtual -Wno-comments -g3 -ggdb3 -fno-rtti -fno-exceptions -Wimplicit-fallthrough -fno-strict-vtable-pointers -O3 -fvisibility=hidden -fvisibility-inlines-hidden -std=c++17 -fno-rtti -nostdinc++ -nostdinc++ -fvisibility=hidden -fno-exceptions -c`,很快就定位到了  `engine/src/build/config/compiler/BUILD.gn` 这个文件。

修改之后,到 `engine` 文件夹下面执行 `make`。

对了,如果编译的过程中出现下面的错误,记得先把`代理关闭`了。

```
The compiler crashed: SocketException: Connection timed out (OS Error: Connection timed out, errno = 110), address = 127.0.0.1, port = 39562
#0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:694:35)
#1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1855:26)
#2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
#3      Socket._startConnect (dart:io-patch/socket_patch.dart:2078:22)
#4      Socket.startConnect (dart:io/socket.dart:763:21)
#5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2466:20)
#6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2906:12)
#7      _HttpClient._getConnection (dart:_http/http_impl.dart:2911:12)
#8      _HttpClient._openUrl (dart:_http/http_impl.dart:2766:12)
#9      _HttpClient.openUrl (dart:_http/http_impl.dart:2604:7)
#10     _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1021:42)
#11     WebSocket.connect (dart:_http/websocket.dart:320:22)
#12     vmServiceConnectUri (package:vm_service/vm_service_io.dart:32:44)
#13     _currentHeapCapacity (package:compiler/src/common/ram_usage.dart:27:41)
<asynchronous suspension>
#14     currentHeapCapacityInMb (package:compiler/src/common/ram_usage.dart:38:20)
<asynchronous suspension>
#15     compile.compilationDone (package:compiler/src/dart2js.dart:1067:12)
<asynchronous suspension>
#16     main (package:compiler/src/dart2js.dart:1424:3)
<asynchronous suspension>
```

最后成功之后,`ohos_debug_unopt_arm64` 和 `ohos_release_arm64` 便是编译出来的产物。

![DM_20231115094712_018.jpg](https://forums-obs.openharmony.c ... l3rlyat8dvt81k6.jpg "DM_20231115094712_018.jpg")

## 下载 Flutter 配置环境

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

在 `.bashrc` 最后添加以下配置,保存,并且执行`source .bashrc` 使之生效。

```
export PATH=/home/zmtzawqlp/ohos/flutter/flutter_flutter/binPATH
export PATH=/home/zmtzawqlp/ohos/flutter/flutter_flutter/bin/cache/dart-sdk/binPATH
export PATH="$PATH":"$HOME/.pub-cache/bin"
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
```

## 鸿蒙签名

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

![DM_20231115094712_019.jpg](https://forums-obs.openharmony.c ... ebs1ewsvdwm15bx.jpg "DM_20231115094712_019.jpg")

### autosign

进入 `developtools_hapsigner/autosign`,执行 命令 `chmod 777 *.sh`

* 在 `.bashrc` 最后添加以下配置,保存,并且执行`source .bashrc` 使之生效。

`export SIGN_TOOL_HOME=/home/zmtzawqlp/ohos/tools/developtools_hapsigner/autosign`

* 增加 `profile_tmp_template.json`
  内容如下, 其中  `{{ohosId}}` 为你等会创建的 `Flutter` 应用的名称。

```
{
    "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`

![DM_20231115094712_020.jpg](https://forums-obs.openharmony.c ... wween9jte0j11id.jpg "DM_20231115094712_020.jpg")

### hapsigntool

进入 `developtools_hapsigner/hapsigntool` 执行 `gradle build` 或者 `gradle jar`。注意 `该工具基于Gradle 7.1编译构建,请确认环境已安装配置Gradle环境,并且版本正确`。

前面安装库已经让下载 `gradle 7.1` 了,这里还需要注意的 `gradle7.1需要jdk11,ohsdkmgr 必须jdk17+`.用完切回 `17` 。

```
# 编译jar 用 11
sudo apt install openjdk-11-jdk
# 鸿蒙的 ohsdkmgr 命令要用 17, 等后面编译了 jar 之后再切换到 17
# sudo apt install openjdk-17-jdk
```

确保 `hap-sign-tool.jar ` 生成

![DM_20231115094712_021.jpg](https://forums-obs.openharmony.c ... rsshd6w1cswupdz.jpg "DM_20231115094712_021.jpg")

## 检查

执行 `flutter doctor -v `, 跟 `Flutter` 和 `OpenHarmony` 应该都要 `ok` 才行。

```
zmtzawqlp@localhost:~$ flutter doctor -v
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[!] Flutter (Channel master, 0.0.0-unknown, on Ubuntu 22.04.2 LTS 5.15.90.1-microsoft-standard-WSL2, locale C.UTF-8)
    ! Flutter version 0.0.0-unknown on channel master at /home/zmtzawqlp/ohos/flutter/flutter_flutter
      Cannot resolve current version, possibly due to local changes.
      Reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install.
    • Upstream repository https://gitee.com/openharmony-sig/flutter_flutter.git
    • FLUTTER_GIT_URL = https://gitee.com/openharmony-sig/flutter_flutter.git
    • Framework revision 2a83a1a976 (29 hours ago), 2023-09-23 09:43:51 +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/zmtzawqlp/ohos/tools/sdk, available api versions has [10]
    • hdc version 1.3.0
    • ohpm version 1.2.0
    • signTool location:/home/zmtzawqlp/ohos/tools/developtools_hapsigner/autosign

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 14.0.0-1ubuntu1.1
    • cmake version 3.22.1
    • ninja version 1.10.1
    • pkg-config version 0.29.2

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[!] Proxy Configuration
    • HTTP_PROXY is set
    ! NO_PROXY is not set

[✓] Connected device (1 available)
    • Linux (desktop) • linux • linux-x64 • Ubuntu 22.04.2 LTS 5.15.90.1-microsoft-standard-WSL2

[✓] HTTP Host Availability
    • All required HTTP hosts are available
```

## 创建编译项目

`flutter create --platforms ohos flutter_ohos`

![DM_20231115094712_022.jpg](https://forums-obs.openharmony.c ... wbdveolamdwjfaa.jpg "DM_20231115094712_022.jpg")

看到 `ohos` 文件夹是不是跟看到 `ios` 和 `android` 一样亲切。

`flutter_ohos` 是项目的名字,记得把 `profile_tmp_template.json` 中的
`{{ohosId}}` 替换成 `com.example.flutter_ohos`

### `.npmrc` 文件

直接运行会报错,会报找不到 `.npmrc` 文件,你需要去 `/home/zmtzawqlp/.npmrc` 创建 `.npmrc` 文件。

![DM_20231115094712_023.jpg](https://forums-obs.openharmony.c ... bcwdgd8bahd989d.jpg "DM_20231115094712_023.jpg")

并且增加以下内容,([配置开发环境-HarmonyOS应用开发](https://developer.harmonyos.com/ ... 0000001052902427-V3))

![DM_20231115094712_024.jpg](https://forums-obs.openharmony.c ... 8szr9gbtktt22tj.jpg "DM_20231115094712_024.jpg")

```
### 仓库配置,支持配置多个地址,使用英文逗号分割
# registry=https://repo.harmonyos.com/ohpm/
registry=https://repo.huaweicloud.com/repository/npm/
@ohos:registry=https://repo.harmonyos.com/npm/
## 支持配置含有group的仓库,优先匹配
#@group1:registry=
#@group2:registry=

### 代理配置
# no_proxy=
# http_proxy=
# https_proxy=

### 证书校验配置
## 是否校验https仓库的证书,取值:true|false, default: true, 为true时需要配置ca_files证书路径
# strict_ssl=true
# #ca_files=

# ### 发布配置
# ## 因为registry支持配置多仓,所以此处需明确指定发布仓
# publish_registry=https://repo.harmonyos.com/ohpm/
# ## 用户发布三方库的发布id,从云端个人中心获取
# #publish_id=
# ## 用户私钥文件路径,公钥需在云端进行配置
# #key_path=

# ### 缓存路径, 默认在用户目录下的.ohpm/cache
# #cache=

# ### 日志级别, 取值:debug|info|warn|error, default: info
# log_level=info

# ### 网络请求超时时间, 单位ms, 默认为60s
# fetch_timeout=60000
```

### 执行编译命令

执行 `flutter build hap --target-platform ohos-arm64 --release --local-engine-src-path /home/zmtzawqlp/ohos/flutter/engine/src --local-engine ohos_release_arm64` 或者 `flutter build hap --target-platform ohos-arm64 --debug --local-engine-src-path /home/zmtzawqlp/ohos/flutter/engine/src --local-engine ohos_debug_unopt_arm64`

![DM_20231115094712_025.jpg](https://forums-obs.openharmony.c ... 432xgs3900p3zsx.jpg "DM_20231115094712_025.jpg")

生成产物的地方跟 `Flutter` 的不一样,不是在 `build` 下面。而是在 `flutter_ohos/ohos/entry/build/default/outputs/default/entry-default-signed.hap`

![DM_20231115094712_026.jpg](https://forums-obs.openharmony.c ... pkoovzfieoo8kpv.jpg "DM_20231115094712_026.jpg")

## `.bashrc`  最终配置

大家在开始的时候,可以先把下面的内容都放进去 `.bashrc` 中,然后根据自己的用户名,以及下载 `sdk`,`engine` 的位置做对应改变。

```
# flutter
export PATH=/home/zmtzawqlp/ohos/flutter/flutter_flutter/binPATH
export PATH=/home/zmtzawqlp/ohos/flutter/flutter_flutter/bin/cache/dart-sdk/binPATH
export PATH="$PATH":"$HOME/.pub-cache/bin"
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn


# depot_tools
export PATH=/home/zmtzawqlp/ohos/flutter/depot_tools:$PATH

# grade
export PATH=/home/zmtzawqlp/ohos/tools/gradle-7.1/bin:$PATH

# ohos
export SIGN_TOOL_HOME=/home/zmtzawqlp/ohos/tools/developtools_hapsigner/autosign
export OHPM_HOME=/home/zmtzawqlp/ohos/tools/oh-command-line-tools/ohpm/
export OHOS_SDK_HOME=/home/zmtzawqlp/ohos/tools/sdk
export HDC_HOME=/home/zmtzawqlp/ohos/tools/sdk/10/toolchains
export PATH=$PATH:$HDC_HOME
export PATH=$PATH:$OHPM_HOME/bin
# nodejs
export NODE_HOME=/home/zmtzawqlp/ohos/tools/node-v14.19.1-linux-x64
export PATH=$NODE_HOME/bin:$PATH
export PATH=/home/zmtzawqlp/ohos/tools/oh-command-line-tools/sdkmanager/bin:$PATH

# 代理
hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
export https_proxy="http://${hostip}:7890"
export http_proxy="http://${hostip}:7890"
export all_proxy="socks5://${hostip}:7890"
```

## 结语

最终安装运行成功。

![1.gif](https://forums-obs.openharmony.c ... ppedccvc9ovcbgd.gif "1.gif")

目前接触下来和听到其他适配鸿蒙的小伙伴的反馈,鸿蒙现在对于开发者来说,就像个毛坯房。 不知道今天下午,是否会听到 `遥遥领先` 的声音,但是不改善开发体验和解决国内厂商合作问题的话,`HarmonyOS Next` 是否真的能成功呢?

欢迎加入 `HarmonyCandies` [QQ群:981630644](https://qm.qq.com/q/ajfsyk2RcA)

我摸了摸我吃灰的 `1520` ,叹了一口气。
![DM_20231115094712_027.jpg](https://forums-obs.openharmony.c ... 3b3ciowficfocxw.jpg "DM_20231115094712_027.jpg")

![DM_20231115094712_028.jpg](https://forums-obs.openharmony.c ... b7hbhilpihdsm74.jpg "DM_20231115094712_028.jpg")

[/md]
作者: zmtzawqlp    时间: 2023-11-15 13:46
[md]大图片,貌似显示不出来,还是 gif 不支持?

[/md]
作者: Kigoal    时间: 2023-11-15 13:46
遥遥领先
作者: junliang    时间: 2023-11-15 13:49
[md]法佬+harmony=法鸿  天下第一

[/md]




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5