Sub topic 2
Where your app's bytes come from
This chapter assumes you can build and run a Flutter app, edit pubspec.yaml, and use the flutter command line. You should also know that debug, profile and release builds differ. Here you learn what a Flutter app is made of, so you can tell which part to work on before you measure anything.
Two sizes: download and install
"App size" can mean two different numbers, and mixing them up leads to wrong conclusions.
- Download size is the compressed data a user fetches from the store. It drives conversion on slow or metered connections.
- Install size is the space the app takes on the device after it is unpacked. It matters for devices with little storage.
Both are affected by the same contents, but not equally. Compressible data such as text, JSON and code shrinks a lot in the download. Data that is already compressed, such as PNG, JPEG and MP3 files, barely shrinks. A change that cuts install size by 5 MB may cut download size by much less.
The store also changes what the user receives. A universal Android APK contains code for every CPU architecture. An Android App Bundle lets Google Play send only the parts one device needs. The next chapters cover that. Here, the point is that the file you build and the bytes a user downloads are not always the same.
The four main contributors
A release Flutter app has four main kinds of content:
- The Flutter engine.
- Your compiled Dart code, including the Dart code of your packages and the framework.
- Assets: images, fonts, data files and shaders.
- Native code and resources from the platform: plugin libraries, Java or Kotlin code, Android resources, and iOS frameworks.
The Flutter engine
The engine is the C++ runtime that draws pixels, runs the Dart VM runtime pieces, handles text layout, input and platform channels. Every Flutter app ships its own copy. On Android it is libflutter.so. On iOS it is Flutter.framework.
This is the fixed cost of using Flutter. A minimal "hello world" release app is already a few megabytes per architecture because of it. You can't remove it, and you can't shrink it by editing your own code. Treat it as a baseline: if your app is 25 MB, the part you control is the amount above that baseline.
The engine is also per architecture. A universal APK carries one copy for each CPU type it supports, so the same engine bytes appear several times.
Compiled Dart code
In release and profile builds, Dart is compiled ahead of time (AOT) to machine code. On Android this becomes libapp.so. On iOS it becomes App.framework. The compiled output includes:
- your own code,
- the Flutter framework code you actually reach (widgets, rendering, animation),
- the code of every package you use that your app can reach.
The compiler removes code it can prove is unused. This is called tree shaking. It works on reachability, so a package that you import and call from main() stays, even if you use one function. Large generated code, such as JSON serializers for hundreds of models, also adds up, because every generated class is reachable.
Like the engine, this library is built per architecture. Release builds also embed some symbol information unless you split it out, which a later chapter covers.
Assets
Everything you list under flutter: in pubspec.yaml is copied into the app. On Android this is assets/flutter_assets/ inside the APK. On iOS it is inside App.framework or the app bundle's flutter_assets folder.
flutter:
assets:
- assets/images/
- assets/data/catalog.json
fonts:
- family: Brand
fonts:
- asset: assets/fonts/Brand-Regular.ttf
- asset: assets/fonts/Brand-Bold.ttf
weight: 700Assets are not compiled or tree shaken, with one exception: the build tool tree shakes icon fonts (see below). A 3 MB PNG in your assets folder adds roughly 3 MB, because PNG is already compressed. Note that an entry like assets/images/ includes every file in that folder, including ones you forgot about. Assets often account for the largest share of an app you control, so look at them early.
Other things that count as assets:
- Fonts. Each weight and style is a separate file. Full-language fonts with thousands of glyphs can be several megabytes each.
- Icon fonts. The Material icon font is large, but release builds trim it to the icons your code references. This is why the build log can show a message such as "tree-shaken icons". Dynamic icon lookups (building an
IconDatafrom a variable code point) can defeat this, and the build then keeps more of the font or stops with an error unless you opt out. - Shaders and Lottie or animation files. Both are bytes you shipped, not code.
- Package assets. A package can bring its own images or fonts. They land in your app even though you never opened them.
Native code and platform resources
Flutter is embedded in a normal Android or iOS app, so that app's usual contents apply.
On Android these include classes.dex (the Java and Kotlin code of your app and of every plugin's Android side), res/ resources, and AndroidManifest.xml. Plugins often include native libraries as well, such as .so files for image processing, databases or ML runtimes. They are per architecture, like the engine. One plugin with a large native library can outweigh all of your Dart code.
On iOS, plugins add frameworks or static libraries, and the same idea applies.
Where each part lives
| Contributor | Android location | iOS location | Mainly driven by |
|---|---|---|---|
| Flutter engine | `lib/<abi>/libflutter.so` | `Flutter.framework` | Fixed cost, multiplied by the number of architectures |
| Compiled Dart | `lib/<abi>/libapp.so` | `App.framework` | Your code, packages, generated code |
| Assets | `assets/flutter_assets/` | `flutter_assets` in the bundle | What you list in `pubspec.yaml` |
| Plugin native code | `lib/<abi>/*.so`, `classes.dex` | Frameworks in the bundle | Plugins you depend on |
| Platform resources | `res/`, manifest | Storyboards, asset catalogs, `Info.plist` | Native project setup |
Looking inside an APK yourself
An APK is a ZIP file, so you can list its contents without special tools. This gives you a first rough view before you use the proper measurement tools in the next chapter.
flutter build apk
unzip -l build/app/outputs/flutter-apk/app-release.apk | sort -nr | head -n 15The first column is the uncompressed size in bytes. The largest entries are usually libflutter.so and libapp.so, one set per architecture, followed by classes.dex and any large assets.
This listing has limits. It shows uncompressed sizes, not what the download costs. It also shows only files, so you can see that libapp.so is big but not which packages or classes make it big. Finding that out is the job of --analyze-size.
Common mistakes
- Optimizing the wrong part. Developers often spend hours on Dart code when the app's largest items are two image folders and a font family. Check the breakdown first.
- Forgetting the multiplier. A fat APK contains the engine and Dart library once per architecture. Reducing code by 1 MB saves about 1 MB per architecture in that APK, but a user's device downloads only one of them when you ship an app bundle.
- Counting "unused" assets as free. A file listed in
pubspec.yamlships even if no code loads it. Listing a whole folder makes this easy to miss. - Adding a package for one function. Reachable code in a package can pull in more code, and plugins can add native libraries on top.
- Comparing numbers from different builds. An APK, an app bundle's download size and an iOS install size are different measurements. Compare like with like.
A useful mental model
Split your app's size into three buckets:
- Fixed: the engine and framework baseline. You can only multiply or divide it, through the architectures you ship.
- Shipped by choice: assets, fonts and native libraries. These are the usual quick wins.
- Grown over time: Dart code, packages and generated code. These creep up with every feature and dependency.
Most size reduction work moves bytes between these buckets or removes items from the second and third. The rest of this tutorial follows that order: measure, then trim by bucket.
Exercise
A universal release APK for your app is 48 MB. Listing it with unzip -l shows three folders under lib/, each with a libflutter.so and a libapp.so, plus a 12 MB assets/ folder of PNG files. Which parts are duplicated per architecture, which part is already compressed, and what is your first step to find out what to trim?
Show answer
The engine (libflutter.so), the compiled Dart code (libapp.so) and any plugin .so files are duplicated once for each architecture folder. The PNG assets are already compressed, so they shrink little in the download and only removing or resizing them helps. The first step is to build a release app with flutter build apk --analyze-size and look at the per-component breakdown. This shows whether Dart code, native libraries or assets are the largest contributor before you change anything.