How to Specify the Required Java Version in a Gradle Build

How do I tell Gradle to use specific JDK version?

Two ways

  1. In gradle.properties in the .gradle directory in your HOME_DIRECTORY set org.gradle.java.home=/path_to_jdk_directory

or:


  1. In your build.gradle

     compileJava.options.fork = true
    compileJava.options.forkOptions.executable = '/path_to_javac'

gradlew uses wrong Java version

A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.

https://developer.android.com/studio/intro/studio-config.html#jdk

This however, is only for building your apps within Android Studio, not from the terminal with gradlew, otherwise, it'll use whatever is on your OS's $PATH variable.

In order to use the embedded JDK, you at least need to set JAVA_HOME, for example on Linux/Mac,

$ export JAVA_HOME=/path/to/AndroidStudio/jdk  # TODO: Find this
$ ./gradlew

My recommendation, however, is to use the mechanism for your OS for installing Java.

For easy Java library management (on Linux & Mac), you can try using sdkman

How to enforce a java compiler version with gradle?

I use the following:

task checkJavaVersion << {
if (!JavaVersion.current().isJava6()) {
String message = "ERROR: Java 1.6 required but " +
JavaVersion.current() +
" found. Change your JAVA_HOME environment variable.";
throw new IllegalStateException(message);
}
}

compileJava.dependsOn checkJavaVersion

Azure build error: Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8

My error is solved by adding below lines into azure-pipelines.yml file:

steps:
- task: JavaToolInstaller@0
inputs:
versionSpec: '11'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'


Related Topics



Leave a reply



Submit