skip to Main Content

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


  1. try this command

    flutter build apk --release
    
    Login or Signup to reply.
  2. 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 the buildTypes section for release:

    android {
        ...
        buildTypes {
            release {
                ...
                minifyEnabled true
                shrinkResources true
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    

    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:

    android {
        ...
        splits {
            abi {
                enable true
                reset()
                include 'armeabi-v7a', 'arm64-v8a', 'x86_64'
                universalApk false  // If true, also generate a universal APK that includes all architectures.
            }
        }
    }
    

    Then, build the APKs using the following command:

    flutter build apk --split-per-abi
    

    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:

    android.enableR8=true
    

    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:

    flutter build apk --obfuscate --split-debug-info=/<project-name>/<directory>
    

    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:

    flutter build apk --release --split-per-abi --obfuscate --shrink
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search