我正在测试Java 8的一些新特性,并将示例复制到我的IDE(最初是Eclipse,然后是IntelliJ),如下所示

Eclipse对lambda表达式不提供任何支持,IntelliJ不断报告错误

此语言级别不支持Lambda表达式

我想知道这是否是我的安装、代码或支持的问题。


当前回答

只需在build中添加compileOptions即可。Gradle你的应用:

android {


...

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

其他回答

对于intellij 13, 使用下面的导航,简单地将Project语言级别本身更改为8.0。

File
|
|
 ---------Project Structure -> Project tab
          |
          |________Project language level

模块郎水平

当java编译器没有maven插件时,我还必须更新Modules lang级别。

File
|
|
 ---------Project Structure -> Modules tab
          |
          |________ language level

但如果已经有maven插件,这个Module lang级别将自动固定,

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>

在改变之后,一切看起来都很好

实际上,Eclipse对Java 8的支持是可用的:请参见这里和这里。

对于开普勒来说,它是针对SR2(即Eclipse 4.3.2)的“功能补丁”。 对于Luna (Eclipse 4.4),它将出现在标准发行版中。(发布时间表)

此解决方案适用于Android Studio 3.0或更高版本。

文件>项目结构>模块>应用>属性选项卡

将Source Compatibility和Target Compatibility都更改为1.8

编辑配置文件

您也可以在相应的版本中直接配置它。gradle文件

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

这个答案对新更新的intelliJ不起作用…为了新智能…

进入——> file——> Project Structure——> Global Libraries ==>设置为JDK 1.8或更高版本。

also File——> project Structure——> projects ===> set "project language manual" = 8

——>项目结构——>项目sdk ==>

这将为您的项目启用lambda表达式。

同样的项目(Android Studio 3.3.2, gradle-4.10.1-all.zip, compileSdkVersion 28, buildToolsVersion '28.0.3') 在新的快速Windows机器上运行良好,在旧的Ubuntu 18.04笔记本电脑上用红色下划线标出Java 8的内容(但是项目在Ubuntu上编译没有错误)。

我只改变了两件事,迫使它停止用红色下划线是排除增量true和dexOptions

compileOptions {
//    incremental true
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

//dexOptions {
//    javaMaxHeapSize "4g"
//}

在应用级build.gradle中。