DEV Community

Devashish Datt Mamgain
Devashish Datt Mamgain

Posted on

How to Reduce App size - Flutter ( With easy steps)

Image descriptionMany developers are concerned with the size of their compiled app. As the APK, app bundle, or IPA version of a Flutter app is self-contained and holds all the code and assets needed to run the app, its size can be a concern. The larger an app, the more space it requires on a device, the longer it takes to download, and it may break the limit of useful features like Android instant apps.
Starting in Flutter version 1.22 and DevTools version 0.9.1, a size analysis tool is included to help developers understand the breakdown of the release build of their application. The size analysis tool is invoked by passing the - analyze-size flag when building:
flutter build apk - analyze-size
flutter build appbundle - analyze-size
flutter build ios - analyze-size
flutter build linux - analyze-size
flutter build macos - analyze-size
flutter build windows - analyze-size

Here's how you can reduce your app size through best practices:

Using SVG images: SVGs are vector drawables, which are much smaller in size as compared to JPG and PNG files. With vector drawables, you also do not need to work about different DPI sizes.
Remove unused code and resources from your app: You can remove unused resources from your app by manually deleting files that are not used anywhere in your project. You also check your pubspec.yaml file to remove any unused library.

Using Android Studio: 
 - Right Click Project.
Pick Refactor from the menu.
Select Remove Unused Resources from the menu.

Using Dart Code Metrics:
 dart run dart_code_metrics:metrics check-unused-files lib
Using Network images: In most cases, we use images from the assets folder to display in the app. In development mode, this will be useful as the images are loaded faster. But when you bundle the app these images add more weight to the app. The solution is to use network images. Upload the images in a permanent storage path like AWS or in your website server and use the link to that image in your code. You can then cache these in the local directory and load them every time from here, which will not require you to do a network call every time. One of the libraries you can use is:

https://pub.dev/packages/cached_network_image

Using proguard (Android only): Proguard is a Java application optimization tool. It isn't just for Flutter. It shrinks your code by altering the representation rather than the functionality. When the original name for types, fields, and methods is unimportant, long names are replaced by short strings like a and b to save time. Even though classes and programs may have lengthy names, they should not be ineffective. It also removes all unnecessary Java code from dependencies.

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. We refer to each uniquely downloadable bundle of Dart libraries and assets as a "deferred component." This is achieved by using Dart's deferred imports, which can be compiled into split AOT shared libraries.

For more tech content click here

Top comments (0)