proguard-rules "-dontwarn" not working (AGP 8.0)
Asked Answered
M

1

5

After updating to Android Gradle Plugin 8.0, I get the following build-time errors during the minifyReleaseAndroidTestWithR8 step:

Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in C:\Users\...\app\build\outputs\mapping\releaseAndroidTest\missing_rules.txt.

Missing class io.reactivex.Flowable (referenced from: io.reactivex.Flowable io.realm.BaseRealm.asFlowable() and 5 other contexts)

Missing class io.reactivex.Observable (referenced from: io.reactivex.Observable io.realm.RealmList.asChangesetObservable() and 3 other contexts)

Missing class java.lang.ClassValue (referenced from: java.lang.ClassValue com.google.common.util.concurrent.FuturesGetChecked$GetCheckedTypeValidatorHolder$ClassValueValidator.isValidClass and 3 other contexts)

I understand that prior to AGP 8.0 missing classes were treated as warnings, and now they are errors. So I added the rules from the missing_rules.txt file to my proguard-rules.pro:

-dontwarn io.reactivex.Flowable
-dontwarn io.reactivex.Observable
-dontwarn java.lang.ClassValue

However, the newly-added -dontwarn rules seem to have no effect. I get the same "Missing class" errors on subsequent builds. I have tried building after cleaning the project, building after restarting Android Studio, and building after invalidating Android Studio's cache. All to no avail.

I have also tried adding ".**" to each rule. I still get the same "Missing class" errors.

My proguard-rules file has another -dontwarn rule which does work:

-dontwarn sun.misc.Cleaner

If I remove this rule, I get a missing class warning for sun.misc.Cleaner (as expected), and if I re-instate the rule, the error goes away. This would appear to indicate that the problem is not a fundamental problem with -dontwarn rules.

If I disable minifyEnabled, the build completes without error.

Two of the three missing (io.reactivex) classes are referenced from Realm classes. Not sure if that is significant.

I've run out of things to try, and would welcome any thoughts/suggestions.

Thanks for reading.


proguard-rules.pro

-dontwarn sun.misc.Cleaner                      # this one works

-dontwarn io.reactivex.Flowable                 # has no effect
-dontwarn io.reactivex.Observable               # has no effect
-dontwarn java.lang.ClassValue                  # has no effect

# don't obfuscate field names of RealmObjects that are imported/exported via JSON
# e.g. design & project, and their sub-ordinate classes
-keep class com.something.model.design.** { <fields>; }
-keep class com.something.model.project.** { <fields>; }

# don't obfuscate the fields in BracingButtonBar's ViewBinding class
-keep class com.something.databinding.BracingButtonBarBinding { <fields>; }
# Without this: BracingButtonBarBinding.class.getDeclaredFields() will not
# return the fields for the CheckImageButtons.
# This breaks BracingButtonBar.getButtons().

# don't obfuscate the fields in FretboardValuesDialog's ViewBinding class
-keep class com.something.databinding.FretboardValuesDialogBinding { <fields>; }
# Without this: will crash when raising the FretboardValuesDialog.

-keepattributes *Annotation*, Exceptions

# retain source file names and line numbers in stack traces
-keepattributes SourceFile, LineNumberTable

# de-obfuscating within inner classes doesn't seem to work, maybe this will help?
-keepattributes InnerClasses
Mycetozoan answered 25/4, 2023 at 20:24 Comment(1)
Ran into this too as I'd like to automate UI tests on CI on our release version to make sure that proguard doesn't cause problemsNovelize
A
10

As the error message refers to releaseAndroidTest the error seems to come from building the test and not the app.

You will then have to add the -dontwarn rules to the testProguardFiles alongside your proguardFiles, something like this:

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

See https://issuetracker.google.com/260300637 for more context.

Aelber answered 27/4, 2023 at 12:29 Comment(1)
That works! Thank you very much. I was not familiar with the 'testProguardFiles' method. I had resorted to turning off fullMode, but this is a far better solution.Mycetozoan

© 2022 - 2025 — McMap. All rights reserved.