compile

steps

  • The installationdepot_toolsAnd add to the environment variable.gclientFrom the depot_tools tool
  • Fork flutter/ Engine (note: configure SSH access)
  • Create an emptyengineDirectory and create in the directory.gclientThe configuration file
  • inengineDirectory executiongclient sync(it willgit cloneEssential items and dependencies)
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH=/path/to/depot_tools:$PATH
mkdir engine
cd engine
touch .gclient

# edit .gclient
gclient sync
Copy the code

The configuration of gitClient is as follows:

solutions = [
  {
    "managed": False,
    "name": "src/flutter",
    "url": "https://github.com/<your_name>/engine.git",
    "custom_deps": {},
    "deps_file": "DEPS",
    "safesync_url": "",
  },
]
Copy the code
  • Switch source code. One important operation before compilation is to switch the source code to the commit point corresponding to the engine version of the native Flutter SDK (a COMMIT ID) to avoid possible errors
Check out the local version of the Flutter SDK engine, This file is contains the corresponding commit id vim SRC/flutter/bin/internal/engine version # adjust engine code CD/SRC/flutter git reset - hard the commit Id > gclient sync-d --with_branch_heads --with_tags # ready build file CD engine/ SRC # flutter 1.12 Generate host_debug_unopt compile configuration using the following command /flutter/tools/gn -- run-time mode debug # flutter 1.17 -- Unoptimized # Android ARM64 (Armeabi-v7a) compiler configuration./flutter/ Tools /gn -- Android -- Unoptimized # Android ARM64 (Armeabi-v8a) /flutter/tools/gn --android --unoptimized -- run-time =debug -- Android -- CPU = ARM64 # out/host_debug_unopt -j 16 ninja -C out/android_debug_unopt -j 16 ninja -C out/android_debug_unopt_arm64 -j 16Copy the code

Pay particular attention to the ninja -c out/host_debug_unopt command. Official documentation mentions it as necessary:

Note that if you use particular android or ios engine build, you will need to have corresponding host build available next to it: if you use android_debug_unopt, you should have built host_debug_unopt, android_profile -> host_profile, etc.

The result of this compilation includes the DART-SDK, which is called when you build your App with the Flutter engine you built.

The compiled directory is as follows:

run

Using a custom engine on the command line:

# Run the Flutter App Flutter run using your local engine --local-engine-src-path <engine path>/src --local-engine=android_debug_unopt_arm64Copy the code

Using a custom engine in the IDE:

# Create a project with a Flutter --org com.yourdomain your_app_name Add the localEngineOut property to your gradle.properties fileCopy the code

Add the localEngineOut property to gradle.properties file and configure it as follows:

localEngineOut=<engine_dir>/out/android_debug_unopt_arm64
Copy the code

Note:

  • The local engine that should be specified should match the Android architecture, such as that used on armeabi-V8a machinesandroid_debug_unopt_arm64
  • Pay particular attention to certain projects that will pass the Flutter SDK themselveslibflutter.soCopy to the code base to integrate the Flutter, which disables the above method, and the actual run time does not load the specified local engine. The solution is to copy the file to the corresponding directory (for exampleandroid_debug_unopt_arm64)libflutter.soOverride already exists in the code baselibflutter.soCan be

Common runtime errors

The most common problem with the Flutter App is that the specified engine cannot be found. Reasons often include:

  • The engine file path is incorrect
  • Architecture mismatch. In the case of my test machine, Huawei Nova 2, it required an ARM64 engine, but I didn’t notice this when compiling, chose arm, and found that the engine architecture didn’t match
  • The lack ofhostProduct. The following error message is displayed

The Mac, of course, cannot execute the Binaries of the Linux platform.

debugging

Debugging the Flutter engine to see step by step how the engine code works is a great way to learn the code of the Flutter engine. Before introducing how to debug the Flutter engine, let’s take a look at some of the debugging scenarios you might encounter during the development of the Flutter:

  1. Debug the Flutter App Dart code
  2. Debug the Flutter SDK Dart code
  3. Debug the Flutter engine Java code
  4. Debug the C++ code of the Flutter engine

The first scenario is as simple as putting a breakpoint on the Dart Code in the Flutter App in VS Code.

The second scenario is simpler. Configure the Dart & Flutter plug-in in VS Code to allow debugging of third-party libraries and the Flutter SDK Dart Code by setting breakpoints in the relevant source Code

Debugging Java code

Consider the third scenario, debugging the Java code in the Flutter engine. Mainly refer to the following information (it is recommended to operate it) :

  • Debugging Android builds with Android Studio

The steps are as follows:

  • The first step is toengine/src/flutter/shell/platform/androidEngineering (calledFlutter engine Engineering) to Android Studio. Pay attention to this directory! In addition, make sure the Android SDK and JDK versions for the project are correct (currently 29 and 8 respectively).
  • Step 2 run the Flutter App using the custom Flutter engine (calledFlutter App project), see above for details
  • The third step,Flutter engine EngineeringSet a breakpoint to the source code and start the Debugger to connect to the started Flutter App process

Debugging C++ code

Finally, let’s look at how to debug the Flutter engine C++ code. The main references are:

  • Flutter Engine c + + source code to debug | asain district
  • Flutter Engine source code debug | xinbao random house