大家好
我知道关于这个问题有各种各样的问题,我也访问了很多,但是他们没有提供任何“解决方案”。
一般的答案是将语言级别设置为 8(允许 lambda),就像我为使用等级构建的 2 个模块所做的那样,见下文
和
我想确认我安装了 Java 8
java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)
当设置语言级别时,它解决了它在 IDE 中的问题,但是当构建我的项目以在我的设备上运行时,我得到这个错误:
Information:Gradle tasks [:app:assembleDebug]
/home/cybex/Documents/University/Year 5/Semester 2/WRAP302 - Advanced Programming 2/Assignments/Assignment1/Task1_SOS2/app/src/main/java/wrap302/nmu/task1_sos/SOSButton.java
Error:(15, 25) error: lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)
/home/cybex/Documents/University/Year 5/Semester 2/WRAP302 - Advanced Programming 2/Assignments/Assignment1/Task1_SOS2/app/src/main/java/wrap302/nmu/task1_sos/MainActivity.java
Error:(85, 34) error: lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1.054 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console
有什么想法吗?
更新
对于那些暗示这可能是 Gradle 问题的人:
项目 Gradle 构建
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="Task1_SOS2" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
模块 Gradle 构建
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "wrap302.nmu.task1_sos"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
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:26.0.0-alpha1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.android.support:gridlayout-v7:26.0.0-alpha1'
}
对于那些建议我需要更改我的项目语言 8(从 7)的人,我已经这样做了(图像作为证明的原因),但我的错误仍然存在,因此我的问题的原因除非我遗漏了一些东西普通网站。
附带问题:有些问题,每次我将语言级别更改为 8,然后进行 gradle 更改(即添加依赖项等)时,语言级别默认为 7,这正常吗?
最佳答案
我正在使用 Android Studio 3.0 Beta 4。在这个版本中,将以下几行添加到 build.gradle (Module:app) 中就可以了。另请参阅:https://developer.android.com/studio/write/java8-support.html
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
关于java - intellij 与 Android SDK : lambda expressions are not supported in -source 1. 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45361411/