|

Why R8 for code shrinking instead of Proguard in android studio

Sharing is caring!

As an android developer, we have seen many changes for building the code and generating the dex file to make an executable file for android.  We know from the initial support of eclipse, we used Ant as a built tool and after coming with the android studio now it has removed and changed to the Gradle build tool.

But still, the developer was not happy because of the slow speed to build the code and convert source code to the dex file to make executable file for android. Generally, we used Progaurd to shrink the code into an optimized dex code while creating the APK file. Progaurd makes our source code optimized to reduce the APK size and provide the security to no one can access the code with the help of reverse engineering.

How Progaurd work to convert java code to dex file?

When we go back and try to understand the process of Progaurd to generating the dex file to make an executable file to install into the android devices. Progaurd will start shrinking the source code while we trigger a build in release mode from the android studio, First, it will compile all java code into the .class file with the help of Java compiler and now Proguard will compile this .class file into optimized java byte code. Now with the help of the Dalvik virtual machine, it will compile into the dex file to convert into an executable file to install into the device. As below we can see there four steps used to convert into the dex file.

SourceCode(.java) —javac—> Java Bytecode(.class) —Proguard—> Optimized Java bytecode(.class) —Dex—> Dalvik Optimized Bytecode(.dex)

And the Android team changes this four-step into a single step to convert direct source code into the dex file, so they introduced the Jack & Jill compiler to direct conversion into the dex file. But it was not a good success because of growing technology is not fitted for this. As we can see below step.

SourceCode(.java) —Jack & Jill—> Dalvik Optimized Bytecode(.dex)

Because of not supported the growing technology, they again moved to old-style with few small changes in place of Dex, introduced by D8. So the process is like that

SourceCode(.java) —javac—> Java Bytecode(.class) —Proguard—> Optimized Java bytecode(.class) —D8—> Dalvik Optimized Bytecode(.dex)

D8 process is now better than the Dex in terms of optimizing the dex code. But now the Android supported both Kotlin and Java and this feature again needs to write some code inside the Progaurd file to keep and don’t warn with the class and package detail.

SourceCode(.java) —javac—> Java Bytecode(.class) —R8—> Dalvik Optimized Bytecode(.dex)

Now, again the android team introduced the new feature called R8 with the latest android studio 3.4 which is default supported R8. A good thing in R8, We do not write any specific code. It will shrink the code and resources which are not used or unnecessary code to make the optimized dex file.

The process to enable to use of Progaurd for releasing the APK:

While generating the APK file in release mode we must enable the Proguard to shrink the code to make optimized the dex file. We need to modify in the Gradle file:

android {
  /// ................
    buildTypes {
        debug {
          minifyEnabled false
        }
        release {
             minifyEnabled true
             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          
        }
    }
}

And we need to write the particular class or package into the progaurd-rule.pro file to don’t warn or keep to optimize the dex file for creating the APK.

-dontwarn android.support.**
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }

 

So if we used any module or additional dependency need to write into the Progaurd rules file to optimize the code for conversion into the dex.

Process of Enable the R8 with below of 3.4 version of Gradle or android studio:

As I said above the android studio or Gradle version 3.4 or above, R8 is default supported, in this case, a developer will not be worry to write any logic. It will optimize the source code into a dex file. But the below Gradle version 3.4 or less stable android studio version 3.4 will not be supported by default. We need to enable this from the Gradle properties file.

gradle.properties

android.enableR8=true

and for full enable R8

android.enableR8.fullMode=true

 

You can disable desugaring warning with R8 (in project’s gradle.properties file):

android.enableR8.desugaring=false

 

But what happened with the very old architecture devices?

it may be we faced some issue on developing the android app on android studio stable version 3.4 or above or Gradle version 3.4 or above that R8 has optimized the dex file but it has not able to install the release APK into few devices which architecture is old designed. So, in this case, enable the Progaurd to optimized the dex file and most chance it will get installed to those devices too.

Conclusion:

The Android R8 is much younger, with a team actively improving its stability and extending its functionality. As a developer, we need to start using the R8 feature to optimized the dex file to help reduce the APK size. For more detail, you check this article and this is used for reference.

If you are wondering to learn Android then Please learn from Android category and wondering to learn Kotlin then Kotlin Category will help you. If you want to learn all the python article, then learn from the python category.

Happy Coding 🙂

5 1 vote
Article Rating

Similar Posts

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments