During the development of Android Application, always we have to think about the apk size and another important thing is to think about reverse engineering. Especially for these two important issues, we, the android developers, are always using ProGuard or R8 in our project.
In this article, we are going to learn about ProGuard and R8 elaborately.
What is ProGuard?
ProGuard is a java tool for shrinking, obfuscating, and optimising the source code in release build.
First, the android studio compiles java classes into Java Byteode (.class file) file using Java Compiler, then the ProGuard will compile the .class files into optimised Java Bytecode. Finally, using the dex compiler, this optimised .class files will be converted into .dex file (Dalvik bytecode). And at the end of the process, this bytecode is shipped in the apk file for installing in the android device.
Here is the graphical illustration:
Three main tasks done by ProGuard:
1. Shrinking: Remove unused codes, methods, and attributes from app and its library dependencies. For instance, if we use a library, most of the time we don’t use all the features of the libraries, or in some cases, we mistakenly add the library dependencies in our application, but not use the features. In that case, all the unused codes and classes will be removed by ProGuard. So the total size of the application will be reduced and it will also contribute to 64k dex method limit issue.
2. Obfuscating: Change the names of classes, methods, and fields. It helps to prevent reverse engineering. By Obfuscating, the DEX file size is also reduced.
3. Optimisation: This helps to inline the function of some selective classes by removing them, removing dead-code, merging class, etc. After removing classes (after shrinking), there also could have some scope to optimise the app. Here this optimisation is also possible. Here, if a branch of code is not used, it will be removed.
What is R8?
R8 is the new code shrinker provided by google. It is almost similar to the ProGuard and is being used as a replacement for ProGuard. It is available from Android Studio 3.3 beta.
For improving the performance of the build process, in 2015, the android team introduced the compiler, Jack and Jill, with the combination of Java compiler, ProGuard, and Dalvik compiler. Then the steps were looking like below:
It was fine for the building process but didn’t fit with the ecosystem of the languages and growing technologies. Finally, in 2017, they again back to the old model but replace the dex compiler with the D8 compiler.
This building process was enough good for the new growing technology as well as both languages, Java and Kotlin. D8 was producing better quality bytecode than dex. But still, there were uses of many steps and was also space for optimising the building process. In this stage, R8 comes for optimising more the building process and kick off the ProGuard and the D8. And the setup has been simplified as below:
When we develop a project using Android Gradle Plugin 3.4.0 or higher, the R8 compiler is by default shipped for optimising and shrinking code. And it performs at the time building release version of our app (we have to enable this manually). R8 compiler works with our existing ProGuard rules files, so that we can customise R8’s tasks or we also can disable some specific tasks using these ProGuard rules files.