以下是build.gradle文件的内容:
apply plugin: 'java'
archivesBaseName = 'foo-bar'
version = '1.0'
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
sourceSets.main.resources.exclude 'foo.jks'
compileJava{
println project.sourceCompatibility
println project.targetCompatibility
println sourceCompatibility
println targetCompatibility
}
Run Code Online (Sandbox Code Playgroud)
以下是运行Gradle jar任务的结果:
[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks:
[sts] :jar
[sts] -----------------------------------------------------
1.6
1.6
1.6
1.6
:compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
:processResources UP-TO-DATE
:classes
:jar
BUILD SUCCESSFUL
Total time: 1 mins 3.072 secs
[sts] -----------------------------------------------------
[sts] …Run Code Online (Sandbox Code Playgroud) 我有多个项目并使用Gradle 2.4构建.
我想覆盖org.gradle.java.home每个项目中的参数,并尝试为每个项目添加gradle.properties文件并覆盖参数.
我已经设置了主gradle项目org.gradle.java.home=C:/Java/jdk1.6.0并在子项目中覆盖为org.gradle.java.home=C:/Java/jdk1.7.0_45
但它没有按预期工作,我得到了
无效的源代码版本:1.7
错误信息.
有人可以提出如何解决这个问题的想法吗?
我有多项目 gradle 配置,它使用 java 7 进行构建。我正在添加一个必须使用 Java 6 构建的新项目。
从这个问答中,我尝试了提出的不同解决方案,但没有一个有效。
这是顶层的 build.gradle:
allprojects {
apply plugin: 'idea'
apply plugin: 'java'
repositories {
maven { url 'http://repo1.maven.org/maven2' }
mavenLocal()
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:-options"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试在子项目中确保源兼容性,但似乎没有效果。
更新:
我创建了一个多模块 gradle 项目的示例,其中一个子项目使用 Java 6,另一个子项目使用 java 8。我为每个子项目使用了org.gradle.java.home属性来gradle.properties为每个子项目设置 Java 主目录。这是github 存储库。
子项目包含使用 Java 8 中的 API 的每个类,使用 java 6 的子项目预计会失败,但构建运行成功。
这是父 build.gradle 文件:
group 'com.test' …Run Code Online (Sandbox Code Playgroud) 我使用Java 1.8来创建我的jar.
我可以在Java项目中使用它,但在Android项目中,我有以下错误:
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:1 error; aborting
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Android-tool\JAVA\jdk1.8.0_77\bin\java.exe'' finished with non-zero exit value 1
Information:BUILD …Run Code Online (Sandbox Code Playgroud) 我将我的Android应用程序移到了Android Studio而没有切换到Gradle.现在我想搬到Gradle.该应用程序在切换到Gradle之前在Android Studio中编译,但现在我已经设置了Gradle,它将不会编译String Switch语句或菱形运算符.我得到的错误是
Gradle: error: strings in switch are not supported in -source 1.6
(use -source 7 or higher to enable strings in switch)
Run Code Online (Sandbox Code Playgroud)
我通过打印确保我在JRE 7上运行
System.getProperty("java.version")
Run Code Online (Sandbox Code Playgroud)
在一项任务中.输出是
1.7.0_25
Run Code Online (Sandbox Code Playgroud)
让我最困惑的是"-source 1.6"和"use -source 7"之间的差异.但是我知道这两个都是Java sdk的名字所以也许标题只是混淆了.
我需要设置Gradle设置吗?或者这在Gradle中是不可能的?如果不是,那么为什么没有Gradle就可以解决这个问题.
应该注意的是,我的项目的无Gradle版本运行默认的Android Studio版本.我没有写一个ant脚本或maven脚本来构建它.其中一个可能是它的构建方式,但我没有任何项目特定的文件.只是Android Studio .iml文件.
更新 我尝试将以下内容添加到build.gradle android {}部分
compileOptions {
sourceCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
targetCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
}
Run Code Online (Sandbox Code Playgroud)
但.class文件无法构建,它不包含在apk中.请参阅此文章中的 "使用Java 1.6编译的Android项目需要库"部分
我正在使用LibGDX编写一个Java游戏,仅desktop作为目标平台.我刚刚创建了一个新的LibGDX项目,并将一些现有代码导入其中,但现在我遇到了一些错误.具体来说,使用diamond operator.例如,这行代码:
HashMap<String, Integer> connectionsMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
正在生成此错误:
Error:(20, 37) java: diamond operator is not supported in -source 1.6
(use -source 7 or higher to enable diamond operator)
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想使用Java 8.我导入的模块中的Gradle文件已保留,因此我将整个项目sourceCompatibility中所有其他build.gradle文件中的行更改1.6为1.8,并确保在Project Structure > Project > Language Level设置为Java 8时但是,我仍然遇到这个问题.我的项目使用Java 8还需要改变什么?
在我的 IntelliJ/Grade/Java 项目中设置sourceCompatibility和targetCompatibility到JavaVersion.VERSION_11没有预期的效果。
即使我设置项目SDK和项目语言水平在项目结构手动11,之后我恢复了8和6 刷新摇篮项目(或导入的变化)。见下图:
确保我创建了一个新项目并在其中测试这些。这是它的树视图:
唯一的build.gradle文件:
plugins {
id 'java'
}
group 'test'
version '1.0-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Run Code Online (Sandbox Code Playgroud)
Gradle 版本4.10.2与您在gradle-wrapper.properties文件中看到的一样:
#Tue Nov 13 00:53:45 IRST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
Run Code Online (Sandbox Code Playgroud)
这是环境信息:
IntelliJ IDEA 2018.1.5 (Community Edition) …Run Code Online (Sandbox Code Playgroud)