support Click to see our new support page.
support For sales enquiry!

Flutter Tree Shaking, Code Size Reduction & Bundle Optimization: Advanced Guide

Flutter Tree Shaking, Code Size Reduction & Bundle Optimization: Advanced Guide - Banner Image

Mohammad AfsahOct. 23, 2025

App size isn’t just a number — it’s the first impression your users get.
A lighter Flutter app installs faster, loads quicker, and performs better on every device. Whether you’re optimizing an MVP or scaling a production app, every megabyte matters.

In this advanced guide, we’ll explore how to reduce your Flutter app intelligently using techniques like tree shaking, code size reduction, and bundle optimization — complete with real commands, tips, and best practices.

New to Flutter optimization?

Start with our beginner-friendly guide — How to Reduce Flutter App Size by 60%

 


Understanding Tree Shaking in Flutter

Tree shaking is Flutter’s automatic process for removing unused Dart code during release builds. When you compile your app in release mode, the Dart compiler analyzes your codebase, keeps what’s needed, and discards everything else — dramatically cutting size.

How It Works

  • The Dart AOT compiler scans your entire app.
     
  • It identifies which classes, functions, and imports are actually used.
     
  • All other “dead code” is removed.
     
  • The result: a smaller, faster binary.
     

This process happens automatically when you build with:

 flutter build apk --release 

 

💡 Pro Tip: Tree shaking only removes unused Dart code. Assets, fonts, and native libraries are handled separately — so don’t forget to optimize those too.

 


When Tree Shaking Might Miss Code

In rare cases, the compiler may keep extra code because it can’t be sure whether it’s needed. This happens if you:

  • Use reflection or dynamic method calls
     
  • Load code based on string identifiers
     
  • Use certain runtime plugin integrations
     

Whenever possible, call functions and classes directly, not via strings or reflection, so Flutter can safely eliminate unused code.

 


Advanced Code Size Reduction Techniques

1. Compress and Optimize Image Assets

Images are often the biggest contributors to app size.
Before adding them to your Flutter project:

  • Compress images using TinyPNG or ImageOptim.
     
  • Convert PNG/JPEG to WebP for 30–70% smaller files.
     
  • Use SVG or vector assets for icons and illustrations.
     
  • Remove unused assets from your  pubspec.yaml.

 


2. Remove Unused Dependencies

Every package you add increases your binary size.
Audit your project regularly with:

 flutter pub deps

Then remove unused or redundant packages.
Even small cleanup steps can save several megabytes — especially if plugins contain large native libraries.

 


3. Implement Deferred (Lazy) Loading

Not every feature needs to load at startup.
With deferred loading, you can load parts of your app only when needed.

Example:

 import 'settings_page.dart' deferred as settings;

 

Future<void> openSettings() async {

  await settings.loadLibrary();

  Navigator.push(context, MaterialPageRoute(builder: (_) => settings.SettingsPage()));

}

 

This approach keeps your initial app bundle smaller and loads additional code on demand.

⚠️ Test deferred loading carefully — it may not be ideal for every app or plugin.

 


4. Minimize Custom Font Usage

Fonts can add several megabytes to your build.
Use system fonts whenever possible or subset your custom fonts to include only necessary characters.

Try Font Squirrel’s Webfont Generator to create optimized, subsetted fonts.

 


Bundle Optimization Strategies

1. Split by CPU Architecture

Instead of packaging every architecture in one APK, generate smaller APKs per ABI:

 flutter build apk --split-per-abi --release 

This reduces the app size by 30–40% per build.

 


2. Use Android App Bundles (AAB)

Google Play now requires App Bundles (AAB) for publishing.
They automatically serve the right version for each device, eliminating unused resources.

Build with:

 flutter build appbundle --release

App Bundles typically reduce download size by up to 50% compared to universal APKs.

 


3. Enable ProGuard or R8 (Android Only)

For Android builds, enable code shrinking in  android/app/build.gradle :

 minifyEnabled true

shrinkResources true

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 

 

R8 removes unused Java/Kotlin classes from dependencies and compresses your native code further.

⚠️ Test thoroughly — aggressive ProGuard rules may strip classes required by Flutter plugins.

 


4. Use Obfuscation Wisely

Obfuscation helps protect your Dart code and may reduce symbol size slightly:

 flutter build apk --release --obfuscate --split-debug-info=./debug-info 

 

Keep the  debug-info  folder safe — it’s required for symbolizing crash reports.

 


Analyzing Your Build Size

Find out exactly what’s making your app heavy:

 flutter build apk --analyze-size --release 

 

This command generates a JSON file you can open in Flutter DevTools.
You’ll see a detailed breakdown of:

  • Dart code size per package
     
  • Native libraries
     
  • Image and font assets
     
  • Plugin contributions
     

Focus first on the largest items — often large plugins, images, or fonts.

 


Additional Expert Tips

 

Area

Technique

Expected Impact

Images

Convert PNGs WebP

20–70% smaller

Dependencies

Remove unused packages

5–15% smaller

Fonts

Subset fonts

1–5 MB smaller

Native code

Use R8/ProGuard

5–10 MB smaller

Build type

Use --split-per-abi

30–40% smaller per APK

App Bundle

Switch to AAB

Up to 50% smaller downloads

 


Tracking Size Over Time

Optimization isn’t a one-time task — monitor app size each release.

  • Use  flutter build apk --analyze-size  for every production build.
     
  • Set a size budget (e.g., <20 MB for small apps).
     
  • Track size changes in your CI/CD pipeline.
     
  • Investigate any unexpected jumps before release.

 


Quick Reference Commands

# Build optimized release APK

 flutter build apk --release

# Split by architecture (reduces file size)

 flutter build apk --split-per-abi --release

# Build Android App Bundle (for Play Store)

 flutter build appbundle --release

# Analyze build size breakdown

 flutter build apk --analyze-size --release

# Obfuscate Dart code (optional)

 flutter build apk --release --obfuscate --split-debug-info=./debug-info

 


Flutter Optimization Checklist

  • Compress all images and assets
  • Remove unused dependencies and fonts
  • Use deferred loading for heavy features
  • Enable  --split-per-abi  or AAB for release builds
  • Use ProGuard/R8 for Android
  • Analyze size regularly and track in CI
  • Test obfuscation with proper debug info

 


Conclusion

Reducing Flutter app size isn’t just about making it lighter — it’s about delivering a smoother, faster user experience.

By combining tree shaking, bundle optimization, and smart asset management, you can reduce your app size by up to 60% without losing functionality.

Keep optimizing, measure every build, and make your Flutter app as fast and efficient as it looks.

 


Related Reading

0

Leave a Comment