How to decrease app Size in flutter?
App size is important for applications. It has two dimensions: the download size (the size of the app on the play store/when downloading/when installing) and the install size (what happens when you download the app, unpack it, compile it, and optimise it). This significantly expands the application and can be two, three, or even four times the size of the install.
Analyze your app size
It includes native code, assets, and even a breakdown of the compiled Dart code at the package level. We can analyse app size using the following commands for the specific platform.
For Android’s AppBundle,
flutter build appbundle --target-platform android-arm --analyze-size
flutter build appbundle --target-platform android-arm64 --analyze-size
flutter build appbundle --target-platform android-x64 --analyze-size
For Android’s APK,
flutter build apk --target-platform android-arm --analyze-size
flutter build apk --target-platform android-arm64 --analyze-size
flutter build apk --target-platform android-x64 --analyze-size
For iOS,
flutter build ios --analyze-size
For Linux,
flutter build linux --analyze-size
For macOS,
flutter build macos --analyze-size
For Windows,
flutter build windows --analyze-size
DevTools
It can be used to see a visual representation of it.
To activate the devtools run the following command,
flutter pub global activate devtools
To see the visual representation of the app,
flutter pub global run devtools --appSizeBase=apk-code-size-analysis_01.json
Remove unused resources
There are manual and automatic methods for deleting unwanted resources.
You can use manual approaches by deleting the files you don’t require.
Automatic method can be approached by Android studio follow the given steps below :
Right Click Project.
Pick Refactor from the menu.
Select Remove Unused Resources from the menu.
Minimize resource imported from libraries
Once your app has been built, you should review your pubspec.yaml
and delete any unnecessary libraries or packages.
Compress PNG and JPEG files
When we need to use images in-app, such as a tutorial mode screen, and we don’t want to call the images from an external storage, we should compress our PNGs and JPGs because high-quality images will bloat the app size.
Fonts
If we are using more fonts from local assets similar like images these fonts will also increase app size. The best solution is to use google_fonts plugin. This pluign will dynamically download font when it is used.
Proguard (for 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 programmes may have lengthy names, they should not be ineffective. It also removes all unnecessary Java code from dependencies.
We have two default proguard files that we can use,
- proguard-android-optimize.txt — progurard configuration
- proguard-android.txt
The default proguard configuration that we utilised was proguard-android-optimize.txt. In your /app directory’s proguard-rules.pro file, we can add your own proguard configurations.
release {
//Enable the proguard
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), "proguard-rules.pro"
//Other parameters
debuggable false
jniDebuggable false
renderscriptDebuggable false
signingConfig playStoreConfig //Add your own signing config
pseudoLocalesEnabled false
zipAlignEnabled true
}
The proguard will eliminate all unneeded methods, instructions, and classes by setting minifyEnabled to true. file dex. We may reduce the classes by enabling on Proguard in each module of our project. dex file size by roughly half.
Split Bundle (for Android Only)
If we are uploading to the Playstore, we could create an app bundle, or we could split the apk per abi, which splits the apk into x64 and x86 bit code. Using appbundle, Google Play’s new app serving model, called Dynamic Delivery, uses your app bundle to generate and serve optimised APKs for each user’s device configuration, ensuring that they only download the code and resources required to run your app.
flutter build apk --split-per-abi