Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing unused assets from 3rd party library

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. 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.

like image 336
Hades Avatar asked Nov 20 '16 16:11

Hades


3 Answers

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 !

like image 75
Abhinav Puri Avatar answered Oct 22 '22 03:10

Abhinav Puri


If nothing else works to make the build system automatically detect unused resources, you might want to try explicitly excluding these via the packagingOptions DSL like:

android {
    packagingOptions {
        exclude '/**/common_google_signin*.png'
    }
}
like image 3
sschuberth Avatar answered Oct 22 '22 04:10

sschuberth


There may be some possibilities to reduce app size and remove unused assets.

You can delete unused assets from (':lib') project library.

If you have imported @aar file than you can do something like this

compile ('com.facebook.android:facebook-android-sdk:3.22.0@aar'){
    exclude module: 'support-v4'
}

Still we have unused files in @aar file than find alternative way that replace that @aar to (':lib'), remove assets from there.

like image 3
Krunal Kapadiya Avatar answered Oct 22 '22 04:10

Krunal Kapadiya