-
Notifications
You must be signed in to change notification settings - Fork 128
Building Flutter apps
This page describes how to build Fluter apps.
You need to install Flutter SDK. See: Installing Flutter SDK
Here introduce how to build the flutter sample app.
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)
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.
$ flutter create sample
$ cd sample
$ flutter build linux
The build result: build/linux/{cpu-arch}/release/bundle/
$ flutter create sample
$ cd sample
$ flutter build linux --debug
The build result: build/linux/{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 build bundle --asset-dir=./bundle/data/flutter_assets
$ cp <path_to_flutter_sdk_install>/bin/cache/artifacts/engine/linux-*/icudtl.dat ./bundle/data
You need to clang_x64/gen_snapshot
is one of the artifacts of the Flutter engine you built. See: Building Flutter Engine
$ flutter create sample
$ cd sample
# Path to Flutter SDK
$ export FLUTTER_SDK=/opt/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
We are also contributing to add cross-building option support. See: https://github.com/flutter/flutter/issues/74929