Sub topic 11
Measuring build size with the App Size tool
This chapter assumes you can build and run a Flutter app and understand Dart packages, assets, and release builds. You will use the App Size tool to find what contributes to an Android build and compare builds after a change.
Why build size matters
Build size is the amount of data users download and store when they install your app. A large app can take longer to download, use more storage, and discourage users on limited mobile connections.
Flutter app size usually comes from several sources:
- Compiled Dart code.
- Flutter engine components.
- Images, fonts, and other assets.
- Native Android or iOS libraries.
- Third-party packages.
The size shown by a development build is not a useful release estimate. Debug builds contain extra information and do not use the same optimizations as release builds. Analyze a release artifact instead.
Generate a size analysis
Run this command from the root of your Flutter project:
flutter build appbundle --analyze-sizeFlutter creates a release Android App Bundle and writes a size analysis file. The command prints the file location in its output. You can also analyze APKs:
flutter build apk --analyze-size --split-per-abiThe --split-per-abi option creates separate APKs for different processor architectures. Each APK is smaller because it contains only the native libraries needed by one architecture.
Open DevTools separately:
dart devtoolsIn DevTools, open App Size and load the size analysis JSON file generated by the build. The tool displays a treemap and a breakdown of the artifact. Larger rectangles represent larger contributors.
Read the treemap and breakdown
The treemap helps you locate large sections quickly. The breakdown provides more precise names and categories. Look for:
- Large asset files, such as uncompressed images or videos.
- Packages that add native libraries.
- Duplicate fonts or image variants.
- A sudden increase in compiled Dart code.
- Native libraries included for architectures you do not need.
The App Bundle uploaded to Google Play is not the same as the APK installed on every device. Google Play can deliver a device-specific split from the bundle. Use the bundle analysis to understand what you publish, and ABI-specific APK analysis to investigate a particular installed package.
| Question | Useful artifact or view |
|---|---|
| What will I publish to Google Play? | Release App Bundle analysis |
| What does one Android architecture contain? | ABI-specific APK analysis |
| Which files are largest? | App Size treemap |
| What changed between builds? | Two analyses compared in DevTools |
Find and verify a size regression
Start with a baseline before making a change. Keep its generated analysis file. Then make one relevant change, such as adding a package or importing a large asset, and generate a second analysis.
Load the analyses in the App Size tool and compare the largest categories. A useful investigation follows this order:
- Confirm that both builds use the same Flutter SDK, build mode, target platform, and dependency lockfile.
- Find the category that increased.
- Expand that category until you reach a package, asset, or native library.
- Check whether the increase is expected.
- Remove, compress, resize, or replace the contributor when possible.
- Build again and confirm that the analysis shows the intended decrease.
Do not judge a change from the total number alone. A small total increase may be acceptable if it adds an important feature. A large asset may also be intentional, while an unexpected native library often deserves closer inspection.
Common errors
A size analysis is less useful when the builds differ in ways unrelated to your change. Avoid comparing debug and release builds, different architectures, or builds with different dependencies. Also check whether an asset is packaged because it is listed in pubspec.yaml, even when the application does not currently display it.
Use the App Size tool to measure and explain a build. Use runtime tools such as the Performance view to investigate slow frames. Build size and runtime performance are related only indirectly.
Exercise
You add a package and the release App Bundle grows. What should you inspect first?
Show answer
Load the new and baseline analyses in DevTools, expand the largest changed category, and determine whether the package added Dart code, assets, or native libraries. Then rebuild after any targeted change.