Building an Engine environment is necessary for further study of Flutter. Engine is an implementation of the Underlying Flutter (C/C++), including Dart VM, Skia, Vulkan, and other third-party libraries.

1. Environment preparation

Since you need access to Google to pull the source code, you must set up the proxy, including:

  • Git’s HTTP/HTTPS proxy
  • HTTP/HTTPS proxy of the terminal

Only after the proxy is successfully set up, subsequent operations will be successful. Otherwise, network failures may occur. You can go to Baidu/Google to search for the specific Settings of the agent.

Before pulling code, you need to have dependency tools ready.

  • Linux, macOS or Windows, preferably macOS, because macOS supports cross-compiling for both Android and iOS.
  • Git, version control.
  • IDE, official recommendation Android Studio.
  • SSH client, used for Github identity authentication. For details, see Connecting to Github with SSH.
  • Depot_tools, the tool containsgclientCommand.
  • Python and The Java environment.

2, Engine source download

Once the environment is ready, you can download the Engine source code. Start by creating an empty folder called engine, or you could use another name. Then create a.gclient file in that folder with the following contents:

solutions = [
  {
    "managed": False,
    "name": "src/flutter"."url": "[email protected]:<your_name_here>/engine.git"."custom_deps": {},
    "deps_file": "DEPS"."safesync_url": "",},]Copy the code

About the url here, need to pay attention to, if your own making account no forkhttps://github.com/flutter/engine, you will need to first fork, and then use their own account name to replace < your_name_here >. If you don’t want to fork, you can replace

with flutter.

Note: create a file named.gclient, not a file with a.gclient suffix. Once created, the file is not displayed in the folder, but can be viewed through vim.

After the.gclient file is created, go to the empty engine folder and run gclient sync to download the engine source code. This time you need to speed up or access Google is stable, if all goes well (may be a few hours), the directory below.

3, Engine compilation

Let’s look at compiling Engine. To compile Engine, go to the SRC directory and run the following commands:

./flutter/tools/gn --android --runtime-mode=debug
ninja -C out/android_debug -j 6
./flutter/tools/gn --runtime-mode=debug
ninja -C out/host_debug -j 6
Copy the code

The above command compiles artifacts that run on Android on a CPU of Armeabi_v7a.

Check other parameters with the./flutter/tools/gn –help command.

usage: gn [-h] [--unoptimized]
          [--runtime-mode {debug,profile,release,jit_release}] [--interpreter]
          [--dart-debug] [--full-dart-debug]
          [--target-os {android,ios,linux,fuchsia}] [--android]
          [--android-cpu {arm,x64,x86,arm64}] [--ios] [--ios-cpu {arm,arm64}]
          [--simulator] [--fuchsia] [--linux-cpu {x64,x86,arm64,arm}]
          [--fuchsia-cpu {x64,arm64}] [--arm-float-abi {hard,soft,softfp}]
          [--goma] [--no-goma] [--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] [--enable-vulkan]
          [--enable-metal] [--enable-fontconfig] [--enable-skshaper]
          [--enable-vulkan-validation-layers] [--embedder-for-target]
          [--coverage] [--out-dir OUT_DIR] [--full-dart-sdk]
          [--no-full-dart-sdk] [--ide IDE] [--build-glfw-shell] [--bitcode]
          [--stripped] [--no-stripped] [--asan] [--lsan] [--msan] [--tsan]
          [--ubsan]
Copy the code

There are many parameters, let’s look at a few commonly used ones.

  • –unoptimized, whether to optimize performance, default optimization.
  • – the runtime – mode, the mode of the flutter, debug, profile, release, jit_release four options.
  • – target – OS, target system, with android, ios, Linux, fuscia four options. –target-os=android equals –android command, –target-os=ios equals –ios command, and so on.
  • — Android CPU, android running platform,arm, X64,x86, ARM64 four options. If you want to run on an Android emulator, — Android-CPU =x86.Note:Here,armAnd it actually corresponds toarmeabi_v7a. But if you want to compilearmeabi(Google no longer recommends itarmeabi) for your referenceThe compilation of the Flutter Engine refers to northThis article.
  • — Simulator, iOS runs the simulator.
  • Ios-cpu: the ios running platform is arm and ARM64.

By combining the above commands, you can compile products for different CPU types on Android and iOS platforms.

For example, on the Android platform, the CPU type is ARM64 and the running mode is DEBUG.

./flutter/tools/gn --android --runtime-mode=debug --android-cpu=arm64 
ninja -C out/android_debug_arm64 -j 6
./flutter/tools/gn --runtime-mode=debug --android-cpu=arm64
ninja -C out/host_debug_arm64 -j 6
Copy the code

For example, on iOS, the CPU type is ARM64 and the running mode is DEBUG.

./flutter/tools/gn --ios --runtime-mode=debug --ios-cpu=arm64 
ninja -C out/ios_debug_arm64 -j 6
./flutter/tools/gn --runtime-mode=debug --ios-cpu=arm64
ninja -C out/host_debug_arm64 -j 6
Copy the code

4. Operation and IDE support

Once compiled, you can run the Flutter project using the compiled local Engine by running the project with the following command.

flutter run --local-engine-src-path <FLUTTER_ENGINE_ROOT>/engine/src --local-engine=android_debug
Copy the code

One thing to note here is that –local-engine must correspond to a specific CPU type. –local-engine=android_debug_x64 –local-engine=android_debug_x64 –local-engine=android_debug_arm64 and so on, as shown in the following example.

flutter run --local-engine-src-path <FLUTTER_ENGINE_ROOT>/engine/src --local-engine=android_debug_arm64
Copy the code

Of course, it can also be run by packaging APK first and then installing APK.

Formal environment APK

 flutter build apk --release --local-engine-src-path /Users/limeihong/Desktop/engine/src --local-engine=android_release
Copy the code

The debug environment APK

 flutter build apk --debug --local-engine-src-path /Users/limeihong/Desktop/engine/src --local-engine=android_debug
Copy the code

However, the above package APK also requires x64, ARM and ARM_64 platform support. However, if you compile only one or both of ARM, X64, and ARM_64, you must specify a type when you package APK.

flutter build apk --target-platform android-arm --split-per-abi --debug --local-engine-src-path /Users/limeihong/Desktop/engine/src --local-engine=android_debug
Copy the code

If you want to use another type of APK, replace the android-ARM CPU with android-x64 or Android-arm_64.

After the success of the packaging, apk default build the path/app/outputs/apk/debug or build/app/outputs/apk/release, and then can be installed by using the following commands.

flutter install build/app/outputs/apk/debug/xxxx.apk
Copy the code

This will successfully package the compiled Engine as APK only and run it.

Note: formal apK requires a signature file, the default formal package is unsigned, and an unsigned formal package will not run on the phone.

To see how to view source Code with VS Code, first configure VS Code’s C/C++ environment and then install CQuery.

You can install cQuery on macOS with the following command.

brew install --HEAD cquery
Copy the code

Once cQuery is installed, VS Code also needs to install cQuery plug-in support, which is as simple as simply going to the store and searching for CQuery and installing it.

After the IDE configuration is complete. Move the SRC /out/compile_commands.json file to SRC /compile_commands.json.

Finally, open the SRC directory with VS Code to easily read the source Code. Of course, if you want to make some changes to Engine, you can also use the above method to recompile Engine and package it into the installation package.

Note: Due to the amount of stuff in Engine, every time YOU open SRC with VS Code it takes a while to load (several minutes).

【 References 】

Setting up the Engine development environment

Compiling the engine

Build the Flutter Engine source code compilation environment

The compilation of the Flutter Engine refers to north