Set Java Source Compatibility and Target Compatibility in Gradle

This article will cover shortly how to set Java source compatibility and target compatibility in Gradle.

1. Overview

When we use Gradle to build our Java project, we often have to define the Java source language level and the target level of the bytecode that is generated. Gradle provides us a  plugin called Gradle Java plugin which has two properties for that:

  • sourceCompatibility: The Java version compatibility to use when compiling Java source
  • targetCompatibility: Java version to generate classes for

2. Set Java source compatibility and target compatibility in Gradle

We can define the sourceCompatibility and targetCompatibility in the gradle.build file. Below is an example that we set Java 8 for both of them:

If we don’t set values for those properties, the default value of sourceCompatibility will be the version of the current JVM in use, and the default value of targetCompatibility will be the same with sourceCompatibility.

3. Summary

In similar to Maven or other build tools, we can set Java source compatibility and target compatibility in Gradle. This is very flexible for us in controlling the level of source code and target generated output. Below are other related articles related to Gradle, you can reference if you’re interested in

Gradle Tutorials

Install Gradle on Windows

Install Gradle on Ubuntu 16.04 LTS (Xenial Xerus)

Where is Gradle Cache Location

Gradle Proxy Settings

Specify The Build File in Gradle

Using Gradlew, The Gradle Wrapper

Refresh or Redownload Dependencies in Gradle