javaintellij-ideagradleintellij-15

Gradle refresh in IntelliJ changes language level of modules


I have a Gradle project that consists of an Android library module and a plain old Java module. (The Android library module has a dependency on the plain Java module.)

In the build.gradle file of the Android library module I have this:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

And in the build.gradle file of the plain old Java module I have this:

compileJava {
    sourceCompatibility = "1.8"
    targetCompatibility = "1.8"
}

When I hit the Gradle refresh button in IntelliJ IDEA 15, the language level for the Android module is set to Java 8 (as I'd expect) but the language level of the Java module is set to Java 1.6. Why is this and is there any way for IntelliJ to set the language level of the Java module to Java 8 on a refresh instead?


Solution

  • You can only use JavaVersion.VERSION_1_8 if you are using the new Jack compiler or the Retrolambda plugin for Gradle.

    So you should be using:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    

    As far as the Intellij/Android Studio language settings goes:

    1. File -> Project Structure:

    enter image description here

    1. You should set the project language settings accordingly:

    enter image description here