33

Since the 3.5 update of Android Studio, I have this warning when building my app :

DSL element 'useProguard' is obsolete and will be removed soon. Use 'android.enableR8' in gradle.properties to switch between R8 and Proguard..

Greelings
  • 4,964
  • 7
  • 34
  • 70

4 Answers4

73

Removing "useProguard" from build.gradle fixed the problem for me, like:

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

Update 2022, or Details; R8 is nowadays:

  • By default enabled,
  • And it simply replaces ProGuard,

    But supports existing .pro files (and there should be no need to reconfigure).

  • Also, any way to disable R8, or use ProGuard instead, is deprecated (or even removed).

    But debug flavors can set debuggable true, and continue to be debuggable line-by-line.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Greelings
  • 4,964
  • 7
  • 34
  • 70
8

set the following in your project's gradle.properties file

android.enableR8=true

R8 also has full mode that is not directly compatible with Proguard. In order to try that out you can additionally set the following in your gradle.properties file

android.enableR8.fullMode=true

This turns on more optimizations, that can further reduce app size. However, you might need a few extra keep rules to make it work.

Praveen
  • 430
  • 3
  • 11
  • 3
    This is no longer required: `The option 'android.enableR8' is deprecated. It was removed in version 7.0 of the Android Gradle plugin. Please remove it from `gradle.properties`. – VIN Apr 16 '22 at 19:07
3

At a glance, when you build you project using Android Gradle plugin 3.4.0 or higher, the plugin no longer uses ProGuard to perform compile-time code optimization. Instead, the plugin works with the R8 compiler by default to handle the Shrink, obfuscate, and optimize your app. However, you can disable certain tasks or customize R8’s behavior through ProGuard rules files.

In fact, R8 works with all of your existing ProGuard rules files, so updating the Android Gradle plugin to use R8 should not require you to change your existing rules.

When you use Android Studio 3.4 or Android Gradle plugin 3.4.0 and higher, R8 is the default compiler that converts your project’s Java bytecode into the DEX format that runs on the Android platform. However, when you create a new project using Android Studio, shrinking, obfuscation, and code optimization is not enabled by default. You may enable them using the below code -

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}

For the more adventurous, R8 also has full mode. In order to try that out you can additionally set the following in your gradle.properties file.3

android.enableR8.fullMode=true

This turns on more optimizations, that can further reduce app size. However, you might need a few extra keep rules to make it work. Learn more here - https://youtu.be/uQ_yK8kRCaA

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • App crashing on enabling android.enableR8.fullMode=true ->Caused by: java.lang.UnsupportedOperationException: Abstract class can't be instantiated! Class name: e.c.a.l.k0 ->Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for class e.c.a.l.k0. Registering an InstanceCreator with Gson for this type may fix this problem. Using Android Studio 4.0 – Girish Sep 16 '20 at 13:53
0

R8 is the default tool available in Android Studio 3.4 onwards. There is no need to explicitly enable R8. Just remove the useProguard true line from the app/build.gradle file.

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81