Using jdk8 and jdk10 in one build

I’m trying build multiple projects with jdk8 and jdk10. Some projects has common code with android application so most of projects should be build with jdk8. I tried build one application with jdk10, but it is failing.
I tried use solutions like:

  • Setting org.gradle.java.home=/usr/lib/jvm/java-10-oracle in gradle.properties in subfolder.
  • options.forkOptions.executable = project.property(‘javacPath’)
    I got error:
    Could not target platform: ‘Java SE 10’ using tool chain: ‘JDK 8 (1.8)’.
  • I tried use options.bootClasspath, but it is deprecated and JDK10 doesn’t support bootClasspath

My project structure looks like:
build.gradle
settings.gradle
common-component(jdk8)/build.gradle
PC(jdk 10)/build.gradle
PC(jdk 10)/build.properties
device-Android(jdk 8)/build.gradle

I use:
Gradle 4.7
Groovy: 2.4.12
Ant: Apache Ant™ version 1.9.9 compiled on February 2 2017
JVM: 1.8.0_171 (Oracle Corporation 25.171-b11)
OS: Linux 4.13.0-41-generic amd64

I tried this a couple of times over the years but always ran into a problem I couldn’t solve (slow build, didn’t work on CI, some task didn’t support configuring a JDK, etc.). My conclusion was that it’s not practical to use a JDK other than the one used to run Gradle. Happy to be proven wrong.

I found workaround using this issue from gradle github. Support forked compilation with Java 9 · Issue #3035 · gradle/gradle · GitHub
I know it won’t work for every project with every JVM language, but in my case I use only java in my project so it is not huge problem. Additionally I will keep this solution until android will fully support Java 10 or next LTS release of java.

PC(jdk 10)/build.gradle:

project (‘:pc-client’) {
dependencies {
compile project(‘:net-default’)
testcompile JUNIT
}
compileJava {
options.fork = true
options.forkOptions.javaHome = file(‘/usr/lib/jvm/java-10-oracle’)
targetCompatibility = 1.10
sourceCompatibility = 1.10
}

compileTestJava {
options.fork = true
options.forkOptions.javaHome = file(‘/usr/lib/jvm/java-10-oracle’)
targetCompatibility = 1.10
sourceCompatibility = 1.10
}
}