flutter嵌入式尝试(Windows10)
安装Docker
链接: https://www.bilibili.com/video/BV1qE411N7aC/?spm_id_from=333.880.my_history.page.click&vd_source=0c443477abedebf2fdf7abecef55405d
我是跟着这个视频安装 Docker Desktop for Windows + 启用k8s
k8s网址
https://github.com/AliyunContainerService/k8s-for-docker-desktop
尝试交叉编译
主要步骤参照 https://www.toradex.com/zh-cn/blog/zai-qian-ru-shi-linux-she-bei-shang-shi-yong-flutter-kai-fa-tu-xing-jie-mian 网址的内容(写的很好~)
以下是我一路踩坑填坑的血泪史啦~~~~
首先一个ubuntu20.04容器
1、创建容器 flutter_build或再次进入容器
~$ docker pull ubuntu:20.04
~$ docker run -it -v /home/user/flutter:/opt/flutter --name flutter_build ubuntu:20.04 /bin/bash
上述的 /home/user/flutter是对应在本机上准备的路径,比如我在E:\workspace_E\Desker\flutter_docker新建了一个目录,即如下:
~$ docker run -it -v E:\workspace_E\Desker\flutter_docker:/opt/flutter --name flutter_build ubuntu:20.04 /bin/bash
即将E:\workspace_E\Desker\flutter_docker 映射到容器内的**/opt/flutter**下。
后续进入改容器重新编译,即输入以下命令:
~$ docker container start flutter_build
~$ docker exec -it flutter_build bash
flutter_build 就是之前创建时容器的名称,你可以在Docker看到。
2、安装所需软件
# apt update
# apt upgrade
# apt install clang cmake build-essential pkg-config libegl1-mesa-dev libxkbcommon-dev libgles2-mesa-dev
# apt install libwayland-dev wayland-protocols git curl wget unzip git
# apt install python2
# apt install virtualenv
3、下载编译工具
# mkdir -p /opt/flutter
# cd /opt/flutter
# git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
# export PATH=$PATH:$(pwd)/depot_tools
注意:如果
4、创建python2环境
默认的 ubuntu:20.04 使用 Python3,在容器里使用 virtualenv 创建 Python2 环境。
# virtualenv .env -p python2
# source .env/bin/activate
5、创建 .gclient文件并指定版本
# cat .gclient
solutions = [
{
"managed": False,
"name": "src/flutter",
"url": "https://github.com/flutter/engine.git@bd539267b42051b0da3d16ffa8f48949dce8aa8f",
"custom_deps": {},
"deps_file": "DEPS",
"safesync_url": "",
"custom_vars" : {
"download_android_deps" : False,
"download_windows_deps" : False,
},
},
]
上面的 bd539267b42051b0da3d16ffa8f48949dce8aa8f 对应 ${path_to_flutter_sdk_install}/flutter/bin/internal/engine.version,两者需要一致。如果不指定的话,会下载最新的版本。除非确实需要编译最新版本的 Engine,否则并不推荐。
我是这样做的:
进入 /opt/flutter 目录
$ cd /opt/flutter
然后使用 echo
命令和重定向操作符>
来创建 .gclient
文件并将第一行写入。
$ echo `solutions = ['>.gclient
然后 ls
,你可以看到目录下已经生成了一个.gclient 文件
在本机对应的映射地址处用笔记本打开,直接复制上去保存即可。
最后,获取代码
$ gclient sync
6、编译embbedder
这里编译为arm64 目标 release 模式的 embedder
# cd src
# ./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode release --embedder-for-target --disable-desktop-embeddings --no-build-embedder-examples
# ninja -C out/linux_release_arm64
在这里,报了不少错,以下是解决方法:
1)
(.env) root@0e5187d0fb8b:/opt/flutter/src# ./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode release --embedder-for-target --disable-desktop-embeddings --no-build-embedder-examples
GOMA usage was specified but can't be found, falling back to local builds. Set the GOMA_DIR environment variable to fix GOMA.
Tried to download prebuilt Dart SDK but an appropriate version could not be found!
Either retry by running flutter/tools/download_dart_sdk.py manually or compile from source by setting `--no-prebuilt-dart-sdk` flag to tools/gn
Generating GN files in: out/linux_release_arm64
Traceback (most recent call last):
File "./flutter/tools/gn", line 589, in <module>
sys.exit(main(sys.argv))
File "./flutter/tools/gn", line 581, in main
gn_call_result = subprocess.call(command, cwd=SRC_ROOT)
File "/usr/lib/python3.8/subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/opt/flutter/src/flutter/third_party/gn/gn'
User
(.env) root@0e5187d0fb8b:/opt/flutter/src# ninja -C out/linux_release_arm64
depot_tools/ninja.py: Could not find Ninja in the third_party of the current project, nor in your PATH.
Please take one of the following actions to install Ninja:
- If your project has DEPS, add a CIPD Ninja dependency to DEPS.
- Otherwise, add Ninja to your PATH *after* depot_tools.
解决方法:
$ apt-get update
$ apt-get install ninja-build
(.env) root@0e5187d0fb8b:/opt/flutter/src# ninja -C out/linux_release_arm64
ninja: Entering directory `out/linux_release_arm64'
ninja: error: loading 'build.ninja': No such file or directory
解决方法:
1、首先需要确定GN是否已经运行并成功生成了build.ninja
。你可以通过在out/linux_release_arm64
目录中运行ls
命令来查看文件是否存在。
2、如果build.ninja
文件不存在,需要运行GN来生成它。可以在src
目录下运行如下的GN命令:
./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode release --embedder-for-target --disable-desktop-embeddings --no-build-embedder-examples
这个命令告诉GN为ARM64架构的Linux生成Flutter Engine 的发布版。
3、运行完GN命令后,你应该可以在out/linux_release_arm64
目录下看到build.ninja
文件。这是,可以再次运行Ninja
开始构建:
$ ninja -C out/linux_release_arm64
(.env) root@0e5187d0fb8b:/opt/flutter/src# ./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode release --embedder-for-target --disable-desktop-embeddings --no-build-embedder-examples
GOMA usage was specified but can't be found, falling back to local builds. Set the GOMA_DIR environment variable to fix GOMA.
Using prebuilt Dart SDK binary. If you are editing Dart sources and wish to compile the Dart SDK, set `--no-prebuilt-dart-sdk`.
Generating GN files in: out/linux_release_arm64
ERROR at //build/config/linux/pkg_config.gni:103:17: Script returned non-zero exit code.
pkgresult = exec_script(pkg_config_script, args, "value")
^----------
Current dir: /opt/flutter/src/out/linux_release_arm64/
Command: python3 /opt/flutter/src/build/config/linux/pkg-config.py -s /opt/flutter/src/build/linux/debian_sid_arm64-sysroot -a arm64 freetype2
Returned 1.
stderr:
Traceback (most recent call last):
File "/opt/flutter/src/build/config/linux/pkg-config.py", line 249, in <module>
sys.exit(main())
File "/opt/flutter/src/build/config/linux/pkg-config.py", line 144, in main
prefix = GetPkgConfigPrefixToStrip(options, args)
File "/opt/flutter/src/build/config/linux/pkg-config.py", line 82, in GetPkgConfigPrefixToStrip
prefix = subprocess.check_output([options.pkg_config,
File "/usr/lib/python3.8/subprocess.py", line 415, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/usr/lib/python3.8/subprocess.py", line 493, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'pkg-config'
See //build/config/linux/BUILD.gn:30:1: whence it was called.
pkg_config("freetype2") {
^------------------------
See //build/config/BUILDCONFIG.gn:357:33: which caused the file to be included.
_native_compiler_configs += [ "//build/config/linux:sdk" ]
^-------------------------
解决方法:
$ apt-get update
$ apt-get install pkg-config
$ apt-get install libfreetype6-dev
GOMA usage was specified but can't be found, falling back to local builds. Set the GOMA_DIR environment variable to fix GOMA.
Using prebuilt Dart SDK binary. If you are editing Dart sources and wish to compile the Dart SDK, set `--no-prebuilt-dart-sdk`.
Generating GN files in: out/linux_release_arm64
上述表示已经成功地通过了 gn
生成步骤。生成GN文件是构建过程的一部分,这些文件指导了下一步的构建过程。在这个步骤,你可能看到有关GOMA的警告,但你可以暂时忽略它,因为它主要用于加速构建过程。
接下来,可以运行下面的命令构建代码:
ninja -C out/linux_release_arm64
请注意,构建过程可能需要一些时间,并且可能会需要大量的磁盘空间。在构建完成后,你应该会在 out/linux_release_arm64 目录下看到你的构建结果。
ERROR at //build/config/linux/pkg_config.gni:103:17: Script returned non-zero exit code.
pkgresult = exec_script(pkg_config_script, args, "value")
^----------
Current dir: /opt/flutter/src/out/linux_release_arm64/
Command: python3 /opt/flutter/src/build/config/linux/pkg-config.py -s /opt/flutter/src/build/linux/debian_sid_arm64-sysroot -a arm64 freetype2
Returned 1.
解决方法:
还是因为环境中没有找到pkg-config
,
apt-get update
apt-get install pkg-config
apt-get install libfreetype6-dev
第二步创建一个arm64的容器
1、选择合适的镜像
这是我选的镜像,其他的都尝试了几个都有不同程度的bug,有更好的话,请评论推荐哈~~
2、创建Container
首先同样在本机创建一个文件夹作为映射地址
$ docker run -it -v E:\workspace_E\Desker\torizon\workspace:/opt/workspace torizon/arm64v8-debian-base /bin/bash
3、在容器中安装所需的软件
# apt update
# apt install clang cmake build-essential pkg-config libegl1-mesa-dev libxkbcommon-dev libgles2-mesa-dev
# apt install unzip git
# apt install curl wget
# apt install libwayland-dev wayland-protocols
# apt install libdrm-dev libgbm-dev libinput-dev libudev-dev libsystemd-dev
4、下载 flutter-embedded-linux
代码
# cd /opt/workspace
# git clone https://github.com/sony/flutter-embedded-linux.git
# cd flutter-embedded-linux/
# mkdir build
5、编译生成 flutter-client
和libflutter_elinux_wayland.so
两个文件
参照网址:https://github.com/sony/flutter-embedded-linux/wiki/Building-Embedded-Linux-embedding-for-Flutter
# cd build
# cmake -DUSER_PROJECT_PATH=examples/flutter-wayland-client -DCMAKE_BUILD_TYPE=Release ..
# cmake -DUSER_PROJECT_PATH=examples/flutter-wayland-client -DCMAKE_BUILD_TYPE=Release -DBUILD_ELINUX_SO=ON -DBACKEND_TYPE=WAYLAND -DENABLE_ELINUX_EMBEDDER_LOG=OFF -DFLUTTER_RELEASE=ON ..
# cmake --build .
很好,目前最大的坑来了,只生成了libflutter_elinux_wayland.so
文件,并没有生成flutter-client
文件~
正在找问题在哪里~~
第三步交叉编译
1、回到flutter_build
容器
$ docker exec -it flutter_build bash
2、下载 ``flutter-elinux.
这个是Flutter SDK
$ cd /opt/flutter/
$ git clone https://github.com/sony/flutter-elinux
$ export PATH=$PATH:/opt/flutter/flutter-elinux/bin
运行下面命令查看安装情况
$ flutter-elinux doctor
$ flutter-elinux devices
3、创建一个示例工程
$ flutter-elinux create demo1
$ cd demo1
4、复制并打包
在本机上cmd上
$ docker ps -a
$ docker cp ce88db40402c:/ arm64-sysroot
$ tar cvf arm64-sysroot.tar arm64-sysroot
注意打包的路径,找到 arm64-sysroot.tar 复制到 flutter_build 容器中。
5、解压
$ cd /opt/flutter
$ tar vxf arm64-sysroot.tar
6、编译
$ cd demo1
$ flutter-elinux build elinux --target-arch=arm64 --target-sysroot=/opt/flutter/arm64-sysroot --target-compiler-triple=aarch64-linux-gnu
编译结束查看 build/elinux/arm64/release/bundle,这里是 Flutter app 运行所需的所以文件。
$ tree build/elinux/arm64/release/bundle -L 2
后面还在尝试中~~
相关资料查询
[1] https://github.com/sony/flutter-elinux/wiki/Building-flutter-apps#3-cross-building-from-x64-to-arm64;
[2]https://github.com/sony/flutter-embedded-linux/wiki/Building-Flutter-Engine-from-source
[3]https://github.com/sony/flutter-elinux/issues/140
[4]