Sub topic 10
Keeping Bundled Assets from Bloating the App
This chapter assumes you can edit pubspec.yaml, build a simple Flutter widget tree, and run flutter pub get. You should also understand basic YAML indentation and know how to load an asset with Image.asset or rootBundle.
An asset is a file packaged with your application. Images, JSON files, audio, videos, and custom font files all add data to the application bundle. Flutter includes every declared asset in the builds for the platforms that use it. A small logo may have little effect, but large photos, several image variants, or a complete font family can increase download size and installation size.
How bundled size grows
Flutter does not automatically know which declared files your application will use. If a directory contains ten images and the application displays only one, all ten can still be included when the directory is declared as an asset directory.
The same rule applies to fonts. If you register regular, bold, italic, and several other font weights, each font file becomes part of the bundle. A font file might be smaller than an image, but many font families can add noticeable size.
| Asset type | Common size risk | Usual response |
|---|---|---|
| Photos | Large pixel dimensions and file size | Resize and compress them |
| Icons | Many duplicate raster files | Prefer an icon font, vector asset, or shared icon set |
| Resolution variants | Several copies of the same image | Keep only the densities you need |
| Custom fonts | Many weights, styles, or large character sets | Register only required variants |
| JSON data | Large static datasets | Remove unused records or fetch data at runtime |
The final size depends on the platform and build format. Android app bundles can deliver different native resources to different devices, while an APK may contain more files in one downloadable artifact. iOS and web builds have their own packaging behavior. Measure the release artifact for each platform you support instead of estimating from the project directory.
Measure before removing files
Use Flutter's size analysis when creating a release build. For example:
flutter build apk --release --analyze-sizeFlutter produces a size analysis file that can be opened with the DevTools app size tool. The report helps separate Dart code, native code, assets, and other parts of the application. It also shows which asset paths contribute the most.
A useful workflow is:
- Build a release version before making changes.
- Record the largest assets and the total artifact size.
- Remove or optimize one category of files.
- Build again with the same target and compare the results.
- Test that the application still displays every required asset.
Do not compare a debug build with a release build. Debug builds include development information and do not represent the size users download.
Keep declarations narrow
A directory declaration is convenient:
flutter:
assets:
- assets/images/However, it can include every file directly inside that directory. If the directory grows to contain old mockups, unused screenshots, and high-resolution originals, those files can silently increase the application size.
Declare individual files when a directory contains mixed content:
flutter:
assets:
- assets/images/logo.png
- assets/data/config.jsonThis makes the asset contract visible and prevents unrelated files from being included accidentally. Keep source files that are not shipped, such as design originals and export files, outside declared asset directories.
An asset directory declaration does not replace good organization. Separate files by purpose:
assets/
images/
logo.png
empty_state.png
data/
config.json
source/
logo-original.psdDeclare only assets/images/ and assets/data/. Keep assets/source/ undeclared.
Optimize images at their real display size
A 4000-by-3000 photograph is wasteful if a screen displays it at 400 by 300 logical pixels. Resize the image before adding it to the application. Compression can also remove metadata and reduce file size while keeping acceptable visual quality.
Use a format suited to the content:
- JPEG works well for photographs.
- PNG works well for transparency and images with sharp, limited-color edges.
- WebP can reduce size for many photographic and graphic images when platform support and project requirements allow it.
- SVG can be useful for simple scalable artwork, but complex SVG files can still be expensive to parse or render.
Do not optimize by changing formats blindly. Compare visual quality on the devices you support and test transparent edges, text inside images, and animation if applicable.
Limit resolution variants
Flutter can select a resolution-aware image based on the device pixel ratio. The base file represents 1.0x, while files in 2.0x and 3.0x directories represent higher-density versions:
assets/images/logo.png
assets/images/2.0x/logo.png
assets/images/3.0x/logo.pngCode still refers to the base path:
Image.asset('assets/images/logo.png')The variants improve sharpness, but each additional copy increases the set of files shipped. Add variants when the image is visible, important, and benefits from extra detail. Do not create large variants for tiny decorative images that are displayed at a few pixels.
Register only the font variants you use
A custom font family can contain regular, bold, and italic files:
flutter:
fonts:
- family: Raleway
fonts:
- asset: assets/fonts/Raleway-Regular.ttf
- asset: assets/fonts/Raleway-Bold.ttf
weight: 700
- asset: assets/fonts/Raleway-Italic.ttf
style: italicEvery listed file is bundled. If the application never uses italic text, remove the italic file and its declaration. If the design uses only regular and semibold text, do not register six unused weights.
Be careful when removing a font variant. Flutter may fall back to another registered font or synthesize a style, which can change the appearance. Search the codebase and theme definitions for fontWeight, FontStyle.italic, and font family names before changing the declaration. Then test screens containing headings, buttons, labels, and long paragraphs.
Fonts from packages also contribute to the application that includes them. A package should avoid shipping large, unrelated font families as part of its default asset contract.
Common size mistakes
- Leaving original design exports inside a declared asset directory.
- Declaring a broad directory when only two files are needed.
- Including large images and scaling them down only at runtime.
- Adding every density variant without measuring the benefit.
- Registering font weights that the interface never uses.
- Measuring only debug builds.
- Removing assets without testing deferred routes or rarely opened screens.
Exercise
An app declares assets/images/, which contains a 12 MB source photograph, a 200 KB logo, and two screen images. The app uses only the logo and screen images. What should you change first, and how should you verify the result?
Show answer
Move the source photograph outside the shipped asset directory and declare the three required files individually. Build a release artifact with --analyze-size, compare its asset contribution with the previous report, and test every screen that uses the remaining images.