How to Shrink the Size of Your Android App With R8

When users can download your app faster, they can fall in love with it quicker

Jaimil Patel
Mar 12 · 4 min read
Photo by Sam Warren on Unsplash

An Android app can serve many purposes for modern enterprises. You can come up with a customized Android app with desired features to streamline the business and connect with your customers with ease.

However, you need to focus on the size of your Android app. It is the app’s size that decides whether people will install your app or not. Thanks to R8, you can optimize your app with a lighter APK.

Let’s discuss the role of R8 from the viewpoint of Android app development.


Configuration to Enable R8 in Your Android Studio

  1. Android Studio version 3.4.0 or higher.

When you use Android Studio 3.4 or the Android Gradle plugin 3.4.0 or higher, R8 is the default compiler.

You might be thinking what the implementation step to run or enable R8 in the project should be, please don’t think so much about it because there is no single line to do this as long as you’re currently working with the above-mentioned features.

“Keep in mind, R8 is designed to work with your existing ProGuard rules, so you’ll likely not need to take any actions to benefit from R8" — Android Dev

Google is intensely focused on build performance, it shows how dedicated Google is to do it and how far they are willing to go to get it.

You might be thinking about what the process is behind the scenes, right? So, let’s look at the process…


Build Process While Generating APK

  1. Desugar converts Java 8 features during the compilation time into something recognizable by Dalvik/ART compilers.

Later, in 2017, Google introduced D8, an optional replacement of the DEX compiler.


With D8, the Build Process Looks Like This

Source

ProGuard is the technique to shrink the code, which detects and removes the unused classes, fields, methods, packages, and attributes (including code libraries).

  1. Shrinking: It shrinks the code by detecting and removing unused code.

D8 runs faster and produces smaller .dex files with equivalent or better runtime performance when compared to the historic compiler, DX.

Google made D8 the default DEX compiler in 2018. Later, in 2019, Google introduced R8, which is a replacement for ProGuard and has all the features of D8.


With R8, the Build Process Looks Like This

Source

As a developer, you all know that we can believe only in the practical result. So, that’s why Android officials have uploaded the below results to better understand how R8 works faster and better than ProGuard.

Here are the results of the Santa Tracker app.

Source
Source
Source

R8 Configuration Files

  1. proguard-rules.pro (Android Studio creates this file in the root directory of that module, you can add your custom ProGuard to keep rules according to your requirements.)

If you want to see the reports of the R8 compiler, add the below lines to the proguard-rules.pro file

  1. -printconfiguration ~/tmp/full-r8-config.txt (Report of R8 applies when building your projects.)

Difference Between R8 and ProGuard

R8

R8 combines desugaring, shrinking, obfuscating, optimizing, and dexing all in one step — resulting in noticeable build performance improvements.

ProgGuard

ProGuard integrates only shrinking, obfuscating, and optimizing.

According to the above statements, ProGuard only works to reduce APK size. So, other processes like desugaring and dexing are included additionally in the build process so they take more time to build.

R8 works for three different things in one step, so it finishes the whole build process in one step to reduce the overall time of compilation.

I definitely believe that you will want to enable R8 in your project to experience a reduction in build process time and reduced size APK.

So, that’s it for R8.

Happy coding, I hope you have enjoyed reading my blog.

Better Programming

Advice for programmers.

Jaimil Patel

Written by

Software Developer at Solution Analysts. Android | Kotlin | Flutter | React Native. Whatever I learn, Happy to share with others

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade