How to set language level in Gradle? (so this is an IDE agnostic)

I would like to set the java language level in gradle in IDE agnostic mode.

sourceCompatibility = 1.x at the root level seems deprecated in Gradle 2.21.

(edit: or is it? IntelliJ gives me a groovy validation error)

So, I found this to work.

 idea { project { languageLevel = '1.7' } } 

But no configuration here connects Gradle with IntelliJ IDEA, due to the structure of idea { } ...

Is there a way to do this in IDE agnostic mode?

I want my Gradle build script to run in any IDE (be it IntelliJ IDEA or Eclipse) or on Jenkins (or something else).

+9
java intellij-idea gradle
source share
5 answers

If you use Gradle on the command line, the language level works fine, as indicated in the build.gradle file.

But when a java project is imported into the IntelliJ IDEA wap file, the Gradle plugin is responsible for creating the IDEA project settings files.

Unfortunately, the plug-in does not respect the build.gradle sourceCompatibility / targetCompatibility property, instead it uses the IDEA setting: File -> Other Settings -> Default Project Structure -> Project Language Level -> 6 (By default). enter image description here

So, I think this is a Gradle idea plugin error. see https://issues.gradle.org/browse/GRADLE-2198

Currently, I sometimes have to change the language level in the previous dialog.

+3
source share

A way to do this to build the CLI is shown. However, I am not sure that each IDE will pick it up.

 allprojects { tasks.withType(JavaCompile) { sourceCompatibility = '1.7' targetCompatibility = '1.7' } } 
+7
source share

If they are defined in compileJava , you will not receive a warning, and IntelliJ and gradle both respect it.

 compileJava { sourceCompatibility = '1.7' targetCompatibility = '1.7' } 

As with any change to the gradle file, you will need synchronization so that IntelliJ can receive the changes.

Sync

+2
source share

inside android closing your build.gradle inside your application module:

 android { compileSdkVersion 27 buildToolsVersion "26.0.2" // etc // other things // add this compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } 
+1
source share

Adding this as an answer since I got this from the comments:

Instead of using sourceCompatibility / targetCompatibility in the compileJava task, use:

 project.sourceCompatibility = '1.7' project.targetCompatibility = '1.7' 

in configuration for relevant projects

0
source share

All Articles