9

I upgraded my Android Studio to 3.4 earlier today, and I am using the default shrinker R8 for the first time. I copied the contents of proguard-project.txt of a library project to its proguard-rules.pro. proguard-project.txt worked flawlessly for this project that generates an aar file for use by other app projects.

File proguard-rules.pro does not seem to be used. The project has the following in its build.gradle:

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
                    'proguard-rules.pro'
            signingConfig signingConfigs.Release
        }
        debug {
            signingConfig signingConfigs.Debug
        }
    }

proguard-rules.pro has the following:

# Preserve all public classes, and their public and protected fields and methods.
-keep public class * {
    public protected *;
}

The public methods' names are not preserved at all: enter image description here

Could anyone offer a tip on how to fix this?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Hong
  • 17,643
  • 21
  • 81
  • 142

1 Answers1

8

Add this line to gradle.properties

android.enableR8 = true

And try below code inside your proguard-rules.pro

-keep public class ** {
    public *;
    protected *;

}

Edit #1

Check here for how to migrate Proguard to R8: Android/java: Transition / Migration from ProGuard to R8?

shizhen
  • 12,251
  • 9
  • 52
  • 88
  • 2
    Thanks a lot. public *; protected *; did it. The others seem to be unnecessary. – Hong Apr 23 '19 at 04:04
  • 1
    Do we need the first line to enable R8? I thought it was enabled by default in the new version of android studio. – Nikos Hidalgo Apr 23 '19 at 11:10
  • @NikosHidalgo No. I do not have the first line, and it is working fine now. I believe you are right - it is enabled by default starting from Android Studio 3.4. – Hong Apr 23 '19 at 14:46
  • This was a bug, see http://issuetracker.google.com/131712625. The issue is fixed and will be part of Android Studio 3.4.1. – sgjesse May 09 '19 at 04:40
  • As of June, 2023, this was the solution that worked for me. I upgraded my gradle version and all of a sudden the R8 builds were obfuscating some files that should not have been and my production builds were breaking with zero changes to the proguard rules on my end. Adding the public and protected lines as mentioned above fixed it, but I was minutes away from downgrading gradle to go back to proguard instead of R8 again. – PGMacDesign Jun 27 '23 at 22:02