Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify source and target compatibility in Java module?

I have a Gradle project consisting of an Android module (the com.android.library plugin is applied in the build.gradle file) and a Java module (the java plugin is applied in the build.gradle file). Within the build.gradle file of the Java module I was previously specifying the source and target compatibility as follows:

apply plugin: 'java'

compileJava {
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}

I've just updated the project's "Android Plugin for Gradle" and "Gradle Wrapper" versions to the latest releases (2.2.3 and 3.2.1 respectively) and I'm now seeing warnings in the Java module as follows:

Access to 'sourceCompatibility' exceeds its access rights
Access to 'targetCompatibility' exceeds its access rights

If I move the sourceCompatibility and targetCompatibility declarations to the root-level of the module's build.gradle file as follows...

apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7

... then I get warning messages telling me that the assignments are not used.

What is the correct manner of specifying the source and target compatibility level of a Java module in the latest Gradle release?

like image 765
Adil Hussain Avatar asked Dec 07 '16 12:12

Adil Hussain


2 Answers

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'

like image 161
Jeroen Wijdemans Avatar answered Oct 10 '22 03:10

Jeroen Wijdemans


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.

like image 22
Stanislav Avatar answered Oct 10 '22 04:10

Stanislav