Search code examples
androiddartflutterproguardminify

How to minify a Flutter app with Proguard?


I already made a Flutter app. The release apk is about 14MB. I searched methods to minify this and found this ons: https://flutter.io/android-release/#enabling-proguard

But my question is, how can I get to know all my used additional libraries for step 1? Are there any commands to know them or is it just all the dependencies that I added to the pubspec.yaml ?

How do I need to implement them in this file /android/app/proguard-rules.pro?


Solution

  • First, we will enable shrinking and obfuscation in the build file. Find build.gradle file which sits inside /android/app/ folder and add lines in bold

    android {
    
        ...
    
        buildTypes {
            release {
                signingConfig signingConfigs.debug     
    
                minifyEnabled true
                useProguard true
    
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    
            }
        }
    }
    

    Next we will create a configuration which will preserve entire Flutter wrapper code. Create /android/app/proguard-rules.pro file and insert inside:

    #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.**  { *; }
    

    Note: Use signingConfigs.release instead signingConfigs.debug to build a apk or appbundle