How to add -Xlint:unchecked to my Android Gradle based project?
Asked Answered
H

7

149

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?

Hylophagous answered 8/9, 2013 at 22:41 Comment(3)
Is everything ok with only one parameter such as options.compilerArgs << "-Xlint:deprecation" ??Temperate
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.Hylophagous
@RicardoAmaral maybe you should just answer it yourself formally and refer this shakalaca's comment.Doersten
S
254

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

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}
Shirleneshirley answered 4/3, 2014 at 21:49 Comment(8)
How would you do it for only the main module, excluding the dependencies?Rhodarhodamine
@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.Hokanson
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.Traci
I think it is better to add it to "build-extras.gradle" instead of "build.gradle".Bayern
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.Francenefrances
for those who want to add parameters tag you need to add options.compilerArgs << "-parameters"Cutcheon
uggggh, why can't these be added to a single invocation at the command prompt? Gradle has been a step back from maven.Comprehend
Run ./gradlew lint instead of ./gradlew lint -Xlint:Xlint:unchecked or similar.Lyrate
S
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"
}
Sprawl answered 24/12, 2015 at 11:42 Comment(6)
Does this go in the top-level build.gradle, or the module?Traci
In the build.gradle file of the module.Servomechanism
Can you expand the context of that first snippet? Where in the gradle.build does it go?Gehenna
This solution works for modules that are applying the java plugin in their build.gradle file but not for modules that are applying the com.android.application or com.android.library plugin.Substantialize
@downvoters: Please comment why you downvote and please explain the solution you found.Sprawl
@koppor: see previous comment. Doesn't work for android developers. Which is a problem for a thread entitled "... Android gradle based project".Richel
M
13

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"
        }
    }
Millman answered 31/12, 2019 at 9:54 Comment(1)
You may also be able to write this as tasks.withType<JavaCompile> for 7 less characters.Nuthatch
T
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"
      }
   }
}
Trivet answered 15/1, 2019 at 10:4 Comment(0)
M
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.

Misgiving answered 7/1, 2015 at 10:24 Comment(0)
P
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
}
Pomelo answered 24/9, 2020 at 18:53 Comment(0)
G
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.

Grobe answered 19/9, 2018 at 0:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.