5

I have an issue with R8. In MyLib I have public abstract MyLibsClass in which I have protected methods. MyChildClass extends from MyLibsClass in MyApp and after R8's magic all protected methods (including protected abstract) in MyLibsClass are changed into public ones, and of course in MyChildClass I'm getting "attempting to assign weaker access privileges ('protected'); was 'public') issue as trying to override protected abstract methods.

Additional info

gradle-6.0.1

MyLib's build.gradle

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

proguard-rules.pro

-keep class com.example.mylib.*{
    public protected *; }

-keep class com.example.mylib.*$*{
    public protected *; }

Anyone had this kind of issue or know a way to fix this?

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61

3 Answers3

4

So based on discussion here ,

DON'T USE DEFAULT PROGUARD SETTINGS FOR LIBRARIES

as allowAccessModification is enabled in default proguard settings, which is located in Android SDK (\Android\Sdk\tools\proguard\proguard-android-optimize.txt) and my mistake was using this for my libraries.

Citation from proguard manual

you probably shouldn't use this option when processing code that is to be used as a library, since classes and class members that weren't designed to be public in the API may become public.

So if anyone has the same issue I will suggest to create your own base config file for proguard and copy past whole default configs without "allowAccessModification" into it.

Also if someone interested more, you can track this issue. Hopefully will get separate config file for libraries in near feature.

Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61
2

I faced the same problem, and thanks to @Hayk Nahapetyan's answer, I could resolve it.

Here is my solution with a little more detail.

In the library module's build.gradle, remove the default file from the buildTypes's release closure:

release {
    minifyEnabled true
    proguardFiles 'proguard-rules.pro'
}

R8 no longer uses the default file that is provided in the Android SDK. It generates one at build time, and puts it in the module's build directory at build/intermediates/default_proguard_files/global.

Copy the contents of proguard-android-optimize.txt-a.b.c (where a.b.c is the library version, if set) from that location to the top of the module's proguard-rules.pro. Then remove -allowaccessmodification; two times, if it originally appeared in both files.

HendrikFrans
  • 193
  • 9
1

This was also reported on the R8 bug tracker, and resolved there. See http://issuetracker.google.com/147447502.

sgjesse
  • 3,793
  • 14
  • 17