4

My current configuration in gradle file is

sourceCompatibility = 1.7
targetCompatibility = 1.7

this specification make my project specific to java 1.7.

My requirement is that i should be able to build my project for different java version like for both java 1.7 and java 1.8

PS: like profile building in gradle where we can specify different java version.

devtest
  • 89
  • 1
  • 8

2 Answers2

4

Gradle uses whatever JDK it finds in the PATH or JAVA_HOME. So if you remove sourceCompatibility and targetCompatibility it should build the project with the Java version used on the machine where the project runs. Of course, this does mean that you shouldn't have constructs like lambdas in the code or compilation on Java 7 will fail.

You should also be aware that starting with Gradle 5 support for Java 7 will be dropped and it will run only with Java 8 (or newer).

Another way to do it is to specify the JDK at runtime, see this link

If you want to build for multiple Java versions at the same time, you can use something like this:

task compile7(type: JavaCompile) {
   source = fileTree(dir: 'src', include: '**/*.java')
   classpath = sourceSets.main.compileClasspath
   sourceCompatibility = 1.7
   targetCompatibility = 1.7
   destinationDir = file('build/java7')
}
task jar7(type: Jar, dependsOn: compile7) {
   from('build/java7')
   archiveName = 'project-JDK7.jar'
   destinationDir = file('build/jars')
}
task compile8(type: JavaCompile) {
   source = fileTree(dir: 'src', include: '**/*.java')
   classpath = sourceSets.main.compileClasspath
   sourceCompatibility = 1.8
   targetCompatibility = 1.8
   destinationDir = file('build/java8')
}
task jar8(type: Jar, dependsOn: compile8) {
   from('build/java8')
   archiveName = 'project-JDK8.jar'
   destinationDir = file('build/jars')
}

You can call gradle jar7 jar8 to build jars for both java versions you want. Of course, the code above can be improved to parametrize the operations, like this:

ext.javaVer = project.hasProperty('javaVer') ? project.getProperty('javaVer') : '1.7'
task compileJ(type: JavaCompile) {
   source = fileTree(dir: 'src', include: '**/*.java')
   classpath = sourceSets.main.compileClasspath
   sourceCompatibility = javaVer
   targetCompatibility = javaVer
   destinationDir = file('build/' + javaVer)
}
task jarJ(type: Jar, dependsOn: compileJ) {
   from('build/' + javaVer)
   archiveName = 'project-' + javaVer + '.jar'
   destinationDir = file('build/jars')
}

Using this format you would call gradle jarJ -P javaVer=1.8 and gradle jarJ to build for Java 8 and Java 7.

Slimu
  • 2,301
  • 1
  • 22
  • 28
  • Is there anyway through command line we can pass the java version for building project? – devtest Mar 21 '18 at 08:14
  • My requirement is to build one war file at a time. i was looking for some solution like this: i read the variable from command line and assign it's value to sourceCompability and targetCompability – devtest Mar 21 '18 at 09:30
  • If gradle uses the jvm on my machine which is 1.8 then why is it failing to build when my app references methods that are only supported starting with Android N and why do I explicitly have to declare targetCompatibility to 1_8 ? – isJulian00 Jun 15 '19 at 20:30
  • You can omit targetCompatibility if it's the same as the sourceCompatibility. I can't say why your build fails without some exception. Also, does it fail because a method from the JKD is missing or from the Android API? Also see this https://stackoverflow.com/questions/35928580/android-n-requires-the-ide-to-be-running-with-java-1-8-or-later – Slimu Jun 18 '19 at 09:23
2

This is how i had solved it

ext.javaVer = project.hasProperty('javaVer') ? project.getProperty('javaVer') : '1.7'
sourceCompatibility = javaVer
targetCompatibility = javaVer

while building project

gradle build -P javaVer=1.8
devtest
  • 89
  • 1
  • 8
  • More recent `gradle` versions may require the `JavaVersion` enum be used, so instead try: ```gradle ext.javaVer = project.hasProperty('javaVer') ? JavaVersion.toVersion(project.getProperty('javaVer')) : JavaVersion.VERSION_1_7``` – jonp Jun 05 '20 at 16:25