安裝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網(wǎng)址
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 網(wǎng)址的內(nèi)容(寫的很好~)
以下是我一路踩坑填坑的血淚史啦~~~~
首先一個ubuntu20.04容器
1、創(chuàng)建容器 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是對應(yīng)在本機上準備的路徑,比如我在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 映射到容器內(nèi)的**/opt/flutter**下。
后續(xù)進入改容器重新編譯,即輸入以下命令:
~$ docker container start flutter_build
~$ docker exec -it flutter_build bash
flutter_build 就是之前創(chuàng)建時容器的名稱,你可以在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、創(chuàng)建python2環(huán)境
默認的 ubuntu:20.04 使用 Python3,在容器里使用 virtualenv 創(chuàng)建 Python2 環(huán)境。
# virtualenv .env -p python2
# source .env/bin/activate
5、創(chuàng)建 .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 對應(yīng) ${path_to_flutter_sdk_install}/flutter/bin/internal/engine.version,兩者需要一致。如果不指定的話,會下載最新的版本。除非確實需要編譯最新版本的 Engine,否則并不推薦。
我是這樣做的:
進入 /opt/flutter 目錄
$ cd /opt/flutter
然后使用 echo
命令和重定向操作符>
來創(chuàng)建 .gclient
文件并將第一行寫入。
$ echo `solutions = ['>.gclient
然后 ls
,你可以看到目錄下已經(jīng)生成了一個.gclient 文件
在本機對應(yīng)的映射地址處用筆記本打開,直接復(fù)制上去保存即可。
最后,獲取代碼
$ 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是否已經(jīng)運行并成功生成了build.ninja
。你可以通過在out/linux_release_arm64
目錄中運行ls
命令來查看文件是否存在。
2、如果build.ninja
文件不存在,需要運行GN來生成它??梢栽?code>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架構(gòu)的Linux生成Flutter Engine 的發(fā)布版。
3、運行完GN命令后,你應(yīng)該可以在out/linux_release_arm64
目錄下看到build.ninja
文件。這是,可以再次運行Ninja
開始構(gòu)建:
$ 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 += [ "http://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
上述表示已經(jīng)成功地通過了 gn
生成步驟。生成GN文件是構(gòu)建過程的一部分,這些文件指導了下一步的構(gòu)建過程。在這個步驟,你可能看到有關(guān)GOMA的警告,但你可以暫時忽略它,因為它主要用于加速構(gòu)建過程。
接下來,可以運行下面的命令構(gòu)建代碼:
ninja -C out/linux_release_arm64
請注意,構(gòu)建過程可能需要一些時間,并且可能會需要大量的磁盤空間。在構(gòu)建完成后,你應(yīng)該會在 out/linux_release_arm64 目錄下看到你的構(gòu)建結(jié)果。
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.
解決方法:
還是因為環(huán)境中沒有找到pkg-config
,
apt-get update
apt-get install pkg-config
apt-get install libfreetype6-dev
第二步創(chuàng)建一個arm64的容器
1、選擇合適的鏡像
這是我選的鏡像,其他的都嘗試了幾個都有不同程度的bug,有更好的話,請評論推薦哈~~
2、創(chuàng)建Container
首先同樣在本機創(chuàng)建一個文件夾作為映射地址
$ 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
兩個文件
參照網(wǎng)址: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、創(chuàng)建一個示例工程
$ flutter-elinux create demo1
$ cd demo1
4、復(fù)制并打包
在本機上cmd上
$ docker ps -a
$ docker cp ce88db40402c:/ arm64-sysroot
$ tar cvf arm64-sysroot.tar arm64-sysroot
注意打包的路徑,找到 arm64-sysroot.tar 復(fù)制到 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
編譯結(jié)束查看 build/elinux/arm64/release/bundle,這里是 Flutter app 運行所需的所以文件。
$ tree build/elinux/arm64/release/bundle -L 2
后面還在嘗試中~~文章來源:http://www.zghlxwxcb.cn/news/detail-832705.html
相關(guān)資料查詢
[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]文章來源地址http://www.zghlxwxcb.cn/news/detail-832705.html
到了這里,關(guān)于在嵌入式Linux設(shè)備上使用Flutter開發(fā)圖形界面(試水)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!