Sub topic 7
Building an App Bundle and APKs
This chapter covers the two Android artifacts you can produce from a Flutter project: the Android App Bundle (AAB) for Google Play, and the APK for direct installs. It assumes you have already set up the application ID, version, and release signing from the earlier chapters, and that you can run flutter commands in a terminal.
AAB and APK: what each one is
An APK (Android Package) is the file an Android device installs. An AAB (Android App Bundle) is a publishing format that Google Play accepts and devices never install. Play reads the bundle and generates APKs for each device that downloads your app.
| Row | App Bundle (.aab) | APK (.apk) |
|---|---|---|
| Installable on a device | No, not directly | Yes |
| Accepted by Google Play for new apps | Yes, required | No |
| Contains | Code and resources for all ABIs, screen densities, and languages | One build, or one build per CPU architecture |
| Typical use | Play Store upload | Sideloading, internal testing, other stores, CI artifacts |
| Signed with | Upload key | Whatever key you configured |
An ABI (application binary interface) is a CPU architecture such as arm64-v8a. A Flutter app ships compiled Dart code and the Flutter engine as native libraries, so each ABI adds a few megabytes. A device only needs the libraries for its own ABI.
An AAB lets Play deliver only what each device needs. Users get smaller downloads without you maintaining several uploads.
Building an App Bundle
Run this from the project root:
flutter build appbundle --release--release is the default for this command, but writing it makes scripts easier to read. The result is:
build/app/outputs/bundle/release/app-release.aabFlutter runs the Gradle task bundleRelease, which reads your signingConfigs.release block from android/app/build.gradle.kts. If key.properties is missing, the Kotlin snippet from the previous chapter skips loading it. Depending on how the release build type is configured, the build then fails or falls back to a different signing key. Check the output of the first release build, not only the exit code.
Building APKs
A plain release APK contains native libraries for all supported ABIs:
flutter build apk --releaseThis writes build/app/outputs/flutter-apk/app-release.apk. It is a "fat" APK. It installs everywhere, but it is the largest option.
To produce one APK per architecture, add --split-per-abi:
flutter build apk --release --split-per-abiThe output folder then holds files like these:
build/app/outputs/flutter-apk/app-armeabi-v7a-release.apk
build/app/outputs/flutter-apk/app-arm64-v8a-release.apk
build/app/outputs/flutter-apk/app-x86_64-release.apkMost phones made in the last several years use arm64-v8a. Older 32-bit ARM devices use armeabi-v7a. The x86_64 build mostly targets emulators and some Chromebooks.
If you only need one architecture, for example to hand a tester a small file, restrict the build:
flutter build apk --release --target-platform android-arm64The valid values are android-arm, android-arm64, and android-x64.
Setting version and environment values at build time
The version: 1.2.0+7 line in pubspec.yaml sets the default version name and build number. You can override both for a single build without editing the file:
flutter build appbundle --release \
--build-name=1.2.0 \
--build-number=8This is useful in CI, where the build number often comes from a counter provided by the pipeline. The build number becomes Android's versionCode. Google Play rejects an upload whose versionCode it has already seen for that app, even if the earlier upload was never released.
The earlier chapter introduced --dart-define. It combines with the build commands in the usual way:
flutter build appbundle --release \
--build-number=8 \
--dart-define=API_BASE_URL=https://api.example.comThe value is compiled into the app. It is not a secret store, because anyone can extract strings from an APK.
Version codes for split APKs
When you use --split-per-abi, the Flutter Gradle plugin changes the version code of each APK so that the per-ABI files do not collide. It adds an offset based on the architecture to the build number. For example, a build number of 8 can become 1008 for armeabi-v7a and 2008 for arm64-v8a. The exact offsets are an implementation detail of the Flutter Gradle plugin and have changed between releases, so check the installed APK rather than assuming them. To read the real value:
aapt2 dump badging build/app/outputs/flutter-apk/app-arm64-v8a-release.apk | grep versionCodeaapt2 is in the Android SDK build-tools directory.
This matters if you publish split APKs to a store other than Play. Each store expects a higher versionCode for updates, and a device on a 64-bit ARM APK should receive an update that is also 64-bit ARM.
Testing the bundle with bundletool
Devices cannot install an .aab, so how do you check that it works? Use bundletool, Google's command-line tool that does the same splitting Play does. Download bundletool-all-<version>.jar from the bundletool releases page on GitHub, then run these commands:
# Build an .apks archive for the connected device, signed with your keystore
java -jar bundletool.jar build-apks \
--bundle=build/app/outputs/bundle/release/app-release.aab \
--output=app.apks \
--connected-device \
--ks=$HOME/upload-keystore.jks \
--ks-key-alias=upload
# Install the right APKs on the connected device
java -jar bundletool.jar install-apks --apks=app.apks--connected-device tells bundletool to generate only the splits that match the phone attached over adb. Without it you get a universal set for many devices. Bundletool prompts for the keystore password. If you leave out the --ks options, bundletool signs with a debug key, which is fine for local checks.
Testing through bundletool catches problems that a fat APK hides, such as a feature that works only when all ABIs are present.
Inspecting size
A bundle's file size is not what users download. Two ways to see real numbers:
- In Play Console, the App size view of a release shows download size per device configuration.
- Locally,
bundletool get-size total --apks=app.apksreports the estimated download size for the generated APK set.
Flutter also has flutter build apk --analyze-size, which writes a JSON size report you can open in DevTools. It needs a single --target-platform.
Common errors and gotchas
- Signed with the debug key by accident. The default Flutter template's
releasebuild type may use the debug signing config until you change it. Play rejects a debug-signed upload. Verify the signature withkeytool -printcert -jarfile app-release.apk, which works on APKs. For an AAB, usejarsigner -verify -verbose -certs app-release.aab. - "Version code has already been used." Raise the build number and rebuild. A new version name alone does not help.
- Uploading an APK to a new Play app. Play expects an AAB. Build with
appbundle. - Wrong working directory. Run the commands from the project root. Gradle properties and
key.propertiesare found relative to theandroidfolder. - Stale builds. After you change signing or Gradle settings, run
flutter cleanand rebuild. Otherwise cached intermediate output can hide a mistake. - Different results from
--split-per-abiand the fat APK. Behavior should be identical. If it is not, the cause is usually a native plugin that ships libraries for only some ABIs. Test on both 32-bit and 64-bit devices if you still support them.
Which artifact to build
Use this as a decision guide:
- Publishing to Google Play, including testing tracks: build the AAB.
- Sending a build to a tester without Play: build split APKs, and send the
arm64-v8aone unless you know the device is older. - Distributing in a store that requires a single file for all devices: build the fat APK.
- Debugging a problem that appears only in release mode on one device: build a single-ABI APK with
--target-platformand install it withadb install.
Exercise
You run flutter build apk --release --split-per-abi and find three files in build/app/outputs/flutter-apk. A tester has a recent phone and asks which one to install. Which file do you send, and why can't you upload any of these files as a new app to Google Play?
Show answer
Send app-arm64-v8a-release.apk, since recent phones use 64-bit ARM. Google Play requires an Android App Bundle for new apps, so you build one with flutter build appbundle --release and upload build/app/outputs/bundle/release/app-release.aab instead.
Exercise
Your CI pipeline builds an AAB with --build-number=12, and Play Console rejects it. The previous upload, which you never released, also used build number 12. What do you change, and where does the number end up in the Android project?
Show answer
Increase the build number, for example to --build-number=13, and rebuild. Play does not accept a versionCode it has already received for the app, even from an unreleased upload. The build number becomes Android's versionCode.
Exercise
Write the commands to build an AAB for production that sets the version name to 2.0.0, build number to 30, and API_BASE_URL to https://api.example.com. Then name the tool you use to install the bundle on a connected phone.
Show answer
flutter build appbundle --release --build-name=2.0.0 --build-number=30 --dart-define=API_BASE_URL=https://api.example.com. Use bundletool: build-apks with --connected-device to create an .apks archive, then install-apks to install it.