一、环境搭建
1.1 GitHub帐户添加SSH密钥
https://docs.github.com/en/github/authenticating-to-github/checking-for-existing-ssh-keys
1.2 配置ninja
- 下载和编译
git clone git://github.com/ninja-build/ninja.git && cd ninja
git checkout release
./configure.py --bootstrap
- 配置环境变量
$ vi ~/.bashrc
export PATH=$PATH:/home/wangzhen/WorkSpace/ninja
$ source ~/.bashrc
1.3 配置depot_tools
Clone the depot_tools repository:
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
在 ~/.bashrc
添加depot_tools
路径:
export PATH=/path/to/depot_tools:$PATH
1.4 配置Engine环境
下载Engine代码:
https://github.com/flutter/engine
-
在engine目录下创建
.gclient
文件,文件中填充以下内容solutions = [ { "managed": False, "name": "src/flutter", "url": "git@github.com:<your_name_here>/engine.git", "custom_deps": {}, "deps_file": "DEPS", "safesync_url": "", }, ]
在engine目录下执行
gclient sync
命令,它会下载所有依赖并同步最新代码-
执行以下命令:
sudo ./build/install-build-deps-android.sh sudo ./build/install-build-deps.sh sudo ./flutter/build/install-build-deps-linux-desktop.sh
二、编译Engine
-
在engine目录下,创建
compile.sh
脚本,填充以下内容:set -ex cd ~/dev/engine/src/flutter #换成你的代码路径 git fetch upstream git rebase upstream/master gclient sync cd .. flutter/tools/gn --unoptimized --runtime-mode=debug flutter/tools/gn --android --unoptimized --android-cpu=arm64 flutter/tools/gn --android --unoptimized --runtime-mode=debug flutter/tools/gn --android --runtime-mode=profile flutter/tools/gn --android --runtime-mode=release cd out find . -mindepth 1 -maxdepth 1 -type d | xargs -n 1 sh -c 'ninja -C $0 || exit 255'
-
授予运行权限:
chmod a+x compile.sh
执行
compile.sh
进行编译
单独编译指定可执行文件的方法:
- 准备编译文件
-
./flutter/tools/gn --android --unoptimized
for device-side executables. -
./flutter/tools/gn --android --unoptimized --android-cpu=arm64
for newer 64-bit Android devices. -
./flutter/tools/gn --android --android-cpu x86 --unoptimized
for x86 emulators. -
./flutter/tools/gn --android --android-cpu x64 --unoptimized
for x64 emulators. -
./flutter/tools/gn --unoptimized
for host-side executables, needed to compile the code.
-
- 使用
ninja
编译-
ninja -C out/android_debug_unopt
for device-side executables. -
ninja -C out/android_debug_unopt_arm64
for newer 64-bit Android devices. -
ninja -C out/android_debug_unopt_x86
for x86 emulators. -
ninja -C out/android_debug_unopt_x64
for x64 emulators. -
ninja -C out/host_debug_unopt
for host-side executables.
-
三、运行与调试
修改engine代码后,需要在flutter app目录下执行:
flutter run --local-engine-src-path <your_path>/engine/src/ --local-engine=android_debug_unopt_arm64
--local-engine-src-path
指定engine路径,--local-engine
指定在什么设备上运行。