NoSuchMethodError with Android Lambdas

I am trying to use Lambda expressions in my Android project (min. Sdk: 16, target sdk: 25), but in many problems.

The first problem is that I develop and debug my application using the emulator, deploying and fixing errors several times when the application suddenly stops loading.

I get the following stack in my log:

java.lang.NoSuchMethodError: there is no direct method (Ljava / lang / Object;) V in the class Lcom / androidtest / - $ Lambda $ 1; or its superclasses (the declaration "com.androidtest.- $ Lambda $ 1" appears in /data/app/com.androidtest-2/base.apk)

I have enabled the use of the lambda expression, as described in the Android documentation, with the following code in the app / build.gradle file:

android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.gfs.mp3lab" minSdkVersion 16 targetSdkVersion 25 ... } ... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } 

I accepted Parth Pandya's suggestion and added the jackOptions parameter to the build.gradle file and believed that to fix the problem, but after recovering my project, I now sproadically get another error:

java.lang.IncompatibleClassChangeError: class' com.gfs.jotsalot.- $ Lambda $ 1 'does not implement the interface' java.lang.Runnable 'in a call to' void java.lang.Runnable.run () '(declaration' android.os. Handler "appears in /system/framework/framework.jar) on android.os.Handler.handleCallback (Handler.java:751) on android.os.Handler.dispatchMessage (Handler.java:95)

Replacing the lambda expression seems to fix the problem, so move on to

() -> { Log.i(TAG, "Hey There"); }

to

 new Runnable() { @Override public void run() { Log.i(TAG, "Hey There"); } } 

works. I do this in a multi-threaded application, so I'm not sure if this is the cause of the error, and so far I have only tried it on the emulator. Restoring a project fixes the problem, but it is very time consuming, and this problem is harmful in order to remove everything from my project.

So far, I have only tested inside the emulator and am not sure if this will affect the true telephone environment. Since these lambda expressions work very well (until they do), I think we can safely conclude that this is a mistake. I'm just wondering if this is known, and if there are any workarounds.

+5
source share
2 answers

Parth's answer was helpful, but in the end I kept getting runtime errors using lambda expressions no matter what I seemed to do. Since the Intellij-based IDE is a visualization of implementations of interfaces with one functionality as lizards, I decided to simply remove all of them from my project, rather than deal with a headache.

+2
source

In your build.gradle file you do not have jackOptions enabled to true, just add this to your defaultConfig as shown below and I think it should work.

 android { defaultConfig { applicationId "com.gfs.mp3lab" minSdkVersion 16 targetSdkVersion 25 jackOptions { enabled true } ... } ... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } 
+3
source

Source: https://habr.com/ru/post/1263902/


All Articles