Sub topic 3
Measuring size with --analyze-size and DevTools
This chapter assumes you can build a Flutter app from the command line and that you know the difference between debug, profile and release builds (covered earlier in this tutorial). You should also be comfortable reading a JSON file path from terminal output and opening a browser-based tool.
Size work starts with a measurement. Guessing which package or asset is heavy usually wastes time. Flutter ships a flag that records exactly what went into a release build, and DevTools can display and compare that record.
What --analyze-size does
--analyze-size is a flag on flutter build. It builds your app in release mode, then inspects the output and attributes bytes to libraries, packages, assets and native code. It prints a summary to the terminal and writes a detailed JSON file for DevTools.
flutter build apk --analyze-size --target-platform android-arm64Some details matter in practice:
- The flag works only for release builds. The build commands that accept it include
apk,appbundle,ios,linux,macosandwindows. Running it in debug or profile mode is not meaningful, because those modes use a different compilation model and include debugging support. --target-platform android-arm64limits the build to one CPU architecture. This keeps the result small and quick to compare. A universal APK includes several architectures, so its total is larger than anything a single user downloads.- The command is slower than a normal build because it does extra analysis.
Reading the terminal output
When the build finishes, the tool prints a tree of the largest contributors. The exact numbers and file names depend on your app, so treat this sample as an illustration of the shape only:
app-release.apk (total compressed) 7 MB
lib/arm64-v8a/libapp.so (Dart AOT) 2 MB
package:my_app 600 KB
package:flutter 900 KB
lib/arm64-v8a/libflutter.so (Flutter Engine) 4 MB
assets/ 500 KBThe tool also prints the location of a JSON file named like apk-code-size-analysis_01.json, and a ready-made command for opening it in DevTools. Keep that file. It is the input for every step that follows.
How the numbers relate to the user's download
The totals are an estimate of the compressed size of the file you built. They are useful for comparing two builds of the same app, but they are not exactly what a user downloads.
- For an app bundle, Google Play creates device-specific APKs from your upload. The download for a given phone is smaller than the bundle.
- For iOS,
--analyze-sizereports on the.appthat the build produces. The App Store applies its own packaging and thinning, so the download size is different again. App Store Connect gives the authoritative figure after upload. - Numbers include the Flutter engine, which is a fixed cost for every app. A "Hello world" app already has a baseline of a few megabytes.
In short, use --analyze-size to find out where bytes go and whether a change helped. Use the store consoles for the final user-facing number.
Opening the result in DevTools
DevTools includes an App Size tool. Launch it with the command the build printed, or write it yourself:
dart devtools --appSizeBase=apk-code-size-analysis_01.jsonYour browser opens DevTools with the App Size tab already showing the file. If you started DevTools another way, open the App Size tab and drop the JSON file onto the page.
The tool has two views:
| View | Purpose | Input |
|---|---|---|
| Analysis | Explore one build and find what is large | One JSON file |
| Diff | Compare two builds and see what grew or shrank | Two JSON files, old and new |
The Analysis view
The Analysis view shows a tree map. Each rectangle is a unit of the output, and its area is proportional to its size. You can click a rectangle to drill into it: the top level shows files in the package (such as libapp.so, libflutter.so and assets), and inside libapp.so you see Dart packages, libraries and classes.
The same data is also shown as an expandable table, with columns for name and size. Sorting by size is the quickest way to find the top few contributors. A tree map is best for getting a sense of proportion, and the table is best for reading exact numbers.
There is also a call graph view for the Dart code. It shows which code references which. This helps when a package looks surprisingly large and you want to know why it was included: follow the edges back to the code in your own app that pulls it in.
The Diff view
Diffing is the most useful part for day-to-day work. Build before and after your change, saving each JSON file under a clear name, then pass both to DevTools:
flutter build apk --analyze-size --target-platform android-arm64
cp ~/.flutter-devtools/apk-code-size-analysis_01.json before.json
# make your change, for example remove a package, then:
flutter build apk --analyze-size --target-platform android-arm64
cp ~/.flutter-devtools/apk-code-size-analysis_02.json after.json
dart devtools --appSizeBase=before.json --appSizeTarget=after.jsonCheck the path printed by your own build. The location and numbering of the JSON file can vary by Flutter version, so copy it from the line the tool prints and do not rely on the paths above.
In the Diff view, red areas grew and blue areas shrank. Entries are labeled with the change in bytes. This answers the question "did my change help, and by how much?" without any guessing.
A repeatable measuring workflow
The following diagram shows the loop used throughout the rest of this tutorial.
Make one change per measurement. If you remove two packages and shrink an image in one step, the diff shows a combined result, and you cannot tell which part helped.
Common mistakes and edge cases
- Measuring a debug build. The flag refuses or gives misleading results outside release mode. A debug APK can be many times larger than a release one, and that number says nothing about what users get.
- Comparing across ABIs. A universal APK contains code for several CPU architectures. Compare it only with another universal APK. The next chapter covers why splitting by ABI matters.
- Ignoring the engine. The
libflutter.soentry is large and you cannot trim it through your code. Focus on the parts you control: Dart code, assets and dependencies. - Trusting small differences. A few kilobytes can come from build metadata and are not worth chasing. Look for changes that stand out in the Diff view.
- Losing the baseline. If you do not save the earlier JSON file, you cannot diff later. Name files with the date or commit hash, and keep them outside
build/soflutter cleandoes not delete them. - Misreading package attribution. Code from a package that is only used indirectly appears under that package's name. A large entry may belong to a transitive dependency (a package that one of your dependencies imports). Use
flutter pub depsto see the dependency tree before blaming your direct imports.
Summary of the workflow
- Build in release mode with
--analyze-sizeand a single--target-platform. - Keep the terminal summary and the JSON file.
- Open the JSON in DevTools and sort by size.
- Change one thing, rebuild, and diff the two files.
- Confirm the final result with the store console.
Exercise
You run flutter build apk --analyze-size --debug to find out why your app is large, and the numbers look huge. What is wrong with this approach, and what should you do instead?
Show answer
Debug builds are not representative of what users install. They include debugging support and use a different compilation model, and --analyze-size is meant for release builds. Run flutter build apk --analyze-size --target-platform android-arm64 (a release build) and analyze that output instead.
Exercise
You removed a package and rebuilt, and you want to see exactly how much each part of the app changed. Which DevTools view do you use, which two inputs does it need, and what command opens it?
Show answer
Use the Diff view of the App Size tool. It needs two JSON files: the baseline from before the change and the new one from after it. Open it with dart devtools --appSizeBase=before.json --appSizeTarget=after.json.
Exercise
A teammate says that the total reported by --analyze-size for your app bundle is exactly the size users will download. Give two reasons this is not guaranteed.
Show answer
Google Play builds device-specific APKs from the bundle, so the real download is usually smaller than the reported total. On iOS, the analysis covers the .app output, while the App Store applies its own packaging and thinning. In both cases the store console gives the authoritative number, and --analyze-size is best used to compare builds.