Sub topic 14
Keeping the gains: budgets and CI checks
Assume you can run Flutter commands, read pubspec.yaml, and use basic asynchronous Dart code. This chapter focuses on repeatable checks, not on finding the first optimization.
Optimization is only useful if it survives later changes. A new package, larger image, or extra startup task can quietly undo earlier work. A budget turns “the app feels bigger” into a measurable rule.
Choose budgets that match your users
Track at least one size metric and one startup metric. For Android, a split release APK is useful for a direct download estimate. An app bundle is useful for Play Store delivery, but the delivered size depends on the device configuration.
For startup, track time to first frame on a fixed physical device or emulator. Keep the test conditions stable. A debug run on a development phone is not a valid comparison with a release run on a slower test device.
| Metric | Example budget | Measurement conditions |
|---|---|---|
| ARM64 release APK | 25 MB | `--split-per-abi`, release mode |
| App bundle | 35 MB | Release build, bundle artifact |
| Time to first frame | 900 ms | Profile mode, same device and app state |
Set the initial budget slightly above the current measurement. A budget that is already exceeded will be ignored. Review the limit when the product or supported devices change.
Make the check repeatable
A CI job should use the same Flutter version, build mode, target platform, and commands on every run. Save the build outputs and size-analysis JSON as artifacts. Artifacts let you inspect a failed change instead of guessing which dependency caused it.
This example builds an ARM64 release APK and fails when the file exceeds 25 MiB:
#!/usr/bin/env bash
set -euo pipefail
max_bytes=$((25 * 1024 * 1024))
flutter build apk \
--release \
--split-per-abi \
--target-platform android-arm64
apk="build/app/outputs/flutter-apk/app-arm64-v8a-release.apk"
actual_bytes=$(wc -c < "$apk")
printf 'APK size: %d bytes\n' "$actual_bytes"
if (( actual_bytes > max_bytes )); then
printf 'Size budget exceeded by %d bytes\n' "$((actual_bytes - max_bytes))" >&2
exit 1
fiThe check should run after dependency installation and before publishing. A pull request may also run flutter build apk --analyze-size. Store the generated *-code-size-analysis_*.json file so a reviewer can open it in DevTools and inspect Dart libraries, assets, and native code.
Check startup without noisy measurements
Startup measurements vary with compilation, device load, caches, and network state. Do not fail a build because of one unusually slow run. Instead, run several launches and compare the median with a budget. Keep network-dependent initialization out of this test, or stub it.
Use profile mode for a startup trace:
flutter clean
flutter pub get
flutter build apk --release
flutter run --profile --trace-startupThe trace is written to build/start_up_info.json. Inspect it with the Flutter DevTools timeline. Look for long synchronous work before the first frame, such as database setup, large JSON parsing, or service registration.
Apply the rule to every change
When a budget fails, compare the current artifact with the previous successful build. Check recently added packages, assets, fonts, and startup initialization first. Record intentional increases in the budget history rather than raising the limit silently.
Exercise
Why should a CI size check use a release APK instead of a debug APK?
Show answer
Debug builds include development metadata and are not representative of the shipped binary, so their sizes do not provide a useful release budget.