Sub topic 1
Measure the right build: debug, profile and release
Prerequisites
This chapter assumes you can run a Flutter app, edit pubspec.yaml, and use the flutter command line tool. You should also know basic Dart and async/await.
Debug, profile and release builds
Flutter has three build modes. Debug builds use just-in-time (JIT) compilation and include assertions and developer hooks, so they are large and slow. Release builds use ahead-of-time (AOT) compilation and strip debug features. Profile builds use AOT like release but keep the tracing hooks that DevTools needs.
Measure size and startup only in profile or release mode. Debug numbers describe a build you will never ship.
flutter run --profile
flutter build apk --release> Note: Use --release for final numbers and --profile when you need timeline data.
Where your app's bytes come from
An app's download size has four main parts:
- The Flutter engine, the native runtime shared by every Flutter app.
- Your compiled Dart code.
- Assets, such as images, fonts and data files.
- Native libraries, including those pulled in by plugins.
Measuring size with --analyze-size and DevTools
Add --analyze-size to a release build to get a size breakdown:
flutter build apk --analyze-size --target-platform android-arm64The build writes a *-code-size-analysis_*.json file. Open it in the size tool in Flutter DevTools to see which packages and files take the most space.
flowchart TD
A[flutter build --analyze-size] --> B[code-size-analysis JSON file]
B --> C[DevTools app size tool]
C --> D[Largest packages and files]caption: Size analysis flow from build to DevTools. alt: A build command produces a JSON file, which the DevTools size tool opens to show the largest packages and files.
Shipping only what each device needs
A single APK contains native code for every CPU architecture (ABI). Split it so each device downloads only its own code:
flutter build apk --split-per-abi
flutter build appbundle--split-per-abi creates one smaller APK per architecture. An Android App Bundle (.aab) lets Google Play deliver device-specific splits automatically, which is usually the better choice for store releases.
| Option | Output | Who picks the right code |
|---|---|---|
| Universal APK | One large file | Nobody, all ABIs included |
| `--split-per-abi` | One APK per ABI | You choose the file to distribute |
| `appbundle` | One `.aab` file | Google Play |
Exercise:
Exercise
Why should you measure app size with a release or profile build instead of a debug build?
Show answer
Debug builds use JIT compilation and developer hooks, so their size and speed do not match what users install. Release and profile builds use AOT compilation, which gives meaningful measurements.