Removing unused resources requires unused code shrinking to be turned on

ZarNi Myo Sett Win picture ZarNi Myo Sett Win · Sep 13, 2018 · Viewed 20.9k times · Source

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

Answer

Mostafa Anter picture Mostafa Anter · Sep 13, 2018

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