Reducing App Size in Flutter Mobile App Development.

Thilina Weerasinghe
6 min readJun 5, 2022

We developed an android application with Flutter. But surprisingly the debug application size is really larger when compare with native developed android applications. So we search way to reduce app size and tried to reduce size of mobile application. So Finally we found some solutions to solve this problem. And we reduced app size. This article is about what are the methods and techniques, that we can follow to reduce application size.

If you are a mobile application developer, you definitely concern about app size and you always trying to build small size applications and use size reduction techniques. Size reduction becomes one of the important and essential processes when it comes to making app available for users in 4G and modern smartphone era because people prefer smaller apps and many users avoid and they think twice before downloading any application which is too large. According to a report, most of smartphone users have limited data plans, So they don’t like to spent their data package on a single application. When coding and after all the coding part we need to do optimization and try to reduce the app size.

There are two dimensions to measure app size

The download size-The size of the app on play store (when downloading)

install size- This can see after installing the app. This is what happens when you download the app, unpack it, compile and optimise it. This expands the application significantly and can be two or three times or even more the install size.

Download Size

When a user goes to install an app from on play store, he sees the loading bar and then the actual app size. And according to google there are 2.59 million apps on play store and no wonder convincing a user to install your app has never been more difficult.. The larger the app, the greater the chances of a failed/cancelled download. As developers, we want to get the app size small so that we get as much distribution for our app as possible.

Install Size

This has a similar phenomenon. Users fill their phones with videos, audios, images, they will try to install a new app and oops no space for it. They will go hunting for space, go to settings, check which app consumes more space. Even while trying to download from the play store, it suggests they remove some apps to accommodate the new one and even give them suggestions.

There are many good coding practices and building a stable architecture will help the app to perform better. there are some common methods and practices that you can implement in your app to make it better.

Compress PNGs and JPGs

Whenever you have to use images in-app like say a walkthrough screen and you don’t want to call the images from an external host, you should compress your PNGs and JPGs as those of high quality will bloat up the app size. You can do a quick google search for tools to compress your PNGs and JPGs.

Use network images

In Flutter, developers use images from the assets folder. This will be useful for loading images faster. But when you store these images in app assets add more weight to the app. The solution is to use network images. Upload the images in a permanent storage path like Firebase storage, AWS and use the link to that image in your code. This method also help you to reduce app size.

Cache

If you used network images cache is very important thing, cache will not help in reducing app size but it will make the app to load faster it improving the app performance. All frequently used images like profile pic, bg picture using cached_network_image plugin

The cached_network_image package allows you to use any widget as a placeholder. It can you find in pub.dev.

Use .svg format icons

We can get couple of benefits using vector drawables. One doesn’t need to worry about different device DPI’s and this also helps in reducing apk size.

When you are downloading system app icons from Google’s Material Design Icon Library, download .svg format instead of .png .svg images helps you to reduce app size.

Use Specific Libraries

Remove all the un-used packages in pubspec.yaml file. Once done building your app, you should check your pubspec.yaml and remove libraries that are not used and remove all unused assets from pubspec.yaml.

Use Google Fonts

Fonts are very important essential thing in app. They are heavy and are stuck in the app. You need fonts for giving the best user experience for the users and it will help you to build the app beautifully. Instead of bundling fonts in-app, you may want to use the google_fonts package. With the google_fonts package, .ttf or .otf files do not need to be stored in your assets folder and mapped in the pubspec.yaml file. Instead, they can be fetched once via HTTP at runtime, and cached in the app’s file system. This is similar to downloadable fonts in native android development. You can check the google font package on pub.dev. And there you can find lots of various fonts from Google fonts.

Use Proguard

Proguard is a java program optimizer. It optimizes your code in ways that don’t change the functionality but change the representation to make it more compact. It obfuscates name of types, fields, methods where the original name doesn’t matter such that long names are replaced with short strings like a and b for efficiency. Packages and classes may have a long name but should not hinder efficiency. It also removes unused java code from dependencies. To set up proguard, ensure the build type in your <app dir>/android/app/build.gradle in similar to what is shown below:

buildTypes {

release {

minifyEnabled true // add this

proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’ // add this

signingConfig signingConfigs.release // this is default for release

}

}

In that same directory, create the file proguard-rules.pro and add the code below:

## 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

You should also go to your gradle.properties and add the snippet below to it:

extra-gen-snapshot-options= — obfuscate

Resource Shrinking

This extends the concept of proguard’s dead code elimination to resources. Your build type in your build.gradle file in <app dir>/android/app directory(where you added the snippet for proguard) should look like this now:

buildTypes {

release {

minifyEnabled true // added previously

shrinkResources true // add this

proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’ // added previously

signingConfig signingConfigs.release // added previously

}

}

Size analysis tools

Flutter new versions included size analysis tool 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

Delete unnecessary things

Analyzing and optimizing the most extensive files and folders is quite effective.

To Test the release android app, run the following command on the terminal.

flutter build apk --split-per-abi

You will get three apk files from the build/app/output/apk/release folder. You can test the v7 version of the apk file.
If you want to publish the app in the google play store, don’t upload any of the following files.

Use an app bundle file that Google recommends.

To generate an app bundle, run the following command on the terminal.

flutter build appbundle --target-platform android-arm,android-arm64,andro

After that, you will get an .aab file in the build/app/output/appbundle/release folder.
Now you can upload this .aab file to the google play store.

After doing all the pieces of stuffs explained in the article we can reduced the total app size. Try these good practices to develop good app with flutter.

--

--