Home > Software engineering >  Proguard / R8 removing a class member that it shouldn't
Proguard / R8 removing a class member that it shouldn't

Time:12-26

I'm not as experienced in the Proguard / R8 matters as others. But, I think that I've stumbled across a bug in the Proguard / R8 tool as used in Android Studio.

I have some Java code that runs great in a standalone app. When I try to use it in my Android project, I get a crash.

Process: com.example.fragmentbasics, PID: 8642 java.lang.NoSuchMethodError: No virtual method findFirst()Ljava/util/Optional; in class Ljava/util/ServiceLoader; or its super classes (declaration of 'java.util.ServiceLoader' appears in /apex/com.android.art/javalib/core-oj.jar)

When I look at the code in the debugger, it does appear as though the R8 tool has removed the method.

enter image description here

I was reading a https://www.guardsquare.com/blog/configuring-proguard-an-easy-step-by-step-tutorial about how Proguard quite often makes a mistake around reflection which is what this issue is.

I've included the following lines in my proguard-android.txt file

-keepclassmembers class java.util.ServiceLoader {
  <methods>;
}

But, running the code keeps throwing a method not found error.

Any ideas about how I could force Proguard / R8 to not remove this method?

CodePudding user response:

Try using

-keep class java.util.ServiceLoader{ *; }

CodePudding user response:

try

-keepclassmembernames class java.util.ServiceLoader {
  java.util.Optional findFirst();
}

or

-dontoptimize class java.util.ServiceLoader
  • Related