简体   繁体   中英

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.

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.

Is there a way to tell gradle to try and compile this library in compatability mode with Java 7?

Right now, to get it to build, I'm using this line in my 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. If your original library jar was build with targetCompatibility = 1.8 , you will not be able to use it on your 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM