This tutorial shows you how to build APK files for Android for applications developed using Flutter.
APK (Application Package File) is a format used by Android operating systems for distribution and installation. After you build an application, it's quite common to build APK files to be tested across different devices. If the application is developed using Flutter, you can easily build APK files by using
flutter build apk command. In this tutorial, I am going to show you how to build APK files for Android using Flutter. This includes how to set the mode (release, debug, or profile), set the main entry point, split per ABI, set target platforms, set the build name and version, build flavor APK, and obfuscate the code.
Build Release APK
By default, if not specified, the generated APK files use release mode.
flutter build apk
The above command is the same as the below command which explicitly uses release mode.
flutter build apk --release
Build Debug APK
For debug mode, you need to add
flutter build apk --debug
Build Profile APK
For profile mode, you need to add
flutter build apk --profile
Set Main Entry-Point File
A Flutter application has a file as the main entry-point. That's the first file to be run when the application is launched (similar to
MainActivity in Android). By default,
lib/main.dart is set to be the main entry-point. If you want to change that, you can pass
--target= flag. The example below changes the entry-point to
flutter build apk --target=lib/home.dart
Split per ABI and Set Target Platforms
Android devices use various CPUs. A large portion of devices uses ARM-based CPUs, while some devices use Intel x86 CPUs. In addition, old devices use 32-bit CPUs, while the majority of devices released in recent years already use 64-bit CPUs. Those different CPUs support different instruction sets. By default, Flutter only generates an APK that works for all platforms. But that causes a problem regarding the build size of the APK. It becomes very big in order to support those different instruction sets.
Fortunately, Flutter already provides an easy way to split the APK files per ABIs. You only need to add
--split-per-abi flag and Flutter will generate multiple APK files, each for a specific platform.
flutter build apk --split-per-abi
The result of the above command will generate the below platform-specific APK files.
Actually, Flutter supports four target platforms:
android-x86 is not included by default. To specify which target platforms you want to include, you can add
--target-platform flag with a comma-separated list of the target platforms as the value. The
--split-per-abi flag is still required if you want to generate multiple APK files for different ABIs. Without
--split-per-abi flag, it will generate a single APK file that's compatible only for the specified platforms.
flutter build apk --target-platform android-arm64,android-arm --split-per-abi
At the time this post was written, Flutter doesn't support to build release version for x86 ABI. You can only build the debug version if you want to create an APK that supports x86 devices.
Set Build Name and Version
The build name and build number of a Flutter application is defined in the
version property of
pubspec.yaml file. The value of
version is a three numbers separated by dots which specifies the build name, followed by an optional build number separated by a '+'. For example,
version: 1.0.1+2 means the build name is 1.0.1, while the build number is 2. The build name is the one that's shown to the users, while the build version is used as an internal version number.
Changing the version name and number can be done by changing the
version property in the
pubspec.yaml. There's an alternative way if you want to build APKs with different version name and number from the value in the
pubspec.yaml file. You can pass
--build-number flags to override the value for build name and build version respectively.
flutter build apk --build-name=1.0.2 --build-number=3
Build Flavor APK
An application can have multiple flavors and Flutter also supports to build APK. You can do it by passing
--flavor flag. For example, if your application has a flavor named 'pro', the command is
flutter build apk --flavor=pro
Split Debug Info
Flutter applications use Dart program symbols. To reduce the application size in release build, you can use
--split-debug--info command, which is used to store the Dart program symbols in a separate file on the host rather than in the application. The flag requires an argument which specifies the directory where program symbol files are stored. By adding the flag, you need to use
flutter symbolize command to obtain a human readable stack trace. The flag cannot be used together with
flutter build apk --split-debug-info build/app/outputs/symbols
Code obfuscation is a technique to make the source code more difficult to be read by human. It's usually used to prevent people from reverse engineering your application. Flutter also supports this feature. You can add
--obfuscate flag, which must be followed by
flutter build apk --obfuscate --split-debug-info build/app/outputs/symbols
pub get Command
By default, the
flutter pub get command will be run every time you run the
build command. To disable it, you can add
flutter build apk --no-pub
If you want to analyze the size of a generated APK file, add
--analyze-size flag in the command. It only works for release mode and you must specify exactly one platform using
flutter build apk --target-platform android-arm64 --analyze-size
Below is the output example of running the above command.
app-release.apk (total compressed) 5 MB
mipmap-xxxhdpi-v4 1 KB
arm64-v8a 5 MB
Dart AOT symbols accounted decompressed size 3 MB
package:flutter 2 MB
dart:core 307 KB
dart:typed_data 217 KB
dart:ui 188 KB
dart:async 114 KB
dart:collection 111 KB
dart:convert 58 KB
dart:isolate 38 KB
dart:io 38 KB
package:vector_math 30 KB
dart:developer 10 KB
typed_buffer.dart 7 KB
priority_queue.dart 5 KB
dart:math 4 KB
dart:ffi 4 KB
main.dart 3 KB
dart:vmservice_io 2 KB
dart:mirrors 697 B
dart:nativewrappers 383 B
Never 63 B
MANIFEST.MF 2 KB
CERT.SF 3 KB
kotlin-stdlib.kotlin_module 1 KB
CERT.RSA 1013 B
flutter_assets 184 KB
reflect 2 KB
collections 1 KB
kotlin.kotlin_builtins 4 KB
resources.arsc 23 KB
AndroidManifest.xml 1013 B
classes.dex 120 KB
That's how to build APK files in Flutter. You can run
flutter build apk --help to get the list of available flags.