Mac os上Flutter engine编译

1,867 阅读4分钟

参考文章:

1、怎样的Flutter Engine定制流程,才能实现真正“开箱即用”?-阿里云开发者社区

2、Flutter Engine 编译指北

3、Flutter Framwork

4、Flutter Engine

所需环境

1、git基础命令。

2、可访问外网。

3、Python 2.7。

4、gclient,这是chromium所使用的一个源码库管理的工具,可以很好的管理源码以及对应的依赖,通过gclinet我们可以获取所有的编译需要的源码和依赖。

5、ninja ,编译工具,负责最终的编译工作。

6、gn ****,负责生成 ninja编译需要的build文件,特别像Flutter这种跨多种操作系统平台跨多种CPU架构的,就需要通过gn生成很多套不同的ninja build文件。

开始编译

1、创建一个工作目录,比如一个名为engine的目录。

2、目录下创建一个gclient的配置文件.gclient, 此配置文件的语法可以参见 chromium.googlesource.com/chromium/to…

solutions = [

 {

  "managed": False,

  "name": "src/flutter",

  "url": "git@github.com:BuildAndRelease/engine.git",

  "custom_deps": {

        'src/third_party/skia':"git@github.com:BuildAndRelease/skia.git@8230258098305e6a6b1f5578f4ea8816c95bc772"

  },

  "deps_file": "DEPS",

  "safesync_url": "",

 },

]

3、下载engine源码

engine目录执行 gclient sync,这个步骤比较耗时,不要提前手动kill掉进程。第一次gclient sync 执行完成了,engine/src/flutter为Flutter Engine源码的位置,我们需要手动切换到对应的版本分支,然后再次执行gclinet sync对此版本的依赖重新同步下,此次执行会比首次执行快很多。

4、检出对应flutter engine版本

首先获取我们当前使用的Flutter版本的提交记录。

cat /当前flutter的路径/bin/internal/engine.version

得到v1.0.0版本的提交记录为

7375a0f414bde4bc941e623482221db2fc8c4ab5n

然后将源码同步到上述提交记录上,执行如下命令然后约等待10分钟。

cd /path/to/engine/src/flutter

git reset --hard 3459eb24361807fb186953a864cf890fa8e9d26a

gclient sync --with_branch_heads --with_tags --verbose

之后即可进行编译。

5、构建引擎产物

cd至 engine/src目录

cd /path/to/engine/src/

./flutter/tools/gn --android --android-cpu arm64 --runtime-mode=release

ninja -C out/android_release_arm64

同理你可以按照如上命令构造flutter其他平台,其他CPU架构的引擎产物。

usage: gn [-h] [--unoptimized]

     [--runtime-mode {debug,profile,release,jit_release}] [--interpreter]

     [--dart-debug] [--no-dart-version-git-info] [--full-dart-debug]

     [--target-os {android,ios,linux,fuchsia,win,winuwp}] [--android]

     [--android-cpu {arm,x64,x86,arm64}] [--ios] [--ios-cpu {arm,arm64}]

     [--simulator] [--fuchsia] [--fuchsia-legacy] [--no-fuchsia-legacy]

     [--winuwp] [--linux-cpu {x64,x86,arm64,arm}]

     [--fuchsia-cpu {x64,arm64}] [--windows-cpu {x64,arm64}]

     [--simulator-cpu {x64,arm64}] [--arm-float-abi {hard,soft,softfp}]

     [--goma] [--no-goma] [--xcode-symlinks] [--no-xcode-symlinks]

     [--depot-tools DEPOT_TOOLS] [--lto] [--no-lto] [--clang]

     [--no-clang] [--clang-static-analyzer] [--no-clang-static-analyzer]

     [--target-sysroot TARGET_SYSROOT] 

     [--target-toolchain TARGET_TOOLCHAIN]

     [--target-triple TARGET_TRIPLE]

     [--operator-new-alignment OPERATOR_NEW_ALIGNMENT]

     [--macos-enable-metal] [--enable-vulkan] [--enable-fontconfig]

     [--enable-vulkan-validation-layers] [--enable-skshaper]

     [--no-enable-skshaper] [--always-use-skshaper]

     [--embedder-for-target] [--coverage] [--out-dir OUT_DIR]

     [--full-dart-sdk] [--no-full-dart-sdk] [--ide IDE]

     [--disable-desktop-embeddings] [--build-glfw-shell]

     [--no-build-glfw-shell] [--bitcode] [--stripped] [--no-stripped]

     [--asan] [--lsan] [--msan] [--tsan] [--ubsan]

     

