Source: Android Developer
Question: How to prevent shrinkResources
from deleting some files?
Answer:
Resouces shrinking is a way to help keep the szie of the apk down, for example:
android {
...
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
This attribute must be set together with the minifyEnabled true
attribute, see the link for detail.
But in case we need to keep some specific files:
If there are specific resources you wish to keep or discard, create an XML file in your project with a <resources>
tag and specify each resource to keep in the tools:keep
attribute and each resource to discard in the tools:discard
attribute. Both attributes accept a comma-separated list of resource names. You can use the asterisk character as a wild card.
For example:
<?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" />
Save this file in your project resources, for example, at res/raw/keep.xml
. The build does not package this file into your APK.