Improve app performance with optimized resource shrinking

Improve app performance with optimized resource shrinking

Home » News » Improve app performance with optimized resource shrinking
Table of Contents

Posted by Johan Bay – Software program Engineer

A small and quick app is essential to a incredible consumer expertise. That is why we constructed R8, our app optimizer, which streamlines your app by eradicating unused code and sources, 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, an excellent higher approach to shrink your app with R8. By opting in, you may make your app smaller, which implies smaller downloads, quicker installations, and fewer reminiscence used in your customers’ units. 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 way in which– as an example, shrinking the useful resource desk (sources.arsc) is now a default optimization.

The brand new strategy improves useful resource shrinking by totally integrating it with the present code optimization pipeline. Within the new strategy, R8 optimizes each code and useful resource references on the similar time making certain that every one sources 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 far more fine-grained and exact info for discarding unused code and sources

That is an enchancment over the present pipeline the place code and useful resource optimization are separate. Within the present pipeline, AAPT2 generates maintain guidelines to unconditionally maintain courses referenced from sources. Then, R8 optimization runs with these maintain guidelines. After R8 is finished optimizing and shrinking the code, it scans the remaining code to construct a graph of all of the sources referenced straight or not directly. Nonetheless, the unconditional AAPT2 guidelines usually maintain code that’s in any other case unused, which in flip causes R8 to maintain each the unused code and the unused sources referenced by it.

The 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 present implementation. For apps that share vital sources 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 good points:

table showing the progressive improvements in size as additional optimizations are enabled

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 dimensions of your DEX, whereas enabling useful resource shrinking removes unused sources in each the res folder and within the useful resource desk, however doesn’t change the DEX dimension additional, and at last, optimized useful resource shrinking additional reduces the dimensions by eradicating each sources and DEX code since it will possibly 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 habits for any venture that has the useful resource shrinker turned on.

Take a look at the newly up to date documentation to strive optimized useful resource shrinking and tell us for those who encounter any issues on the difficulty tracker.

Supply hyperlink

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

share this article.

Enjoying my articles?

Sign up to get new content delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.
Name