Open In App

Difference Between Proguard and R8 in Android

Improve
Improve
Like Article
Like
Save
Share
Report

Proguard and R8 both are similar tools that are used for reducing the size of our APK and increase the performance of our APK by shrinking the unused resources. In this article, we will take a look at: 

  • What is Proguard?
  • What is R8?
  • Difference between Proguard and R8.

What is Proguard? 

Proguard is a java tool in Android that helps to do the following things such as: 

  • It removes the unused classes and methods from your app which helps to reduce the size of your APK.
  • It makes your application difficult to reverse engineer by obfuscating the code.
  • It reduces the size of your application.

How to enable Proguard in your Application? 

To enable proguard in your application Navigate to the Gradle Scripts > build.gradle(:app) and then you will get to see a method called buildTypes. 

buildTypes {

       release {

           minifyEnabled true

           proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’

       }

}

In this block of code, we have to change minifyEnabled to true to activate our Proguard. This code of Proguard is written under the release block so it will only work on the release build of your APK. It will activate the proguard which will take it from the file named as

‘proguard-android.txt’.  

What is R8? 

R8 is another tool that will convert your java byte code into an optimized format of dex code. It will check the whole application and will remove the unused classes and methods. It helps us to reduce the size of our APK and to make our app more secure. R8 uses similar proguard rules to modify its default behavior. 

How to enable R8 in your Application?

R8 is already present in your application you just have to enable it. To enable it just Navigate to the Gradle Scripts > build.gradle(:app) and then you will get to see a method called buildTypes.

buildTypes {

       release {

           minifyEnabled true

       }

}

In this block of code, we have to change minifyEnabled to true to activate R8. This code for R8 is written under the release block which is only working on the release build of your APK. 

Difference Between Proguard and R8.

  • R8 has more Kotlin support than that of Proguard.
  • R8 is having a faster processing time than Proguard which reduces build time.
  • R8 gives better output results than Proguard.
  • R8 reduces the app size by 10 % whereas Proguard reduces app size by 8.5 %.
  • The android app having a Gradle plugin above 3.4.0 or above then the project uses R8 by default with Proguard rules only.

Proguard

Proguard

In Proguard java compiler converts Apps code into JAVA bytecode and proguard will converts this bytecode into the optimized java bytecode. This java bytecode is then optimized by dex into Optimized Dalvik bytecode. The conversion process of apps code to optimized Dalvik bytecode is a four steps process.  

R8

R8

In R8 java compiler converts Apps code in JAVA bytecode and then R8 will directly convert JAVA bytecode into Optimized Dalvik bytecode. The conversion process of apps code to Optimized Dalvik bytecode is a three steps process that is faster in comparison with proguard. 

Difference Table 

Proguard

R8

Proguard is having lesser speed in comparison with R8 which affects the build time. R8 is having a faster processing time which helps developers in reducing build time.
The output quality by using proguard is not superior.  The output quality using R8 is superior.
Proguard reduces app size by 8.5 %. R8 reduces app size by 10 %.

The conversion process of converting apps code to Optimized Dalvik bytecode is as follows : 

Apps Code > Java bytecode > Optimized java bytecode > Optimized Dalvik bytecode.

The conversion process of converting apps code to Optimized Dalvik bytecode is as follows : 

Apps code > Java bytecode > Optimized Dalvik bytecode.

Proguard is only supported for the gradle plugin which is below 3.4.0 R8 is only supported for the gradle plugin of 3.4.0 and above.
Peephole Optimizations in Proguard is around 520. Peephole Optimizations in R8 is around 6.
No of the steps in converting of Apps code to Optimized Dalvik code is 4. No of the steps in converting Apps code to Optimized Dalvik bytecode is 3. 
Proguard is not having Kotlin specific optimizations.  R8 is having Kotlin specific optimizations. 
Proguard does not propagate constant arguments.  R8 propagate constant arguments. 

Conclusion

While R8 is faster than proguard as there are fewer number steps. It reduces the size of the app in an optimized manner with a default compile-time optimizer. 


Last Updated : 23 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads