How to specify source and target compatibility in Java module?

霸气de小男生 提交于 2019-11-29 10:05:36

I think that the last one(declaration in the root) is the correct one and it has to work properly, though it causes warnings in IDE. It's nowhere said, what is the prefered way to declare it and there is only one example in the official docs, where it's used as:

sourceCompatibility = 1.6

in the root of the build.gradle.

Jeroen Wijdemans

Compatibility needs to be specified inside the compileJava block

compileJava   {
  sourceCompatibility = '1.8'
  targetCompatibility = '1.8'
}

IntelliJ gives this warning

Access to 'sourceCompatibility' exceeds its access rights

if you use a double parameter.

When you make the value a string (e.g. quoted) the warning will disappear.

--edit--

the warning only appears using 1.8 in IntelliJ 2016. In 2017 the warning is gone using either 1.8 or '1.8'

(As pointed out by the OP, this answer doesn't really fit the question. I'd like to keep it here at the bottom though, just in case somebody else lands on this page searching for the same problem as me)

For android, the compileJava block that Jeroen Wijdemans suggested, does not work. What does work is specifying in the app build.gradle

// https://developer.android.com/studio/write/java8-support.html
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

Currently, intellij 2017.2.6 tells me then that you need to enable Jack. So let's do that as well, by adding the following inside the android block:

    // https://stackoverflow.com/questions/37004069/errorjack-is-required-to-support-java-8-language-features
    jackOptions {
        enabled true
    }

If building now results in errors or warnings, simply rebuilding the whole project might get rid of them. (It did work for me)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!