ndroid中的IncompatibleClassChangeE

ndroid中的IncompatibleClassChangeE

本文介绍了使用jack时,Lambda表达式在Android中的IncompatibleClassChangeError崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android项目中使用Java 8。我已经设置了Jack(在Android应用程序模块中)和Retrolambda(在其他模块中)。

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

我遇到的问题是我的Lambda表达式在一个特定的情况下崩溃了我试图让它使用类变量(我可以在任何模块中重现它),在所有其他情况下它按预期工作。也许这是标准的Java行为,但到目前为止我找不到任何解释。有谁知道问题在哪里?
我的课程和崩溃如下:

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);
    }
}

崩溃日志:

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)


推荐答案

Google不再支持android-jack。

Google are no longer supporting android-jack.

Android Studio 3.0及更高版本支持Lambdas表达式。配置build.gradle文件:

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
 }
}



这篇关于使用jack时,Lambda表达式在Android中的IncompatibleClassChangeError崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 05:48