Lambda expressions crash with IncompatibleClassCha

2019-03-18 03:28发布

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)

1条回答
Rolldiameter
2楼-- · 2019-03-18 04:15

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

查看更多
登录 后发表回答