THE Latest version of the Android Gradle plugin (AGP) introduces an optimized approach to resources which unifies the optimization of the code and the narrowing of resources, achieving up to 50% of the size of applications for applications that share important resources and code on different form factors, explains Google.
In the new approach, the long -standing optimizer of Android R8, deals with the code and resource references at the same time, which makes it possible to identify all the resources which are only referenced from the unused code, and thus delete them with the unused code.
This completely eliminates the need for unconditional maintenance rules generated by AAPT2 (the resource packaging tool for Android) and provides much more grain and precise information to eliminate the code and unused resources.
In previous versions, AAPT2 generated Unconditional maintenance rules Before R8, R8 optimized the code, often making them reference the unused code and prevent R8 from deleting this code as well as its associated resources.
“As mentioned, the new R8 optimization pipeline can make more than 50% improvements in applications that share code and significant resources on different form factors, while also offering measurable advantages for small applications.
With the new optimized behavior, developers now have four distinct approaches to the optimization of applications: no optimization, R8 only, R8 with resource reduction and narrowing of optimized resources. Each successive step offers additional advantages.
The activation of R8 cuts the size of your DEX, while activation of shrinking resources deletes unused resources in the Res and in the Table of Resources, but does not change the size Dex more, and finally, the optimized resource is more narrowing the size by deleting both resources and dex code because it can trace references through DEX and
The new resource narrowing pipeline is available as opt-in in the last AGP 8.12.0. To activate it, you use the following AGP configuration:
android {
buildTypes {
release {
isMinifyEnabled = true
isShrinkResources = true
…
}
}
}
The new behavior will be the default value of the next AGP 9.
Reducing the size of an application has several advantages, including faster download, faster installation and lower memory consumption. It also improves the start -up time, improves the rendering speed and reduces the ANR (the application does not meet the behavior generally when the user interface thread of an application is blocked for too long).