delete unused assets from the third-party library

advertisements

I've run into this issue where minifyEnabled and shrinkResources give me sub-optimal results in the sense that; when I have included,

'com.google.android.gms:play-services-places:9.6.1'

I am not using Google Sign In API's yet, I get loads of sign in images.

How do I get rid of them from my APK? They seem to add a fair bit of KB's.

I am using proguard with minify and shrink resources as explained above.


There is a detail topic here at android developer user guide.

Basically, you have lot of options to actually reduce the size of your apk by shrinking your resources. I have discussed in little brief about them, and I think Enable strict reference checks discussed below should solve your problem, but you can look at all available options to further reduce your apk size.


Customizing which resources to keep

As the doc says, use the below xml at res/raw/keep.xml, to decide what to keep and what not to keep :

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
    tools:discard="@layout/unused2" />

And rest will be taken care of by build system (This is useful when using different build variants).


Enable strict reference checks

If you have your code or your library referencing resources like below :

getResources().getIdentifier("image1", "drawable", getPackageName())

Then, in this case resource shrinker behaves defensively by default and marks all resources with a matching name format as potentially used and unavailable for removal.

So, add following to res/raw/keep.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:shrinkMode="strict" />

Adding this should solve your problem.


Remove unused alternative resources

Resource Shrinker does not remove alternative resources, like, alternative drawable resources for different screen densities, alternative string resources for different languages, etc.

So, you can yourself choose what to keep, from your build file, say, you want to keep on strings in 'en' locale :

android {
    defaultConfig {
        ...
        resConfigs "en", "fr"
    }
}

This can reduce size significantly.


If still resources are kept by resource shrinker, then manually exclude them using the first discussed method, and see if gets compiled and build properly, if not, then reason for keeping resources by resource shrinker, will become clear from the exception thrown while building.

Hope it helps !