148

I tried to add the following to the root build.gradle file:

subprojects {
    gradle.projectsEvaluated {
        tasks.withType(Compile) {
            options.compilerArgs << "-Xlint:unchecked -Xlint:deprecation"
        }
    }
}

But I'm getting this:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Libraries:ActionBarSherlock:compileRelease'.
> invalid flag: -Xlint:unchecked -Xlint:deprecation

What am I doing wrong?

rfgamaral
  • 16,546
  • 57
  • 163
  • 275
  • Is everything ok with only one parameter such as options.compilerArgs << "-Xlint:deprecation" ?? – shakalaca Sep 09 '13 at 15:24
  • 1
    Yes, it works. I've changed to `"-Xlint:unchecked" << "-Xlint:deprecation"` and it worked for both :) If you want to create an answer with this, I'll gladly mark it as accepted. – rfgamaral Sep 09 '13 at 16:21
  • @RicardoAmaral maybe you should just answer it yourself formally and refer this shakalaca's comment. – Dandre Allison Nov 12 '13 at 03:22

7 Answers7

252

This is what worked for me: (in your project's build.gradle)

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}
Dror
  • 5,107
  • 3
  • 27
  • 45
Felipe Lima
  • 10,530
  • 4
  • 41
  • 39
  • 1
    How would you do it for only the main module, excluding the dependencies? – aleb Jul 11 '14 at 09:17
  • 4
    @aleb, I assume you mean only apply the setting to the current build.gradle and not to all build.gradle's (i.e. allprojects). In that case, don't wrap it in an `allprojects` closure. – TheChrisPratt Jul 23 '14 at 06:17
  • Is this supposed to make a difference on Lint runs? For some reason, my Lint is displaying the exact same warning message regardless of `-Xlint:unchecked` setting. – IgorGanapolsky Jan 19 '16 at 15:19
  • I think it is better to add it to "build-extras.gradle" instead of "build.gradle". – Maxim Feb 11 '17 at 23:14
  • 3
    Thanks, this worked for me although I am not sure where this answer came from. It seems ridiculous to me that by default the linter will raise an error with no information about the problem until you add these options. – JHS Apr 08 '18 at 18:41
  • for those who want to add parameters tag you need to add options.compilerArgs << "-parameters" – Ayyappa Feb 04 '20 at 12:36
  • uggggh, why can't these be added to a single invocation at the command prompt? Gradle has been a step back from maven. – Sridhar Sarnobat Aug 24 '20 at 23:29
  • Run `./gradlew lint` instead of `./gradlew lint -Xlint:Xlint:unchecked` or similar. – CoolMind Jan 22 '21 at 16:40
62

Disclaimer: Even though this answer has more than 10 upvotes, it does not address the problem in the context of an Android project. However, Google finds this question in the context of non-Android projects. Thus, I keep this answer for those folks.

According to JavaCompile, the following seems to be solution:

compileJava {
    options.encoding = 'UTF-8'
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}

If you want it to have for the test cases, use compileTestJava

compileTestJava {
    options.encoding = 'UTF-8'
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
Synoli
  • 1,123
  • 1
  • 13
  • 17
koppor
  • 19,079
  • 15
  • 119
  • 161
12

For everyone using gradle.kts use the following to match up with the simple build.gradle file

build.gradle.kts

afterEvaluate {
        tasks.withType(JavaCompile::class) {
            options.compilerArgs.add("-Xlint:unchecked")
            options.compilerArgs.add("-Xlint:deprecation")
        }
    }

build.gradle

 gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
Dipali Shah
  • 3,742
  • 32
  • 47
8

Put this in your build.gradle file (root directory):

allprojects { // Projects
   gradle.projectsEvaluated {
      tasks.withType(JavaCompile) {
         options.encoding = 'UTF-8'
         options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
      }
   }
}
Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
3

I had a different compilation argument to set. The following works for me.

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-XDignore.symbol.file"
        options.bootClasspath = "$System.env.JAVA_HOME/jre/lib/rt.jar"
    }
}

You have to set the boot classpath for JDK 1.8 and above for things like Unsafe and sun.swing.* classes. Fix the source code especially for the latter, because Jigsaw Java 9, the up and coming modularity implementation for the JRE, will finally make these methods inaccessible(!). Consider yourself warned.

peter_pilgrim
  • 1,160
  • 11
  • 18
3

For deprecation, you can now use this in gradle kotlin script, which is better than modifying compilerArgs because it's type-safe:

tasks.withType<JavaCompile> {
    options.isDeprecation = true
}

In higher version of Gradle I'd recommend using the new API:

tasks.withType<JavaCompile>().configureEach {
    options.isDeprecation = true
}
ice1000
  • 6,406
  • 4
  • 39
  • 85
0

I'm not sure the problem was about using the Gradle subprojects configuration parameter, but the syntax you used:

options.compilerArgs << "-Xlint:unchecked -Xlint:deprecation"

This worked for me:

subprojects {
  gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
      options.compilerArgs += [
        '-Xlint:unchecked', // Shows information about unchecked or unsafe operations.
        '-Xlint:deprecation', // Shows information about deprecated members.
      ]
    }
  }
}

or

subprojects {
  gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
      options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    }
  }
}

If you only want to add one option (you would normally add more), inside the task JavaCompile you just need to add:

options.compilerArgs << "-Xlint:unchecked"

You can find more information about Lint here and here.

cesards
  • 15,882
  • 11
  • 70
  • 65