Home » Android » java – NoSuchMethodError Using an API to connect to server-Exceptionshub

java – NoSuchMethodError Using an API to connect to server-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I am back into Android. It has been many years since I have tried to do programming for Android. I have years of experience for Java, but when it comes to Android I consider myself a newbie.

I have gotten a simple Android App up and running using an Emulator with Android Studio.

When I try to use our inhouse API to connect to a server, I am getting an error that I do not see when using the API in a Java App.

2020-02-18 10:49:08.723 11033-11033/com.company.monitor E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.company.monitor, PID: 11033
    java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; in class Ljava/lang/invoke/LambdaMetafactory; or its super classes (declaration of 'java.lang.invoke.LambdaMetafactory' appears in /apex/com.android.runtime/javalib/core-oj.jar)
        at com.company.client.api.PingPongSender.setupExecutorThread(PingPongSender.java:68)
        at com.company.client.api.PingPongSender.<init>(PingPongSender.java:64)
        at com.company.client.api.ServiceContext.<init>(ServiceContext.java:34)
        at com.company.monitor.MainActivity.onCreate(MainActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:7802)

The PingPongSender.java:68 is calling an ExecutorService. It is a class that is responsible for sending Ping to the server and receives Pong back. The API sends and receives Google Protobuf messages.

executor = Executors.newSingleThreadExecutor(new ThreadFactory("Executor"));
executor.execute(() -> {
    boolean doRun = true;
    while (doRun) {
        try {
            Runnable task = jobQueue.take();
            task.run();
        } catch (InterruptedException e) {
            logger.log(Level.WARN, "Failed to execute runnable or service was interrupted.", e);
            doRun = false;
        }
    }
});

Running Android Studio 3.5.3. Emulator “Nexus 5X API 29 x86”, Java 8.

Edit: The PingPongSender at pastebin https://pastebin.com/5YH51iv5

Edit: Found a similar question that had an answer which got me to recheck if my App was using Java 8 compatibility. I should have seen it sooner. I used several search phrases, but alas no results previously.
No static method metafactory

How to&Answers:

AFAIU There seems to be 2 types of newSingleThreadExecutor as mentioned below

  1. public static ExecutorService newSingleThreadExecutor ()
    https://developer.android.com/reference/java/util/concurrent/Executors#newSingleThreadExecutor()

  2. public static ExecutorService newSingleThreadExecutor (ThreadFactory threadFactory)
    https://developer.android.com/reference/java/util/concurrent/Executors#newSingleThreadExecutor(java.util.concurrent.ThreadFactory)

Solution could be change your

executor = Executors.newSingleThreadExecutor("Executor");

to

executor = Executors.newSingleThreadExecutor();