Removing unused resources requires unused code shrinking to be turned on

21,065

Solution 1

Resource shrinking works only in conjunction with code shrinking. After the code shrinker removes all unused code, the resource shrinker can identify which resources the app still uses. This is especially true when you add code libraries that include resources—you must remove unused library code so the library resources become unreferenced and, thus, removable by the resource shrinker

To enable resource shrinking, set the shrinkResources property to true in your build.gradle file (alongside minifyEnabled for code shrinking). For example:

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

reference

Solution 2

Maybe you set by mistake minifyEnabled = false and shrinkResources = true in your buildTypes.debug, so, maybe, it is a root of a problem, not your buildTypes.release

Solution 3

You might want to refer to the Android Documentation to shrink your code and resources:

Shrink your code and resources

Like a comment already pointed out, resource shrinking only works when you have used the code shrinker. To enable shrinkResources in your build.gradle file you must have first set minifyEnabled to true

Solution 4

android {
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
        }
    }
}

Solution 5

Simple just open the build.gradle file on App level i.e. android/app/build.gradle and implement this:

 release {
        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
        useProguard true
        minifyEnabled false
        shrinkResources false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 

    }
Share:
21,065

Related videos on Youtube

ZarNi Myo Sett Win
Author by

ZarNi Myo Sett Win

Software Engineer at Capps Solutions Learning Java, Kotlin, Android, and iOS Development........ Love Nature, Animals, Travelling & Driving The best person for you is?

Updated on July 09, 2022

Comments

  • ZarNi Myo Sett Win
    ZarNi Myo Sett Win over 1 year

    I am preparing to release an App to production. So, I generated signed apk. After generating signed apk, I was getting a problem. My apk file size is a little large and I tried ways to shrink the apk size. I already tried

    app --> Refactor --> Remove Unused Resources

    and it is not too reduce. So, I added shrinkResources true in my build.gradle(app)

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

    After adding shrinkResources true and I got below error when I rebuild. My question is how should I turn on unused Code shrinking first? Thanks and appreciating.

    enter image description here

    • ישו אוהב אותך
      ישו אוהב אותך about 5 years
      probably minifyEnabled true
    • ZarNi Myo Sett Win
      ZarNi Myo Sett Win about 5 years
      thanks, that fixed me.
    • Maaz Patel
      Maaz Patel about 5 years
      With technical stuff, shrinkResources works only when you have set minfyEnabled to true
  • Muganwas
    Muganwas over 3 years
    I've got minifyEnabled true but still getting the error
  • Vasanth
    Vasanth over 3 years
    @Muganwas same here, I'm also facing same problem, how you resolved it.
  • Muganwas
    Muganwas over 3 years
    @Vasanth I don't think I did, I must have just given up.
  • Limbo
    Limbo over 2 years
    For me, it was the order of the codes, changing it to ``` release { // Caution! In production, you need to generate your own keystore file. // see reactnative.dev/docs/signed-apk-android. signingConfig signingConfigs.debug minifyEnabled enableProguardInReleaseBuilds minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" }``` cleared all errors
  • Rohit Patil
    Rohit Patil almost 2 years
    minifyEnabled has to be true if you are willing to apply shrinkResources true