Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android/java: Transition / Migration from ProGuard to R8?

I wonder how to make the transition / migration from ProGuard to R8.

Should I just remove the Proguard-related lines from my Gradle files and add the android.enableR8 = true line instead ?

Thanks.

like image 273
Regis_AG Avatar asked Oct 15 '18 13:10

Regis_AG


People also ask

What is the difference between R8 and proguard?

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 %.

Does R8 use Proguard?

R8 configuration files. R8 uses ProGuard rules files to modify its default behavior and better understand your app's structure, such as the classes that serve as entry points into your app's code.

How do I enable R8 on my Android?

To enable R8, open build. gradle module app file and add this piece of code inside the buildTypes . The code inside the release{} block means that this will be applied to the release build version of your application. If you launch the app in the emulator, this code is not executed.


1 Answers

Proguard is developed and maintained by GuardSquare while R8 is developed and maintained by Android team which means they are two different products although R8 is compatible with Proguard.

As seen from here https://www.guardsquare.com/en/blog/proguard-and-r8

Compatibility of ProGuard and R8

The good news for developers is that R8 is backward compatible with ProGuard. If you have a working ProGuard configuration (maybe eclectically copied from Stackoverflow), you can carry that over to R8. It currently still ignores some options. Notably, R8 doesn't implement the options -whyareyoukeeping and -addconfigurationdebugging, which we consider essential to quickly get to a working configuration, as we've explained in a previous blog.

Yes, android.enableR8 = true will enable the R8 feature.

Also note that, R8 does not currently (as the time of Android Studio 3.2.1) support Android Archive Library (AAR) projects. It is used only when building APK files.


Edit #1

From @Archie, If you are using Gradle plugin version 3.4.0 and above, R8 is on by default.

See: https://developer.android.com/studio/releases#r8-default


Edit #2

For the transition from Proguard to R8, you can follow below steps:

1. Disable Proguard

Update the buildTypes { } configuration to disable Proguard, e.g. for release build type:

   android {        ...        buildTypes {            release {                useProguard false // <-- disable proguard                minifyEnabled true // <-- enable minification                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'            }        }        ...    } 

On Android Studio 3.4, useProguard by default is false. And R8 is enabled by default.

2. (Optional) Set full R8 configurations report file

Add below line into your proguard-rules.pro to output a full report of all the rules that R8 applies when building your project.

// You can specify any path and filename. -printconfiguration <your-path>/full-r8-config.txt 

3. Generate the obfuscated app.

./gradlew assembleRelease 

4. (Optional) Fine-tune and trouble shooting

Find your <your-path>/full-r8-config.txt to fine-tune the configuration or do trouble shooting if any.

References:

https://developer.android.com/studio/build/shrink-code.html#configuration-files

like image 79
shizhen Avatar answered Sep 17 '22 12:09

shizhen