Sub topic 11
Measuring build size with the App Size tool
This chapter assumes you can build and run a Flutter app from the command line and know the difference between debug, profile, and release modes. It also assumes you have pubspec.yaml dependencies to look at and have opened DevTools at least once. Earlier chapters covered the runtime tools. This one covers a different question: what is in the app you ship, and why is it that big?
Why build size matters
Build size affects install conversion on slow or metered connections, update time, and storage on low-end devices. Stores also set limits. Google Play's per-APK and per-bundle limits and Apple's cellular download warnings both depend on size.
A size problem has no runtime symptom. The app runs fine and nothing shows up in the Performance or Memory views. You find it by measuring the artifact, so the App Size tool works on a build output, not on a running app.
What the tool analyzes
The App Size tool reads a JSON file that the Flutter command line produces when you build with --analyze-size. It does not attach to a running process, so you don't need a VM service URI.
Two rules follow from this:
- Analyze a release build. Debug builds include the Dart VM's JIT support, debugging information, and unoptimized code, so their size says little about what users download.
- Analyze a single target architecture on Android. A fat APK contains native code for several CPU architectures, which makes the numbers harder to read.
# Android: one architecture at a time
flutter build apk --analyze-size --target-platform android-arm64
# iOS (build without code signing if you only want the analysis)
flutter build ios --analyze-size --no-codesign
# Desktop targets work the same way
flutter build macos --analyze-size
flutter build windows --analyze-size
flutter build linux --analyze-sizeAfter the build, the terminal prints a short summary of the largest contributors. It also prints the path of a JSON file, which Flutter writes to a .flutter-devtools folder in your home directory. The name looks like apk-code-size-analysis_01.json. The number increments with each build, so earlier analyses are kept.
Opening the analysis in DevTools
Start DevTools on its own and load the JSON file:
dart devtoolsOpen the App Size tab. It has two sub-tabs: Analysis and Diff. In Analysis, drag the JSON file onto the page or use the file picker. The tab is available without a connected app. In some setups it is hidden until you load a file from the standalone DevTools page.
You can also pass the file when launching:
dart devtools --appSizeBase=/path/to/apk-code-size-analysis_01.jsonThis flag opens DevTools in the diff-oriented flow, so use the file picker for a single analysis.
Reading the Analysis tab
The Analysis tab shows the same data in two forms.
Treemap
The treemap draws every part of the build as a rectangle. Area is proportional to size. The root is the whole artifact, and clicking a block drills into its children. A breadcrumb at the top lets you go back up.
A typical Android release build splits into these groups:
| Group | What it contains |
|---|---|
| `lib/arm64-v8a/` | Native libraries: `libapp.so` (your compiled Dart code), `libflutter.so` (the engine) |
| `assets/` | Images, fonts, and other files declared in `pubspec.yaml`, plus `AssetManifest` files |
| `classes.dex` | Java or Kotlin code from the Android embedding and plugins |
| `res/` and `resources.arsc` | Android resources |
Inside libapp.so, DevTools breaks the Dart AOT snapshot down by package and library. A block such as package:intl or package:my_app shows how much compiled code each one adds. You can also see the size of the VM snapshot data and instructions.
libflutter.so is the engine and is a fixed cost. You cannot shrink it with code changes, so skip it when looking for savings.
Dominator tree
The dominator tree is a list view of the same data, sorted by size, with expandable rows. Use the treemap to spot an unexpected block quickly. Use the tree to sort and read exact byte counts.
A reading routine
- Find the largest top-level groups: native code, assets, or Java code.
- If assets dominate, open that group and look for oversize images or fonts.
- If
libapp.sodominates, sort packages by size and ask whether each one is worth its cost. - Compare against what you expect. A package you thought was small can pull in large transitive dependencies.
Comparing builds with the Diff tab
One analysis tells you what is big. A diff tells you what changed, which is the better tool for regressions.
- Build and save the analysis for the baseline, for example on your main branch.
- Make the change, rebuild, and keep the second JSON file.
- In the Diff tab, load the old file as the base and the new file as the test file.
The treemap now shows differences. Colors mark parts that grew and parts that shrank, and the totals show the net change. Drill down to find which package or asset caused it.
Use the same architecture and the same Flutter version for both builds. Otherwise engine changes or a different ABI will drown out your own change.
Since the files are plain JSON, you can keep one per release. Store them as build artifacts in CI and you can diff any two releases later.
Common causes of large builds
These are the findings that turn up most often:
- Large or uncompressed images. Resize to the maximum display size and prefer formats like WebP for photos. Provide resolution variants only when you need them.
- Bundled fonts. A full font family with many weights and a large glyph set adds up. Declare only the weights you use.
- Heavy packages. One package may bring a large set of transitive dependencies. Check how many blocks sit under it in the treemap.
- Icons. Flutter tree-shakes
IconDatafrom icon fonts in release builds, so only the glyphs you use remain. This breaks if you build icons dynamically from non-constant code points. The build then fails with a message about non-constantIconData. Fix the code first. Passing--no-tree-shake-iconsdisables the shaking and makes the size worse. - Debug symbols. Dart AOT builds keep symbol names for stack traces. You can move them to a separate file.
flutter build apk --release \
--analyze-size \
--target-platform android-arm64 \
--split-debug-info=build/symbols--split-debug-info writes the symbol files to the given directory and removes them from the app. Keep those files for every release you ship, because you need them to read stack traces from crash reports later. Adding --obfuscate also renames identifiers and shrinks the output a little more. Obfuscation requires --split-debug-info.
Deferring code
If a large feature is rarely used, you can load it on demand with a deferred import. On Android, Flutter can place deferred libraries in separate downloadable modules through deferred components, which cuts the size of the initial install. That needs extra setup in the Android project and pubspec.yaml. On other platforms, a deferred import still ships in the same binary, so it only changes when the code loads, not how big the download is.
import 'package:flutter/material.dart';
import 'reports.dart' deferred as reports;
class ReportsButton extends StatelessWidget {
const ReportsButton({super.key});
Future<void> _open(BuildContext context) async {
// The library loads on the first call. Later calls return immediately.
await reports.loadLibrary();
if (!context.mounted) return;
Navigator.of(context).push(
MaterialPageRoute<void>(builder: (_) => reports.ReportsPage()),
);
}
@override
Widget build(BuildContext context) {
return FilledButton(
onPressed: () => _open(context),
child: const Text('Open reports'),
);
}
}Measure the result before keeping the change. Deferred loading adds complexity and a loading delay, and it only pays off when the deferred code is large.
Limits and failure modes
- The tool shows contributors, not causes. A big block tells you where the bytes are. You still decide whether the feature is worth them.
- Treemap names can be unclear. Some entries show as generic snapshot or stub names. Use the package-level breakdown instead of trying to map every block to your code.
- Numbers differ between runs of different setups. A change of Flutter version, target architecture, or build flags changes the totals. Keep those fixed when you compare.
- Compression hides some effects. The analyzed size is the artifact on disk. A package or store compresses it, so a 1 MB saving in raw size may be smaller in the real download.
- Debug builds mislead. If your analysis shows a very large
libapp.soor kernel blob, check that you built in release mode.
Where it fits in the workflow
The other tools in this tutorial start from a symptom the user feels, such as jank, a crash, or a slow screen. The App Size tool starts from a number you want to keep low. Treat it as a regression check. Record the size at each release, and run a diff whenever a dependency or large asset is added. As with any fix, verify it: rebuild, reload the analysis, and confirm the block you targeted is smaller.
Exercise
You added a charting package, and your release APK grew noticeably. Describe how you would confirm the package is the cause and measure how much it added, including which build settings must match between the two builds.
Show answer
Build the app before the change and after it, both in release mode with flutter build apk --analyze-size --target-platform android-arm64, using the same Flutter version and the same architecture. Keep both JSON files. Open the App Size tab in DevTools, go to Diff, load the old file as the base and the new file as the test file, and drill into libapp.so to find the charting package and its transitive dependencies. The diff shows the net growth and which blocks account for it. Matching the architecture and Flutter version keeps unrelated differences out of the comparison.
Exercise
Your teammate analyzes a debug build with --analyze-size, sees a 90 MB app, and proposes removing several features. What is wrong with the approach, and what should they do instead?
Show answer
Debug builds include JIT support, debugging information, and unoptimized code, so their size does not match what users download. The analysis is also only useful for release artifacts. They should build in release mode for a single target architecture, load that analysis in the App Size tab, and look for the largest real contributors such as assets, packages, or fonts before deciding to cut features.