Gradle sourceCompatibility has no effect to subprojects

Solution 1:

It seems this behavior is caused by specifying the sourceCompatibility before apply plugin: 'java', which happens if you try to set the compatibility option inside allprojects.

In my setup, the situation can be solved by replacing:

allprojects {
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
}

with:

allprojects {
    apply plugin: 'java'
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
}

Will be glad if anyone else can verify this in a different setup.

I am still not sure whether this should be reported as a bug but I believe this solution is better than the work-around mentioned above (which has been very helpful however).

Solution 2:

Symptoms indicate that somewhere somebody is overwriting project.sourceCompatibility. But given that there are many ways to customize Gradle, I can't say from a distance who that is.

As a workaround, you can set the properties on the task level, which is what ultimately counts:

tasks.withType(JavaCompile) { 
    sourceCompatibility = "1.6"
    targetCompatibility = "1.6" 
}

Add this to allProjects { ... } block.

Solution 3:

You need to define compileJava tasks in build.gradle file, if you are using sourceCompatibility or targetCompatibility. Without compileJava tasks, both compatibility variables are displayed as unused variables in Intellij. I am using Gradle version 2.10.