Skip to content

Building Flutter apps

Hidenori Matsubayashi edited this page Sep 6, 2021 · 17 revisions

This page describes how to build Fluter apps.

1. Installing flutter-elinux

You need to install flutter-elinux. See: flutter elinux install

2. Building Flutter apps

Here introduce how to build the flutter sample app.

Note that you need to build Flutter apps in the same mode(release/debug) libflutter_engine.so was built. It means you need to build Flutter apps in the release mode if you use libflutter_engine.so was built in release mode.

2-1. Self-build

This is the method when the build machine and the target machine are the same. (x64 targets on x64 hosts / for Arm64 targets on Arm64 hosts)

Build in release mode:

$ flutter-elinux create sample
$ cd sample
$ flutter-elinux build elinux

The build result: build/elinux/{cpu-arch}/release/bundle/

Build in debug mode:

$ flutter-elinux create sample
$ cd sample
$ flutter-elinux build elinux --debug

The build result: build/elinux/{cpu-arch}/debug/bundle/

Also, You can do the following steps on both x64 and arm64 hosts. In debug mode, the Flutter bundle artifacts are not architecturally different between x64 and arm64.

$ flutter-elinux build bundle --asset-dir=./bundle/data/flutter_assets
$ cp <path_to_flutter_sdk_install>/bin/cache/artifacts/engine/elinux-common/icu/icudtl.dat ./bundle/data

2-2. Cross-build for Arm64 targets on x64 hosts

See: Cross-building from x64 to arm64

2-3. Cross-build for Arm64 targets on x64 hosts manually

You need to clang_x64/gen_snapshot is one of the artifacts of the Flutter engine you built. See: Building Flutter Engine

Build in release mode:

$ flutter-elinux create sample
$ cd sample

# Path to Flutter SDK
$ export FLUTTER_SDK=/opt/flutter-elinux/flutter

# Package name of the build target Flutter app
$ export APP_PACKAGE_NAME=sample

# The build data.
$ export RESULT_DIR=build/linux-embedded-arm64
$ export BUILD_MODE=release

$ mkdir -p .dart_tool/flutter_build/flutter-embedded-linux
$ mkdir -p ${RESULT_DIR}/${BUILD_MODE}/bundle/lib/
$ mkdir -p ${RESULT_DIR}/${BUILD_MODE}/bundle/data/

# You need to use `gen_snapshot` for cross-building
$ mkdir -p ${FLUTTER_SDK}/bin/cache/artifacts/engine/linux-arm64-release/clang_x64
$ cp <path_to_your_working_directory_to_build_engine>/src/out/linux_release_arm64/clang_x64/gen_snapshot \
  ${FLUTTER_SDK}/bin/cache/artifacts/engine/linux-arm64-release/clang_x64/gen_snapshot

# Build Flutter assets.
$ flutter build bundle --asset-dir=${RESULT_DIR}/${BUILD_MODE}/bundle/data/flutter_assets
$ cp ${FLUTTER_SDK}/bin/cache/artifacts/engine/linux-x64/icudtl.dat \
   ${RESULT_DIR}/${BUILD_MODE}/bundle/data/

# Build kernel_snapshot.
$ ${FLUTTER_SDK}/bin/cache/dart-sdk/bin/dart \
  --verbose \
  --disable-dart-dev ${FLUTTER_SDK}/bin/cache/artifacts/engine/linux-x64/frontend_server.dart.snapshot \
  --sdk-root ${FLUTTER_SDK}/bin/cache/artifacts/engine/common/flutter_patched_sdk_product/ \
  --target=flutter \
  --no-print-incremental-dependencies \
  -Ddart.vm.profile=false \
  -Ddart.vm.product=true \
  --aot \
  --tfa \
  --packages .dart_tool/package_config.json \
  --output-dill .dart_tool/flutter_build/flutter-embedded-linux/app.dill \
  --depfile .dart_tool/flutter_build/flutter-embedded-linux/kernel_snapshot.d \
  package:${APP_PACKAGE_NAME}/main.dart

# Build AOT image.
$ ${FLUTTER_SDK}/bin/cache/artifacts/engine/linux-arm64-release/clang_x64/gen_snapshot \
  --deterministic \
  --snapshot_kind=app-aot-elf \
  --elf=.dart_tool/flutter_build/flutter-embedded-linux/libapp.so \
  --strip \
  .dart_tool/flutter_build/flutter-embedded-linux/app.dill

$ cp .dart_tool/flutter_build/flutter-embedded-linux/libapp.so ${RESULT_DIR}/${BUILD_MODE}/bundle/lib/

The build result: build/linux-embedded-arm64/release/bundle