While building a project, I discovered the shrinkResources attribute, which removes unused resources from the APK. Here is how to configure it and what to watch out for.
Basic Configuration
shrinkResources requires minifyEnabled to be true, because resource shrinking runs after ProGuard/R8 removes unused code:
| |
Inspecting Results
Use --info logging to see overall reduction:
| |
View which specific files were skipped:
| |
Keeping or Discarding Specific Resources
Resources referenced via reflection (common in third-party SDKs) can be mistakenly removed. Modern versions support keep.xml for fine-grained control.
Strict Mode
In res/raw/keep.xml:
| |
Keeping Specific Resources
| |
Forcing Removal
| |
The build system also skips resources referenced via URLs like file:///android_res/drawable/ic_plus.png.
Language and Density Configuration
For apps that do not need multi-language support, use resConfigs to keep only specific languages or densities:
| |
You can also restrict to specific densities like "nodpi", "hdpi".
Summary
Enable shrinkResources even if your app is relatively clean. The log output helps you understand which resources are in use and which might be incorrectly removed.