Flutter Engine 编译与调试

一、环境搭建

1.1 GitHub帐户添加SSH密钥

https://docs.github.com/en/github/authenticating-to-github/checking-for-existing-ssh-keys

1.2 配置ninja

  1. 下载和编译
git clone git://github.com/ninja-build/ninja.git && cd ninja
git checkout release
./configure.py --bootstrap
  1. 配置环境变量
$ 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环境

  1. 下载Engine代码:https://github.com/flutter/engine

  2. 在engine目录下创建.gclient文件,文件中填充以下内容

    solutions = [
      {
        "managed": False,
        "name": "src/flutter",
        "url": "[email protected]:/engine.git",
        "custom_deps": {},
        "deps_file": "DEPS",
        "safesync_url": "",
      },
    ]
    
  3. 在engine目录下执行gclient sync命令,它会下载所有依赖并同步最新代码

  4. 执行以下命令:

    sudo ./build/install-build-deps-android.sh
    sudo ./build/install-build-deps.sh
    sudo ./flutter/build/install-build-deps-linux-desktop.sh
    

二、编译Engine

  1. 在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'
    
  2. 授予运行权限:

    chmod a+x compile.sh
    
  3. 执行compile.sh进行编译

单独编译指定可执行文件的方法:

  1. 准备编译文件
    • ./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.
  2. 使用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指定在什么设备上运行。

你可能感兴趣的:(Flutter,Engine)