Flutter如何适配鸿蒙Next系统并打HAP安装包

Overview

因最近有媒体报道后期鸿蒙Next系统不支持安卓应用的安装,虽然不清楚真假,但作为开发我们也需要为此做准备。

着手调研使用Flutter打适配鸿蒙Next的HAP包的技术方案后,通过分析和对比最后也确认了开源鸿蒙定制Flutter的方案,这种方案对于Flutter项目来说是一种开放成本最低的方式。

安装适配鸿蒙系统的Flutter

关于Flutter的配置可以参考开源鸿蒙适配的Flutter中的文档一步步进行配置。

配置成功后如下方所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
zhouwei@FYMacBook-Air ~ % flutter doctor -v
[!] Flutter (Channel master, 3.7.12, on macOS 12.6.9 21G726 darwin-x64, locale zh-Hans-CN)
• Flutter version 3.7.12 on channel master at /Users/zhouwei/Desktop/hm_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 198162b99b (18 hours ago), 2023-11-23 12:38:27 +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: /Users/zhouwei/Documents/sdk, available api versions has [10]
• hdc version 1.3.0
• ohpm version 1.2.0
• signTool location:/Users/zhouwei/Documents/developtools_hapsigner/autosign

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
• Android SDK at /Users/zhouwei/Library/Android/sdk
• Platform android-33, build-tools 33.0.2
• ANDROID_HOME = /Users/zhouwei/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14C18
• CocoaPods version 1.11.3

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.82.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.70.0

[✓] Connected device (2 available)
• macOS (desktop) • macos • darwin-x64 • macOS 12.6.9 21G726 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 119.0.6045.159

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

! Doctor found issues in 1 category.

编译适配鸿蒙系统的Flutter engine

根据适配鸿蒙系统的Flutter版本中的文档配置完后,打HAP包则需要指定--local-engine

如果你发现在执行完flutter doctor -v命令后,在flutter/bin/cache/artifacts/engine目录下没有发现鸿蒙的engine的时候,需要我们手动编译可构建支持在OpenHarmony设备上运行的flutter engine程序

同样我们可以查考可构建支持在OpenHarmony设备上运行的flutter engine程序中的文档进行编译。

需要注意的是我们在编译前需要下载depot_tools

我们可以使用下方命令进行下载(终端需要配置代理):

1
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

所有一切都成功后便可编译HAP包安装到模拟器或手机(Next系统)手机。

适配鸿蒙Next的Flutter项目

参考文献