To specify the minimum Java version required for a Gradle project, you can use the sourceCompatibility
and targetCompatibility
properties in the java
block of your build file.
For example, to require Java 8 or higher, you can set the sourceCompatibility
and targetCompatibility
properties to 1.8
:
java { sourceCompatibility = '1.8' targetCompatibility = '1.8' }
This will tell Gradle to use Java 8 for both the source and the target compatibility, and will cause the build to fail if the required version is not available on the build machine.
You can also use the JavaVersion.current()
method to check the current Java version at runtime and ensure that it meets the minimum requirement:
if (JavaVersion.current().isJava8Compatible()) { // Java 8 or higher is available, proceed with the build } else { // Java 8 or higher is not available, fail the build throw new GradleException('Java 8 or higher is required to build this project') }
For more information about the java
block and the options that it supports, you can refer to the Gradle documentation for the Java plugin.