How to decrease Flutter App Size
When I am building app on release mode I got app size is too large how to decrease app size anybody can explain:
runs this command:
flutter build apk
I am Expecting for reduced app size as minimum as
How to decrease Flutter App Size
When I am building app on release mode I got app size is too large how to decrease app size anybody can explain:
runs this command:
flutter build apk
I am Expecting for reduced app size as minimum as
2
Answers
try this command
To decrease the size of your Flutter app when building it in release mode, you can follow several strategies. Here are some detailed steps to help you reduce your Flutter app’s APK size:
1. Remove Unused Dependencies
Analyze your
pubspec.yaml
and remove any dependencies that are not used in your application. Unused dependencies can bloat your app size unnecessarily.2. Minify and Optimize the Code
Enable code shrinking, minification, and obfuscation by configuring ProGuard for Android builds. This can help remove unused code and reduce the size of the APK.
In your
android/app/build.gradle
, update thebuildTypes
section for release:3. Split the APKs by ABI
By default, Flutter builds a fat APK that includes binaries for multiple architectures (ARM, ARM64, x86). You can split the APKs to target specific architectures, reducing the size of each APK.
In your
android/app/build.gradle
, enable ABI splits:Then, build the APKs using the following command:
This command generates multiple APK files, one for each architecture.
4. Compress PNG Files
Ensure that all your image assets are optimized. You can use tools like ImageOptim or TinyPNG to compress PNG files without losing quality.
5. Use WebP for Images
WebP is a modern image format that provides superior lossless and lossy compression for images on the web. Consider converting your PNG and JPEG images to WebP format.
6. Reduce the Usage of Plugins
Review the plugins you are using in your project. Some plugins may add significant size to your APK. If possible, try to find lighter alternatives or implement the required functionality natively.
7. Remove Unused Resources
Manually remove any unused resources (images, strings, etc.) from your project. This can significantly reduce the size of the final APK.
8. Enable R8
R8 is the code shrinker for Android that replaces ProGuard. It is enabled by default in Flutter, but ensure it is activated in your
gradle.properties
file:9. Use Dart Obfuscation
Obfuscating Dart code can reduce the size of your compiled app by making the code more difficult to reverse engineer, which can also help with reducing the size of the final APK.
Add the following flags to your build command:
10. Remove Debugging Information
Ensure you are building in release mode and not including debugging information which can add to the app size.
Example Build Command
Combining several of the above methods, your build command might look like this:
Note
By carefully managing dependencies, optimizing assets, splitting APKs by ABI, and using tools for code minification and resource shrinking, you can significantly reduce the size of your Flutter APK. Following these best practices ensures that your app is lean and efficient, providing a better user experience with faster download and installation times.