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
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
- Android Studio version 3.4.0 or higher.
- Android Gradle plugin version 3.4.0.
build.gradleproject level for code optimization).
build.gradleproject level for resource shrinking = optional).
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
- Desugar converts Java 8 features during the compilation time into something recognizable by Dalvik/ART compilers.
- ProGuard removes unused classes, fields, methods, and attributes.
- The DEX compiler converts Java bytecode to Android’s .dex file format.
Later, in 2017, Google introduced D8, an optional replacement of the DEX compiler.
With D8, the Build Process Looks Like This
ProGuard is the technique to shrink the code, which detects and removes the unused classes, fields, methods, packages, and attributes (including code libraries).
- Shrinking: It shrinks the code by detecting and removing unused code.
- Optimizer: It analyzes and optimizes the byte code.
- Obfuscator: It renames the optimized classes, methods with short names. (Used to prevent reverse engineering).
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.
R8 Configuration Files
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.)
proguard-android-optimize.txt(Generated by the Android Gradle plugin at runtime.)
If you want to see the reports of the R8 compiler, add the below lines to the
-printconfiguration ~/tmp/full-r8-config.txt(Report of R8 applies when building your projects.)
-printusage <output-dir>/usage.txt(Report of removed code by R8.)
-printseeds <output-dir>/seeds.txt(Report of entry points that R8 determines from your project’s keep rules.)
Difference Between R8 and ProGuard
R8 combines desugaring, shrinking, obfuscating, optimizing, and dexing all in one step — resulting in noticeable build performance improvements.
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.