Skip to content

Building Flutter Engine from source

Hidenori Matsubayashi edited this page Sep 10, 2022 · 17 revisions

See also:

0. Use pre-build image

You can also download a specific libflutter_engine.so (stable) version from https://github.com/sony/flutter-embedded-linux/releases/latest without building yourself.

However, please note that Flutter Engine is using many third-party software libraries. So you need to build the Flutter Engine yourself by following the steps below if you want to check all software licenses.

1. Install build tools

$ sudo apt install g++-multilib git python3 curl
$ 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

It isn't recommended using the latest version unless you want to build the 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):

See also: https://github.com/flutter/flutter/wiki/Flutter-build-release-channels

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 a version you want to use.

3. Get source files

$ gclient sync

4. Build embedder

$ cd src

arm64 target + debug mode

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

arm64 target + profile mode

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

arm64 target + release mode

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

x64 target + debug mode

$ ./flutter/tools/gn --runtime-mode debug --unoptimized \
                     --embedder-for-target --disable-desktop-embeddings --no-build-embedder-examples \
                     --enable-fontconfig
$ ninja -C out/host_debug_unopt

x64 target + profile mode

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

x64 target + release mode

$ ./flutter/tools/gn --runtime-mode release \
                     --embedder-for-target --disable-desktop-embeddings --no-build-embedder-examples \
                     --enable-fontconfig
$ 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

clang_x64/gen_snapshot

If you want to cross-build for arm64 on x64, you need to use out/linux_*_arm64/clang_x64/gen_snapshot later. See also: https://github.com/sony/flutter-embedded-linux/wiki/Building-Flutter-apps#cross-build-for-arm64-targets-on-x64-hosts