How to add -Xlint:unchecked to my Android Gradle based project?
Solution 1
This is what worked for me: (in your project's build.gradle)
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
Solution 2
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"
}
Solution 3
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"
}
}
Solution 4
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"
}
}
}
Solution 5
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.
rfgamaral
Updated on March 27, 2021Comments
-
rfgamaral about 3 years
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?
-
shakalaca over 10 yearsIs everything ok with only one parameter such as options.compilerArgs << "-Xlint:deprecation" ??
-
rfgamaral over 10 yearsYes, 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. -
Dandre Allison over 10 years@RicardoAmaral maybe you should just answer it yourself formally and refer this shakalaca's comment.
-
-
aleb almost 10 yearsHow would you do it for only the main module, excluding the dependencies?
-
TheChrisPratt almost 10 years@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. -
IgorGanapolsky over 8 yearsIs 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 over 8 yearsDoes this go in the top-level build.gradle, or the module?
-
Dr. Simon Harrer over 8 yearsIn the build.gradle file of the module.
-
not2qubit over 7 yearsCan you expand the context of that first snippet? Where in the
gradle.build
does it go? -
Maxim about 7 yearsI think it is better to add it to "build-extras.gradle" instead of "build.gradle".
-
Adil Hussain almost 7 yearsThis solution works for modules that are applying the
java
plugin in theirbuild.gradle
file but not for modules that are applying thecom.android.application
orcom.android.library
plugin. -
koppor almost 7 years@downvoters: Please comment why you downvote and please explain the solution you found.
-
JHS about 6 yearsThanks, 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.
-
Robin Davies almost 6 years@koppor: see previous comment. Doesn't work for android developers. Which is a problem for a thread entitled "... Android gradle based project".
-
Ayyappa about 4 yearsfor those who want to add parameters tag you need to add options.compilerArgs << "-parameters"
-
Sridhar Sarnobat over 3 yearsuggggh, why can't these be added to a single invocation at the command prompt? Gradle has been a step back from maven.
-
CoolMind over 3 yearsRun
./gradlew lint
instead of./gradlew lint -Xlint:Xlint:unchecked
or similar. -
Hakanai almost 3 yearsYou may also be able to write this as
tasks.withType<JavaCompile>
for 7 less characters.