R8 vs Proguard in Android

Tram Ho

Introduce

Hello 500 ae, come and see you again, let’s find out something), I just know about this R8, so I also want to learn and share a little with you. Hope this article of mine will help you

Proguard is a keyword that is quite familiar to all Android developers. We use it to reduce size, improve application performance by shrinking unused resources.

Google has released R8 to replace Proguard to help developers shrink code with better generated output (APK). They are considered much faster than Proguard.

In this blog we will learn about

What is R8? How does the R8 shrink work? Compare R8 with Proguard

Source: https://blog.mindorks.com/r8-vs-proguard-in-android

What is R8?

R8 is a tool that converts our java byte code into optimized dex code. It iterates the entire application and then it optimizes like removing unused classes, methods, etc. It runs in compile time. It helps us to reduce the size of the build and make our application more secure. R8 uses Proguard rules to modify its default behavior.

How Does R8 Shrinking Work?

While optimizing the code, R8 reduces our application code and then the APK size will decrease.

To reduce APK size, the R8 has three different techniques:

  1. Shrinking or Tree Shaking: Minimize is the process of removing the inaccessible code from our Android project. R8 performs some static analysis to get rid of inaccessible code and remove objects with no information.
  2. Optimization: Used to optimize code for the size. It involves removing dead code, removing unused arguments, selective inline, class merging, etc.
  3. Identifier Renaming: In the process, we scrambled the class name and other variable names. For example, if the name of the class is ” MainActivity “, then it will be scrambled to ” a ” or something else but of a smaller size.

Activate R8 Shrinking in your app?

By default, R8 is present in our app but to enable R8 in our app let’s set minifyEnabled to true in build.gradle app’s main build.gradle file.

R8’s comparison with Proguard

Now let’s compare R8 and Proguard!

  • With Android apps using Gradle plugin over 3.4.0 and above, the project uses R8 by default and no longer uses Proguard to perform optimization. However, it only uses Proguard rules.
  • R8 efficiently inline container classes and removes unused classes, fields, and methods. Proguard 8.5% reduced application size while with R8 reduced 10%.
  • R8 has more Kotlin support than Proguard.
  • R8 gives better outputs than Proguard and does so faster than Proguard, thus reducing overall build time.

So now let’s compare how both Proguard and R8 work.

Proguard

When using Proguard, the Application code is converted to Java bytecode by the Java compiler. Once transformed, it will be optimized by Proguard using the rules we have written. The dex then converts it to an optimized Dalvik byte encoding.

This is almost a 4 step process to convert it into Dalvik bytecode.

R8.

While using R8, the application’s code is first converted by the java compiler to Java byte code and then using R8 directly, it will convert java byte code in Dalvik byte code.

By using R8, it directly reduces Java bytecode to Dalvik Bytecode conversion steps from 2 to 1.

  • Proguard applies 520 vulnerability optimization compared to R8 which is very few. Vulnerability optimizations are optimizations performed on a set of compiler-generated code to improve code performance by making the code shorter and faster.
  • In both Proguard and R8, we have to handle reflection by writing custom configuration.
  • R8 is faster than Proguard in doing code conversion.

Optimization comparison between Proguard and R8.

Let’s discuss a few features that are supported by both Proguard and R8.

For example, Proguard and R8 both put methods in private mode in code. They both remove the class, field, or even methods that are not used in the project from being used. Both of these support the simplification of Enum types. Both inline methods, code merging, etc.

Proguard also makes final layers while R8 cannot do it. But comparing R8 supported by Kotlin, Kotlin structure optimization that Proguard cannot do.

Now, if you also want to enable active optimization in R8 and reduce the size even more, enable the following in gradle.properties ,

Conclude

With R8 becoming the default compile-time optimizer, it reduces the size of the application.

Share the news now

Source : Viblo