简体   繁体   English

如何让 Gradle 编译使用较新 Java 版本的源库?

[英]How do I make Gradle compile a source libary that uses newer java version?

I have a gradle project that is compiled for Java 7, and I'm told this project cannot be updated to use a newer version of Java because of a bug introduced in later java versions.我有一个为 Java 7 编译的 gradle 项目,我被告知这个项目无法更新为使用较新版本的 Java,因为在更高版本的 Java 版本中引入了一个错误。

I am trying to get this project to use a library that is compiled via java 8, and as a result I'm getting a major minor version 52 conflict during run time.我试图让这个项目使用通过 java 8 编译的库,结果我在运行时遇到了一个主要的次要版本 52 冲突。

Is there a way to tell gradle to try and compile this library in compatability mode with Java 7?有没有办法告诉 gradle 尝试在与 Java 7 兼容的模式下编译这个库?

Right now, to get it to build, I'm using this line in my build.gradle:现在,为了构建它,我在 build.gradle 中使用了这一行:

sourceCompatibility = 1.7
targetCompatibility = 1.7

compile(group: 'com.blah.example', name: 'some-java-8-library', version: '1.0') {
    transitive = false
}

The problem you'll run into is less of a Gradle problem, and more of a Java problem.您将遇到的问题不是 Gradle 问题,而是 Java 问题。 If your original library jar was build with targetCompatibility = 1.8 , you will not be able to use it on your Java7 JVM.如果您的原始库 jar 是使用targetCompatibility = 1.8构建的,您将无法在 Java7 JVM 上使用它。 You will see the version conflict error message.您将看到版本冲突错误消息。

If you however have access to the library source, you can try building from source with targetCompatibility = 1.7 and then use the resulting jar with Java7 JVM.但是,如果您可以访问库源代码,则可以尝试从targetCompatibility = 1.7源代码构建,然后将生成的 jar 与 Java7 JVM 一起使用。

The complication here, unlike earlier versions of java is that the java compiler will force you to use targetCompatibility=1.8 if you set sourceCompatibility to 1.8 - which means, if your library source uses any 1.8 features like Lambdas, you will not be able to compile to targetCompatibility = 1.7这里的复杂之处在于,与早期版本的 java 不同,如果您将 sourceCompatibility 设置为 1.8,java 编译器将强制您使用targetCompatibility=1.8 1.8 - 这意味着,如果您的库源使用任何 1.8 功能,如 Lambdas,您将无法编译目标targetCompatibility = 1.7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编辑 android studio Java 代码以在较新版本的 Gradle 上工作? - How do I edit an android studio Java code to work on a newer version of Gradle? 我可以用较新的JDK用ant编译Java较旧版本的Java,以便在编译使用较新API的代码时生成输出吗? - Can i compile java for an older Java version with ant with a newer JDK so it generates output when it compiles code that uses the newer API? 如何让rJava在osx上使用更新版本的java? - How can I make rJava use the newer version of java on osx? 如何更改Eclipse SpringSource插件使用的Gradle版本? - How do I change the Gradle version the Eclipse SpringSource plugin uses? 如何编译使用JAR的Java代码? - How do I compile Java code that uses JAR? Gradle - 使用不同的 java 源/编译版本将特定文件夹添加到 JAR - Gradle - Add specific folder to JAR with different java source/compile version 由于 Gradle 版本 I 没有和来源 8,Gradle 构建失败 - Gradle Build Failing Due to Gradle Version I Do Not Have and Source 8 如何在 Gradle 构建中指定所需的 Java 版本? - How do I specify the required Java version in a Gradle build? 如何编译Java源文件? - How do I compile my Java source files? 如何编译将另一个Java类用作对象的Java类? - How do I compile a Java class which uses another Java class as an Object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM