不能在Android中使用带有Java 1.8的lambda函数

我正在使用的库是bychan ,我甚至无法将它包含在Nougat空应用程序项目中,无论是作为模块加载还是通过jar加载。

所以这是Android Studio消息中返回的内容的示例:

有人可以在下面的错误中解释“未知界面”的含义吗?

Error:TokenDefinitionBuilder.java:118-119: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Function Error:TokenDefinitionBuilder.java:118-119: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Predicate ... Error:com.android.jack.JackAbortException 

这来自我用过的图书馆。 我认为在尖括号中添加类型会有帮助吗? 我不知道它是否做了什么。

这是该库中的第118-119行:

 strings.stream().map(StringMatcher::new).map( m -> m.tryMatch(input, searchStart)).filter(Objects::nonNull).findFirst().orElse(null)); 

我不确定它在抱怨什么? 我该怎么办或改变以逃避这个问题? 我原以为Android可能不支持Lambda表达式,但错误清楚地表明“Lambda来自…”,这意味着它认识到我使用了lambda表达式。

请帮忙。


更新

我已经通过在另一个jar库中的jar中编译它来包含库 ,如果这有帮助的话。 如果你不知道的话,我是Android新手。

更新2

我认为这与API版本无关? 我已将构建目标设置为25 。 但构建仍然失败。

另外,如果你看下面,我没有看到1.8的选项? 但我已将构建JDK设置为我的1.8 JDK位置

在此处输入图像描述

在此处输入图像描述


摇篮

 apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.0" defaultConfig { applicationId "this.is.where.i.have.my.id" minSdkVersion 25 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" jackOptions { enabled true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.1' compile 'com.android.support:support-v4:25.0.0' compile 'com.android.support:design:25.0.0' compile 'com.android.support:preference-v7:25.0.0' compile 'com.android.support:preference-v14:25.0.0' compile 'com.google.android.gms:play-services-appindexing:9.8.0' testCompile 'junit:junit:4.12' compile 'com.google.code.gson:gson:2.8.0' compile files('libs/my_lib_that_contains_lambda.jar') } 

Android SDK <24不支持java.util.function.Functionjava.util.function.Predicate接口,因为Java 8允许function接口包含仅在SDK 24及更高版本中支持的默认方法 。 当我使用消费者时,我遇到了这个问题,我修复它的方法是创建我自己的java.util.function.Consumer接口的小丑,没有所有默认方法。 您还可以通过将应用的最低SDK增加到24来解决此问题。

更新

将以下内容添加到build.gradle文件中

 defaultConfig { jackOptions { enabled true } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } 

我不建议使用java 8.现在不支持它。 但是,甚至,它会:记住,每个设备都有自己的java机器,不会更新。 我的意思是,如果你想确保你的应用程序正常工作将你的项目设置为java 6.旧设备从不支持最新的JMV

也请阅读此讨论

Android Studio需要哪个JDK版本(语言级别)?

除了Java JDK 1.8部分外,整体设置看起来不错。您需要在Android Studio中设置正确的JDK安装文件夹路径,以便使用所有新的Java 8function。 请验证您的JDK位置