Improve app performance with optimized resource shrinking
A fast and small app is crucial for a great user experience. R8, an app optimizer, achieves this by removing unused code and resources and optimizing runtime performance. The Android Gradle Plugin version 8.12.0 introduces optimized resource shrinking with R8. This feature makes apps smaller, leading to faster downloads, quicker installations, and reduced memory usage. It results in faster startups, better rendering, and fewer application not responding (ANR) errors. The new approach integrates resource shrinking with code optimization, enabling R8 to identify and remove resources exclusively referenced by unused code. This eliminates the need for AAPT2's unconditional keep rules, providing more precise discarding of unused code and resources. Previously, code and resource optimization were separate, leading AAPT2 to keep code referenced by resources, which then caused R8 to retain that unused code and its associated resources. To enable optimized resource shrinking, developers must set isMinifyEnabled and isShrinkResources to true in their build.gradle.kts file and add android.r8.optimizedResourceShrinking=true to their gradle.properties file. Significant app size improvements, over 50% for apps sharing resources and code, have been observed. Optimized resource shrinking further reduces size by removing both resources and DEX code through cross-boundary reference tracing. Starting with AGP 9.0.0, this optimized approach will be the default when resource shrinking is enabled. Developers are encouraged to try it and report any issues.
isMinifyEnabledandisShrinkResourcesto true in theirbuild.gradle.ktsfile and addandroid.r8.optimizedResourceShrinking=trueto theirgradle.propertiesfile. Significant app size improvements, over 50% for apps sharing resources and code, have been observed. Optimized resource shrinking further reduces size by removing both resources and DEX code through cross-boundary reference tracing. Starting with AGP 9.0.0, this optimized approach will be the default when resource shrinking is enabled. Developers are encouraged to try it and report any issues.