Size Optimization Tips for Flutter App

Β· Β· 479 words Β· 3 minute read

1. Use SVG for icons and vector images πŸ”—

2. Use WEBP instead of JPG/PNG πŸ”—

It’s like JPG/PNG but it’s ~25% smaller in size

https://developers.google.com/speed/webp

3. Shake Your Icons πŸ”—

Flutter has the functionality to shake out unused icons from Material, Cupertino, FontAwesome, etc. fonts.

For iOS πŸ”—

If you’re building from Xcode, you can set the TREE_SHAKE_ICONS environment variable

For Android πŸ”—

It would be setting the tree-shake-icons property in the gradle.properties

4. Whatever you use, COMPRESS it πŸ”—

Everybody knows they can and should JPG/PNG compress but how many people know you can also compress SVG and WEBP images?

5. Zip your animations, too πŸ”—

6. Use gzip to minimize everything πŸ”—

For example lang files

final String response = await rootBundle.loadString('assets/en_us.json');
List<int> original = utf8.encode(response);
List<int> compressed = gzip.encode(original);
List<int> decompress = gzip.decode(compressed);
final enUS = utf8.decode(decompress);

You can save sizes using gzip compression

https://abhishekdoshi26.medium.com/compression-algorithm-flutter-f628481cc4e0

7. Use Proguard Rules πŸ”—

Add to the file android/app/build.gradle

android {
  buildTypes {
    getByName("release") {
      // Enables code shrinking, obfuscation, and optimization
      minifyEnabled = true
      // Enables resource shrinking
      shrinkResources = true
      // Enables proguard rules
      proguardFiles(
        getDefaultProguardFile("proguard-android-optimize.txt")),
        "proguard-rules.pro"
      )
    }
  }
}

Add to the file android/app/proguard-rules.pro

## Flutter wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }
-keep class com.google.firebase.** { *; } // uncomment this if you are using firebase in the project
-dontwarn io.flutter.embedding.**
-ignorewarnings

https://www.geeksforgeeks.org/how-to-use-proguard-to-reduce-apk-size-in-android/

8. Obfuscate Your Code πŸ”—

This function gives you both security and reducing size! here is how.

https://proandroiddev.com/obfuscation-is-important-do-you-know-your-options-30b3ef396dfe?gi=a6606339a89b

For Android πŸ”—

Add to the file /android/gradle.properties

extra-gen-snapshot-options=--obfuscate

For IOS πŸ”—

https://newbedev.com/how-to-obfuscate-flutter-apps

9. Use Google Fonts πŸ”—

This package will dynamically download fonts when only used. Not every variant of the font

https://pub.dev/packages/google_fonts

10. Use Cache Image Network πŸ”—

It gives you cache images automatically and gives you some space

https://pub.dev/packages/cached_network_image

11. Enable more aggressive optimizations πŸ”—

Removes unused codes, combines classes to make smaller the code, etc.

android/gradle.properties

android.enableR8.fullMode=true

https://developer.android.com/studio/build/shrink-code#optimization

12. Compress Native Libs πŸ”—

Reduce the download size and the size of the app on devices

android/gradle.properties

android.bundle.enableUncompressedNativeLibs=true

https://stackoverflow.com/questions/56551036/native-libraries-not-found-in-applicationinfo-nativelibrarydir-when-building-app/56551499#56551499

13. Split debug info from your code πŸ”—

That splits debug info from our code and helps us create smaller apk

flutter build appbundle --split-debug-info=/<path-to-export>

14. Deferred Components πŸ”—

Flutter has the capability to build apps that can download additional Dart code and assets at runtime. This allows apps to reduce install APK size and download features and assets when needed by the user.

https://flutter.dev/docs/perf/deferred-components

15. Detect heavy packages and get rid of them if possible πŸ”—

https://flutter.dev/docs/perf/app-size

For Android’s AppBundle πŸ”—

flutter build appbundle --target-platform android-arm64 --analyze-size

For Android’s APK πŸ”—

flutter build apk --target-platform android-arm64 --analyze-size

For iOS πŸ”—

flutter build ios --analyze-size

refs:

Share: