我在Android项目中使用Java 8。我已经设置了Jack(在Android应用程序模块中)和Retrolambda(在其他模块中)。
我遇到的问题是,当我尝试使用类变量(并且我可以在任何模块中重现它)时,我的Lambda表达式在一个特定场景中崩溃,在所有其他情况下,它按预期工作。也许这是标准的Java行为,但到目前为止我找不到任何解释。有谁知道问题在哪里? 我的班级和崩溃在下面:
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)
答案 0 :(得分:1)
Google不再支持android-jack。
不再支持Jack,您应首先禁用Jack以使用默认工具链中内置的Java 8支持
Android Studio 3.0及更高版本支持Lambdas表达式。配置build.gradle文件:
// String stream definitions
#include<sstream>
for (int rank_index = 0; rank_index < 13; rank_index++) {
for (int suit_index = 0; suit_index < 4; suit_index++) {
std::string l_file_name;
std::stringstream s_l_file_name;
s_l_file_name << ranks[rank_index] << suits[suit_index] << ".png";
s_l_file_name >> l_file_name;
// Remainder of the code
}
}
注意:如果Android Studio检测到您的项目使用的是Jack,Retrolambda或DexGuard,则IDE会使用这些工具提供的Java 8支持。但是,请考虑迁移到默认工具链