Skip to content

Building Flutter Engine from source

Hidenori Matsubayashi edited this page Jun 4, 2021 · 17 revisions

See also:

1. Install build tools

$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
$ export PATH=$PATH:$(pwd)/depot_tools

Python 2

Python 2 is required to build. If you default installation is Python 3 you could workaround this by using virtualenv:

$ virtualenv .env -p python2
$ source .env/bin/activate

See also: https://github.com/dart-lang/sdk/wiki/Building#python-2

2. Create .gclient file

When using the latest version of Flutter Engine

solutions = [
  {
    "managed": False,
    "name": "src/flutter",
    "url": "https://github.com/flutter/engine.git",
    "custom_deps": {},
    "deps_file": "DEPS",
    "safesync_url": "",
    "custom_vars" : {
      "download_android_deps" : False,
      "download_windows_deps" : False,
    },
  },
]

When using a specific version of Flutter Engine

You can check the current engine version (commit id / SHA):

You can also get the engine version from ${path_to_flutter_sdk_install}/flutter/bin/internal/engine.version of the Flutter SDK which you are currently using.

solutions = [
  {
    "managed": False,
    "name": "src/flutter",
    "url": "https://github.com/flutter/engine.git@FLUTTER_ENGINE",
    "custom_deps": {},
    "deps_file": "DEPS",
    "safesync_url": "",
    "custom_vars" : {
      "download_android_deps" : False,
      "download_windows_deps" : False,
    },
  },
]

Note: Replace FLUTTER_ENGINE with the commid it of the Flutter engine you want to use.

3. Get source files

$ gclient sync

4. Build embedder

$ cd src

arm64 targets with debug mode

$ ./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode debug --unoptimized --embedder-for-target --disable-desktop-embeddings
$ ninja -C out/linux_debug_unopt_arm64

arm64 targets with profile mode

$ ./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode profile --no-lto --embedder-for-target --disable-desktop-embeddings
$ ninja -C out/linux_profile_arm64

arm64 targets with release mode

$ ./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode release --embedder-for-target --disable-desktop-embeddings
$ ninja -C out/linux_release_arm64

x64 targets with debug mode

$ ./flutter/tools/gn --runtime-mode debug --unoptimized --embedder-for-target --disable-desktop-embeddings
$ ninja -C out/host_debug_unopt

x64 targets with profile mode

$ ./flutter/tools/gn --runtime-mode profile --no-lto --embedder-for-target --disable-desktop-embeddings
$ ninja -C out/host_profile

x64 targets with release mode

$ ./flutter/tools/gn --runtime-mode release --embedder-for-target --disable-desktop-embeddings
$ ninja -C out/host_release

5. Install embedder library

$ cp ./out/${path to your selected target and mode}/libflutter_engine.so <path_to_cmake_build_directory>

Supplement

You need to install libflutter_engine.so in <path_to_cmake_build_directory> to build. But you can switch quickly between debug / profile / release modes for the Flutter app without replacing libflutter_engine.so by using LD_LIBRARY_PATH when you run the Flutter app.

$ LD_LIBRARY_PATH=<path_to_engine> ./flutter-client <path_to_flutter_project_bundle>

# e.g. Run in debug mode
$ LD_LIBRARY_PATH=/usr/lib/flutter_engine/debug/ ./flutter-client ./sample/build/linux/x64/debug/bundle

# e.g. Run in profile mode
$ LD_LIBRARY_PATH=/usr/lib/flutter_engine/profile/ ./flutter-client ./sample/build/linux/x64/profile/bundle

# e.g. Run in release mode
$ LD_LIBRARY_PATH=/usr/lib/flutter_engine/release/ ./flutter-client ./sample/build/linux/x64/release/bundle
Clone this wiki locally