skip to Main Content

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?

2

Answers


  1. Try using

    -keep class java.util.ServiceLoader{ *; }
    
    Login or Signup to reply.
  2. try

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

    or

    -dontoptimize class java.util.ServiceLoader
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search