To make your APK file as small as possible, you should enable shrinking to remove unused code and resources in your release build.
To enable code shrinking with ProGuard, add minifyEnabled
true to the appropriate build type in your build.gradle
file.
android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile(‘proguard-android.txt'),
'proguard-rules.pro'
}
}
}
where:
minifyEnabled true
: enable code shrinkinggetDefaultProguardFile(‘proguard-android.txt')
method gets the default ProGuard settings from the Android SDKproguard-rules.pro
file is where you can add custom ProGuard rulesTo enable resource shrinking, set the shrinkResources
property to true in your build.gradle
file.
android {
...
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Pay attention because resource shrinking works only in conjunction with code shrinking.
You can customize which resources to keep or discard creating an XML file like this:
<?xml version=1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@layout/mylayout,@layout/custom_*"
tools:discard="@layout/unused" />
Save this file in res/raw
folder.
All libraries come with resources that are not necessary useful to your application. For example Google Play Services comes with translations for languages your own application don’t even support.
You can configure the build.gradle file to specify which resource you want to keep.
For example:
defaultConfig {
// ...
resConfigs "en", "de", "it"
resConfigs "nodpi", "xhdpi", "xxhdpi", "xxxhdpi"
}