Posted by Johan Bay – Software program Engineer
A small and quick app is vital to a implausible person expertise. That is why we constructed R8, our app optimizer, which streamlines your app by eradicating unused code and assets, rewriting code to optimize runtime efficiency, and extra.
With the discharge of model 8.12.0 of the Android Gradle Plugin (AGP), we’re introducing optimized useful resource shrinking, a fair higher strategy to shrink your app with R8. By opting in, you may make your app smaller, which suggests smaller downloads, quicker installations, and fewer reminiscence used in your customers’ gadgets. The result’s a quicker startup, improved rendering, and fewer ANRs.
The way it works
Useful resource shrinking for Android apps has been round for a very long time, with a number of enhancements made alongside the best way– as an example, shrinking the useful resource desk (assets.arsc) is now a default optimization.
The brand new method improves useful resource shrinking by totally integrating it with the prevailing code optimization pipeline. Within the new method, R8 optimizes each code and useful resource references on the identical time guaranteeing that every one assets referenced solely from unused code are recognized as unused after which eliminated. This fully eliminates the necessity for the unconditional maintain guidelines generated by AAPT2 (the useful resource packaging device for Android) and offers way more fine-grained and exact info for discarding unused code and assets
That is an enchancment over the prevailing pipeline the place code and useful resource optimization are separate. Within the present pipeline, AAPT2 generates maintain guidelines to unconditionally maintain courses referenced from assets. Then, R8 optimization runs with these maintain guidelines. After R8 is completed optimizing and shrinking the code, it scans the remaining code to construct a graph of all of the assets referenced instantly or not directly. Nonetheless, the unconditional AAPT2 guidelines typically maintain code that’s in any other case unused, which in flip causes R8 to maintain each the unused code and the unused assets referenced by it.
The best way to use it
First, activate R8 optimization with useful resource shrinking, through the use of the next in your construct.gradle.kts file:
android { buildTypes { launch { isMinifyEnabled = true isShrinkResources = true … } } }
Activate the brand new optimized useful resource shrinking pipeline by including the next to your gradle.properties file:
android.r8.optimizedResourceShrinking=true
Advantages
The optimized useful resource shrinking pipeline has proven vital enhancements over the prevailing implementation. For apps that share vital assets and code throughout completely different kind issue verticals, we measured enhancements of over 50% in app dimension. Smaller apps see enhancements as nicely – for instance, in Androidify we see the next positive factors:

The desk exhibits the progressive enhancements in dimension as extra optimizations are enabled, from no shrinking to optimized useful resource shrinking. The cells marked with an asterisk (*) point out improved numbers in comparison with the earlier row. Enabling R8 trims the scale of your DEX, whereas enabling useful resource shrinking removes unused assets in each the res folder and within the useful resource desk, however doesn’t change the DEX dimension additional, and eventually, optimized useful resource shrinking additional reduces the scale by eradicating each assets and DEX code since it may well hint references throughout the DEX and useful resource boundary.
Subsequent steps
Beginning with AGP 9.0.0, optimized useful resource shrinking turns into the usual conduct for any undertaking that has the useful resource shrinker turned on.
Try the newly up to date documentation to strive optimized useful resource shrinking and tell us if you happen to encounter any issues on the situation tracker.