Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix "Couldn't desugar invokedynamic" when running androidConnectedTest

My Android app uses Java 8 lambdas like so:

myView.setOnClickListener(view -> someMethod());

Everything works fine with the above when building the app or running unit tests. However, when I run ./gradlew connectedAndroidTest, I get the following error:

Execution failed for task ':myModule:transformClassesWithDesugarForDebugAndroidTest'.

Exception in thread "main" java.lang.IllegalStateException: Couldn't desugar invokedynamic for com/.../MyActivity.onClick using java/lang/invoke/LambdaMetafactory.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; (6) with arguments [(Landroid/view/View;)V, com/.../MyActivity.lambda$onViewBound$0(Landroid/view/View;)V (7), (Landroid/view/View;)V]
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.visitInvokeDynamicInsn(LambdaDesugaring.java:474)
    at org.objectweb.asm.ClassReader.readCode(ClassReader.java:1623)
    at org.objectweb.asm.ClassReader.readMethod(ClassReader.java:1126)
    at org.objectweb.asm.ClassReader.accept(ClassReader.java:698)
    at org.objectweb.asm.ClassReader.accept(ClassReader.java:500)
    at com.google.devtools.build.android.desugar.Desugar.desugarClassesInInput(Desugar.java:477)
    at com.google.devtools.build.android.desugar.Desugar.desugarOneInput(Desugar.java:361)
    at com.google.devtools.build.android.desugar.Desugar.desugar(Desugar.java:314)
    at com.google.devtools.build.android.desugar.Desugar.main(Desugar.java:711)
Caused by: java.lang.IllegalAccessException: no such method: com...MyActivity.lambda$onViewBound$0(View)void/invokeSpecial
    at java.lang.invoke.MemberName.makeAccessException(MemberName.java:867)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1003)
    at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1386)
    at java.lang.invoke.MethodHandles$Lookup.findSpecial(MethodHandles.java:1004)
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.toMethodHandle(LambdaDesugaring.java:670)
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.toJvmMetatype(LambdaDesugaring.java:647)
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.visitInvokeDynamicInsn(LambdaDesugaring.java:408)
    ... 8 more
Caused by: java.lang.NoClassDefFoundError: com/.../databinding/MyActivityBinding
    at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
    at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:975)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1000)
    ... 13 more
Caused by: java.lang.ClassNotFoundException: Class com...databinding.MyActivityBinding not found
    at com.google.devtools.build.android.desugar.HeaderClassLoader.findClass(HeaderClassLoader.java:53)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 16 more

Note that removing the lambda makes it build again. Any idea on what could be happening and how to fix it?

like image 304
ADev Avatar asked Apr 21 '18 05:04

ADev


1 Answers

I had similar issues, problem was using of wrong dependency in classpath used in gradle file

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
        maven { url 'https://maven.fabric.io/public' }
        mavenCentral()
    }
    dependencies {
        classpath 'org.cryptonode.jncryptor:jncryptor:1.2.0'
        classpath 'io.fabric.tools:gradle:1.25.4'
        // The following adds the ability to use @DebugLog on methods
        // instead on 1.2.1 I was using 1.1.0 which was the problem
        classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
    }
}
like image 188
Swapnil Kadam Avatar answered Sep 23 '22 23:09

Swapnil Kadam