0

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?

  • follow this post (https://stackoverflow.com/a/66456053/20812539), maybe it can help – VipulSharma Dec 26 '22 at 06:37
  • Couldn't the issue be that `ServiceLoader` on Android does not implement `findFirst`? See https://developer.android.com/reference/java/util/ServiceLoader. – sgjesse Jan 02 '23 at 08:28

2 Answers2

0

Try using

-keep class java.util.ServiceLoader{ *; }
DroidCoder
  • 41
  • 4
0

try

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

or

-dontoptimize class java.util.ServiceLoader
Sunny
  • 14,522
  • 15
  • 84
  • 129