optional arguments:

 -h, --help      show this help message and exit

 --unoptimized

 --runtime-mode {debug,profile,release,jit_release}

 --interpreter

 --dart-debug     Enables assertions in the Dart VM. Does not affect

            optimization levels. If you need to disable

            optimizations in Dart, use --full-dart-debug

 --no-dart-version-git-info

            Set by default; if unset, turns off the dart SDK git

            hash check

 --full-dart-debug   Implies --dart-debug and also disables optimizations

            in the Dart VM making it easier to step through VM

            code in the debugger.

 --target-os {android,ios,linux,fuchsia,win,winuwp}

 --android

 --android-cpu {arm,x64,x86,arm64}

 --ios

 --ios-cpu {arm,arm64}

 --simulator

 --fuchsia

 --fuchsia-legacy

 --no-fuchsia-legacy

 --winuwp

 --linux-cpu {x64,x86,arm64,arm}

 --fuchsia-cpu {x64,arm64}

 --windows-cpu {x64,arm64}

 --simulator-cpu {x64,arm64}

 --arm-float-abi {hard,soft,softfp}

 --goma

 --no-goma

 --xcode-symlinks   Set to true for builds targetting macOS or iOS when

            using goma. If set, symlinks to the Xcode provided

            sysroot and SDKs will be created in a generated

            folder, which will avoid potential backend errors in

            Fuchsia RBE. Instead of specifying the flag on each

            invocation the FLUTTER_GOMA_CREATE_XCODE_SYMLINKS

            environment variable may be set to 1 to achieve the

            same effect.

 --no-xcode-symlinks

 --depot-tools DEPOT_TOOLS

            Depot tools provides an alternative location for

            gomacc in /path/to/depot_tools/.cipd_bin

 --lto

 --no-lto

 --clang

 --no-clang

 --clang-static-analyzer

 --no-clang-static-analyzer

 --target-sysroot TARGET_SYSROOT

 --target-toolchain TARGET_TOOLCHAIN

 --target-triple TARGET_TRIPLE

 --operator-new-alignment OPERATOR_NEW_ALIGNMENT

 --macos-enable-metal

 --enable-vulkan

 --enable-fontconfig

 --enable-vulkan-validation-layers

 --enable-skshaper

 --no-enable-skshaper

 --always-use-skshaper

 --embedder-for-target

 --coverage

 --out-dir OUT_DIR

 --full-dart-sdk    include trained dart2js and dartdevc snapshots. Enable

            only on steps that create an SDK

 --no-full-dart-sdk

 --ide IDE       The IDE files to generate using GN. Use `gn gen help`

            and look for the --ide flag to see supported IDEs. If

            this flag is not specified, a platform specific

            default is selected.

 --disable-desktop-embeddings

            Do not include desktop embeddings in the build.

 --build-glfw-shell  Build the GLFW shell on supported platforms where it

            is not built by default.

 --no-build-glfw-shell

            Do not build the GLFW shell on platforms where it is

            built by default.

 --bitcode       Enable bitcode for iOS targets. On debug runtime

            modes, this will be a marker only.

 --stripped      Strip debug symbols from the output. This defaults to

            true and has no effect on iOS.

 --no-stripped

 --asan

 --lsan

 --msan

 --tsan

 --ubsan

6、最后编译出的引擎产物如下

7、用编译出的引擎打包APP

flutter build apk --target-platform android-arm64 --flavor ************ --release --local-engine-src-path /Users/johnson/Desktop/WorkSpace/engine/src --local-engine=android_release_arm64

可能的坑: 在编译2.0.3引擎的时候可能在运行gclient的时候报stagehand错误,原因是这个三方库的地址改变了,在.gclient文件中加上 'src/third_party/dart/third_party/pkg/stagehand':"git@github.com:BuildAndRelease/stagehand.git@e64ac90cac508981011299c4ceb819149e71f1bd"