Sub topic 10
Packaging for Release: Source, AAR, and Frameworks
This chapter assumes you can already launch a Flutter screen from an Android or iOS host, as covered in the integration chapters. It also assumes you know Gradle (Android) or CocoaPods and Xcode build configurations (iOS). Here you decide how the module reaches the native build and what changes when you build for release.
Why packaging is a decision
The Flutter module is not a finished binary. The native host build has to compile it, or it has to receive a prebuilt version of it. That choice affects three things:
- Toolchain: whether every native developer needs the Flutter SDK installed.
- Build time: whether each native build recompiles Dart.
- Ownership: whether the Flutter team ships artifacts that the native team consumes like any other library.
There are two broad approaches. In the source approach, the host build calls into the module's directory and builds it. In the binary approach, you build the module once and hand the native team an Android Archive (AAR) or a set of iOS frameworks.
| Approach | Android | iOS | Needs Flutter SDK on host machines |
|---|---|---|---|
| Source | Gradle includes the module as a subproject | CocoaPods loads the module's `podhelper.rb` | Yes |
| Binary | AAR files in a Maven repository | XCFrameworks, linked manually or through a podspec | No, only on the machine that builds the artifacts |
Android: source integration
With source integration, settings.gradle of the host loads a script that the module generates. The script adds a :flutter subproject. (This example uses the Groovy DSL. The module's .android/include_flutter.groovy can be loaded from Kotlin DSL scripts as well, with different syntax.)
// settings.gradle in the host app
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
google()
mavenCentral()
maven { url 'https://storage.googleapis.com/download.flutter.io' }
}
}
rootProject.name = 'host_app'
include ':app'
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
'my_flutter_module/.android/include_flutter.groovy'
))// app/build.gradle
dependencies {
implementation project(':flutter')
}The module must be a sibling of the host project, or you must change the path. The host build then runs the Flutter Gradle plugin, which invokes the Flutter tool and compiles Dart. This is convenient during development, but every machine and CI runner that builds the host needs a working Flutter SDK at a compatible version.
Android: AAR packaging
An AAR is the standard Android library archive. Flutter can build one for each build mode:
cd my_flutter_module
flutter build aar --build-number 1.0.0Without flags this builds debug, profile, and release. To build only release, pass --no-debug --no-profile. The output is a local Maven repository at build/host/outputs/repo. Inside are artifacts named flutter_debug, flutter_profile, and flutter_release, plus one set per plugin you use.
The host then points at that repository and selects the artifact by build type:
// app/build.gradle in the host app
android {
buildTypes {
profile {
initWith debug
}
}
}
repositories {
maven { url '../my_flutter_module/build/host/outputs/repo' }
maven { url 'https://storage.googleapis.com/download.flutter.io' }
}
dependencies {
debugImplementation 'com.example.my_flutter_module:flutter_debug:1.0.0'
profileImplementation 'com.example.my_flutter_module:flutter_profile:1.0.0'
releaseImplementation 'com.example.my_flutter_module:flutter_release:1.0.0'
}The profile build type is required because Flutter produces a profile artifact. If your host has no such build type, initWith debug creates one. Without it Gradle cannot resolve the profile variant.
The second maven line matters. The Flutter engine itself is hosted on download.flutter.io, and your AAR depends on it. Omit it and you get unresolved-dependency errors for io.flutter:flutter_embedding_*.
For a team workflow, publish build/host/outputs/repo to an internal Maven server such as Artifactory or Nexus. Native developers then add a normal dependency line.
Android release details
- ABIs: Release builds of Flutter support
armeabi-v7a,arm64-v8a, andx86_64. A 32-bitx86ABI is not available in release mode. If the host setsabiFilters, make sure it lists only supported values. - Shrinking: If you enable R8 or ProGuard in the host, the Flutter embedding ships its own consumer rules. Test a minified release build, since stripped classes only show up at runtime.
- Debug and release engines differ: Debug uses a JIT runtime and is much larger and slower. Never ship
flutter_debug.
iOS: CocoaPods source integration
For source integration, the host Podfile loads a helper script that the module generates in its hidden .ios directory:
# Podfile in the host app
flutter_application_path = '../my_flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'HostApp' do
install_all_flutter_pods(flutter_application_path)
end
post_install do |installer|
flutter_post_install(installer) if defined?(flutter_post_install)
endRun pod install afterward. Whenever you change the module's dependencies in pubspec.yaml, run flutter pub get in the module and then pod install again in the host. Xcode runs a script phase that builds the Dart code with the Flutter tool, so the Flutter SDK must be on the path of whoever builds, including CI.
iOS: frameworks
To avoid requiring the Flutter SDK in the host, build XCFrameworks:
cd my_flutter_module
flutter build ios-framework --output=../HostApp/FlutterThis produces Debug, Profile, and Release folders. Each contains:
App.xcframework, which is your compiled Dart code.Flutter.xcframework, which is the engine.- One XCFramework per plugin.
You can narrow the output with --no-debug, --no-profile, or --no-release. You can also add --cocoapods to generate a podspec for the engine instead of a raw Flutter.xcframework.
There are three ways to consume the output:
- Link manually in Xcode. Drag the frameworks into the target under "Frameworks, Libraries, and Embedded Content". Set them to "Embed & Sign" (or "Do Not Embed" for static ones).
- Use a local CocoaPods podspec. With
--cocoapods, addpod 'Flutter', :podspec => '../HostApp/Flutter/Release/Flutter.podspec'and avendored_frameworksentry forApp.xcframeworkand plugins. - Use Swift Package Manager. Recent Flutter versions are adding SwiftPM support. Check the current documentation for your Flutter version, since the details are still changing.
Selecting the right mode per configuration
The frameworks in Debug, Profile, and Release are not interchangeable. In the manual approach, make the search path depend on the Xcode build configuration:
FRAMEWORK_SEARCH_PATHS = $(inherited) "$(PROJECT_DIR)/Flutter/$(CONFIGURATION)"This works only if your Xcode configurations are named Debug, Profile, and Release. If the host uses names such as Staging or AppStoreRelease, add folders with those names or map them in a script. A mismatch causes a debug engine in a store build, or a missing-module error during development.
Version coupling
The module's Dart code, the Flutter engine, and every plugin are built together by one Flutter SDK version. A binary artifact carries its own engine. Two consequences follow:
- If two modules in the same host both embed Flutter, the host can contain two engines unless they come from the same module. This increases the app size and can cause duplicate-symbol errors.
- Upgrading Flutter means rebuilding and re-publishing every artifact. Pin the SDK version, for example with FVM or a CI image, and record it with the artifact version.
Verifying a release build
Before you hand over a binary, check the following:
- Build the artifact with only the release mode, using
--no-debug --no-profilefor AAR or--no-debug --no-profilefor frameworks. - Build the host in its release configuration on a machine without the Flutter SDK, which proves the binary path is self-contained.
- Install on a physical device. Simulators and emulators can hide ABI and signing problems.
- Open a Flutter screen, call a platform channel, and rotate the device. Confirm there are no
MissingPluginExceptionerrors, since plugin registration issues often appear only in release builds. - Compare the app size with and without the module so the team knows the real cost.
Choosing source or binary
Choose source integration when one team owns both the host and the module and everyone already uses Flutter. Hot reload with flutter attach and debugging are simplest, and no publishing pipeline is needed.
Choose binary packaging when the native and Flutter teams are separate, when many apps consume one module, or when CI machines for the host should not install Flutter. The cost is a publishing step and the need to keep versions aligned.
Some teams use both. They use source integration for daily development and binary artifacts for release pipelines and for teams that only consume the module.
Exercise
Your host Android app builds with source integration. The release pipeline now runs on a CI image without the Flutter SDK. Which command do you run in the module, which repository do you add to the host, and why must you also add https://storage.googleapis.com/download.flutter.io?
Show answer
Run flutter build aar (with --no-debug --no-profile if you only need release) in the module on a machine that has the Flutter SDK. This writes a local Maven repository to build/host/outputs/repo. Add that path as a maven { url ... } repository in the host and replace implementation project(':flutter') with releaseImplementation 'group:flutter_release:version' (plus debug and profile entries as needed). The Flutter download repository is needed because the AAR depends on the Flutter engine artifacts, which are hosted there and not in your local repo.
Exercise
An iOS host links frameworks from Flutter/$(CONFIGURATION). The team adds an Xcode configuration called Staging. The build fails with a missing App.xcframework. What is wrong, and what are two ways to fix it?
Show answer
The folder Flutter/Staging does not exist, because flutter build ios-framework only creates Debug, Profile, and Release. Fix it by copying or symlinking the appropriate folder (usually Release) to Flutter/Staging, or by changing the framework search path for the Staging configuration to point directly at Flutter/Release. Either way, make sure the configuration uses the right mode, so a staging build does not accidentally embed the debug engine.