Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expressions crash with IncompatibleClassChangeError in Android when using jack

I am using Java 8 in my Android project. I have setup both Jack (In android application module) and Retrolambda (in other modules).

The problem I am having is that my Lambda expressions crash in one specific scenario when I try to make it use class variable (and I can reproduce it in any module), in all others situations it works as expected. Maybe this is a standard Java behaviour, but I couldn't find any explanations so far. Would anyone know where the problem is? My class and crash below:

public class LambdaBugTest {

    private final String parentClassVariableString = "parentClassVariableString";

    public void testLambdas() {

        // ----------- THESE WORK OK --------------
        final Functional simpleLambdaWorks = text -> System.out.print(text);
        final Functional methodReferenceWorks = System.out::print;
        final Functional interfaceWithClassParamWorks = new Functional() {
            @Override
            public void doSomething(String text) {
                System.out.print(text + " " + parentClassVariableString);
            }
        };
        // -----------------------------------------

        // ----------- THIS ONE CRASHES ------------
        final Functional lambdaWithClassParamCrashes = text -> System.out.print(text + " " + parentClassVariableString);
        // -----------------------------------------

        simpleLambdaWorks.doSomething("Text from simpleLambdaWorks");
        methodReferenceWorks.doSomething("Text from methodReferenceWorks");
        interfaceWithClassParamWorks.doSomething("Text from interfaceWithClassParamWorks");
        lambdaWithClassParamCrashes.doSomething("Text from lambdaWithClassParamCrashes");
    }

    private interface Functional {
        void doSomething(String text);
    }
}

The crash log:

08-21 00:27:41.564 25752-25752/debug E/AndroidRuntime: FATAL EXCEPTION: main
        Process: debug, PID: 25752
        java.lang.IncompatibleClassChangeError: The method 'void data.api.mapper.LambdaBugTest.data_api_mapper_LambdaBugTest_lambda$testLambdas$1(java.lang.String)' was expected to be of type direct but instead was found to be of type virtual (declaration of 'java.lang.reflect.ArtMethod' appears in /system/framework/core-libart.jar)
        at data.api.mapper.LambdaBugTest.access$lambda$1(LambdaBugTest.java)
        at data.api.mapper.LambdaBugTest$$Lambda$5.doSomething(Unknown)
        at data.api.mapper.LambdaBugTest.testLambdas(LambdaBugTest.java:27)
        at home.presentation.HomeActivity.onCreate(HomeActivity.java:47)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
like image 368
Team Avatar asked Aug 20 '16 23:08

Team


People also ask

Does Java 7 support lambda expressions?

From the Retrolambda documentation: Retrolambda lets you run Java 8 code with lambda expressions and method references on Java 7 or lower. It does this by transforming your Java 8 compiled bytecode so that it can run on a Java 7 runtime. After the transformation they are just a bunch of normal .

Can lambda expressions be used for all interfaces?

From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods.

What is use of lambda expression in Android?

Lambda expressions provide a shorthand syntax to write functions. You can pass function types into other functions. You can return a function type from another function. A lambda expression returns the value of the last expression.

How is lambda Expresssion represented by the JVM at runtime?

The bootstrap method information is needed for JVM to construct object representation of lambda during runtime. Lambda body code is generated within an instance or static private method which has the same parameters and return type as lambda's functional interface abstract method.


1 Answers

Google are no longer supporting android-jack.

Jack is no longer supported, and you should first disable Jack to use the Java 8 support built into the default toolchain

Android Studio 3.0 and later supports Lambdas expressions. Configure build.gradle file:

android {
...
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
 }
}

Note: If Android Studio detects that your project is using Jack, Retrolambda, or DexGuard, the IDE uses Java 8 support provided by those tools instead. However, consider migrating to the default toolchain

like image 159
flutter Avatar answered Oct 01 '22 15:10

flutter