Android/java: Transition / Migration from ProGuard to R8?
Asked Answered
F

1

33

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.

Fritillary answered 15/10, 2018 at 13:57 Comment(1)
Folks, you may find this helpful - https://mcmap.net/q/424126/-gradle-dsl-element-39-useproguard-39-is-obsolete-and-will-be-removed-soonCarbarn
H
46

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

Himyaritic answered 22/10, 2018 at 1:53 Comment(7)
Would you know where to enable android.enableR8 = true ? I encountered this for a "old" empty project (Just did a string of Android Developer updates). I couldn't work out where to enable the setting so I wiped the project and created a new one in the hopes I could add that information as a comment, but after grepping the new files I find I have no entry for this line.Clap
Add this line to gradle.propertiesHimyaritic
Thanks for the feedback, I've just started making head way into Android applications.Clap
if you are using Gradle Version 3.3.0 and above, R8 is on by default. See: developer.android.com/studio/releases#r8-defaultWoodenware
R8 is turned on by default starting with 3.4.0, not 3.3.0. The part of the changelog that you've linked is for 3.4.0. If you're interested in some of the behavioral differences between ProGuard and R8, the company I work for, PreEmptive Solutions, has put together some documentation that tries to cover that here: r8-docs.preemptive.com.Cabob
One more thing is that useProguard will be deprecated from the DSL. If anyone wants to keep using proguard instead of R8, set android.enableR8 = false in gradle.properties. See: issuetracker.google.com/issues/129758703Heti
@shizhen: any way to fix this? #57736608 as I know useProguard is going to get deprecated. But this issue happens with enabling r8Carswell

© 2022 - 2024 — McMap. All rights reserved.