Gradle using 2 different JDK

我们两清 提交于 2021-02-07 22:01:40

问题


I would like use some features from Java 9 and 10 in my PC java application which has common code with android app. When I use jdk 10 or jdk 9 android application is building, but it doesn't launch(it is throwing a lots of errors). When I use jdk 8 for whole project everything is working correctly without any error. When I manually build project using 2 different jdk everything is working fine. I tried set targetCompability and sourceCompability for android application for JavaVersion.Version_1_7 but it doesn't help. I tried use different jdks for java 9 and java 10 but it doesn't help with this problem.

I would like build android application and common component with jdk8 and other components with jdk10. Is it possible to force gradle to use different jdk for specific project without using external tools like bash?

My project structure looks like:

build.gradle
common-component(jdk8)/build.gradle
PC(jdk 10)/build.gradle
device-Android(jdk 8)/build.gradle

回答1:


I found workaround. It works fine with gradle 4.7(Java 10 support was added in this release). This hack/workaround requires to launch project using lower JDK like Oracle JDK 8 or OpenJDK. We can build some components using higher version of JDK, but we can't build JDK project with JDK10 and then use it with JDK8 if we specify targetcompablility higher than 1.8. It will work only for java project and probably it won't work for android plugin and other JVM languages.

Part of build.gradle for PC application :

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
    }
}


来源:https://stackoverflow.com/questions/50349011/gradle-using-2-different-jdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!