lambda expressions are not supported at this language level(需设置project language level)

在jdk配置都正确的情况下,当使用lambda表达式时,出现异常lambda expressions are not supported at this language level


原因:编译时所需要的语言版本和代码所需要的不对应


解决方式:

方式一

在设置中配置

File --->project structure---> project language level

lambda expressions are not supported at this language level(需设置project language level)_第1张图片


lambda expressions are not supported at this language level(需设置project language level)_第2张图片

方式二

在build.gradle中添加

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
}

lambda expressions are not supported at this language level(需设置project language level)_第3张图片



将编译的目标与源版本改为1.8后,此时报异常:Process 'command 'D:\soft\jdk\jdk1.8.0_73\bin\java.exe'' finished with non-zero exit value 1。

原因:Android studio默认使用Lambda表达式会报错,即使使用的jdk是1.8.需要借助一个插件retrolambda,该插件将java8种的lambda表达式特性兼容到java5


1.在project 的build.gradle中加入
classpath 'me.tatarka:gradle-retrolambda:3.2.0'

lambda expressions are not supported at this language level(需设置project language level)_第4张图片
2.在module的build.gradle中加入
apply plugin: 'me.tatarka.retrolambda'

lambda expressions are not supported at this language level(需设置project language level)_第5张图片

你可能感兴趣的:(异常)