How to add java compiler options when compiling with Android Gradle Plugin?
Solution 1
I found the following solution based on Gradle Plugin User Guide on Manipulating Tasks and Gradle DSL doc about JavaCompile:
Add to build.gradle
:
preBuild {
doFirst {
JavaCompile jc = android.applicationVariants.find { it.name == 'debug' }.javaCompile
jc.options.compilerArgs = ["-Xlint:unchecked"]
}
}
The application variants are null
during Gradle's configuration phase and the required JavaCompile
task also doesn't exist, thus I do the modification in the execution phase instead.
Solution 2
I tried the solution posed by @Konrad Jamrozik but it didn't work with my project due to flavours in my project.
Given that we're just turning on additional warnings, not something that's significantly changing how the compiler operates, I don't see it being an issue that it will be added to both release and debug builds. As such, this answer has a cleaner method that works with flavours: How to add -Xlint:unchecked to my Android Gradle based project?
In my case, adding this to the build.gradle file of the affected module:
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
Solution 3
Add this to build.gradle file:
android { // <---
tasks.withType(JavaCompile) {
configure(options) {
options.encoding = 'UTF-8'
options.debug = true
options.failOnError = true
options.warnings = true
options.compilerArgs << '-Xlint:deprecation' << '-Xlint:unchecked'
}
}
Related videos on Youtube

Konrad Jamrozik
Engineer @ Microsoft, working at the Security and Compliance Engineering Team of One Engineering System.
Updated on June 24, 2022Comments
-
Konrad Jamrozik 6 months
I have a
build.gradle
file withdependencies { classpath 'com.android.tools.build:gradle:0.13.3'}
andapply plugin: 'com.android.application'
.When I do a debug build I get:
gradle clean assembleDebug :myapp:preBuild (...) :myapp:compileDebugJava Note: C:\path\to\MyClass.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. :myapp:preDexDebug (...) :myapp:assembleDebug BUILD SUCCESSFUL
How can I add the
-Xlint:unchecked
to the underlying task? Gradle Plugin User Guide on Java compilation options is unhelpful.-
lcnicolau over 2 yearsDoes this answer your question? Recompile with -Xlint in Android studio
-
-
Andrew almost 8 yearsIn which build.gradle file and in which section of that file?
-
Konrad Jamrozik almost 8 years@Andrew threre are two gradle files: "project-level" and "app-level". Only the "app-level" should apply
com.android.application
plugin, and that's the file I mean. You have to add it directly to the file, not into any section (the indentation depth ofpreBuild {
is 0) -
Andrew almost 8 yearsthat doesn't seem to work. the top of my modules build.gradle file looks like: apply plugin: 'com.android.application'<br/> preBuild { doFirst { JavaCompile jc = android.applicationVariants.find { it.name == 'debug' }.javaCompile jc.options.compilerArgs = ["-Xlint:deprecated"] } } android { And it dies with: Execution failed for task ':climbingguide:preBuild'. > Cannot get property 'javaCompile' on null object
-
Andrew almost 8 yearsWell, the <br/> isn't in the file, but StackExchange won't let me insert line breaks and now won't let me remove my attempt to put them in.
-
Konrad Jamrozik almost 8 yearsHmm, I cannot replicate the failre. Does the project build with
assembleDebug
without thepreBuild
task? Have you tried printing out the names of existingandroid.ApplicationVariants
? Does it have one nameddebug
? I have two:debug
andrelease
. -
Andrew almost 8 yearsYes, I can assemble stuff fine but I have a few flavours so I'm building assemble[Flavour]Debug rather than assembleDebug. Will a little fiddling I've come up with something that's working for me. Rather than matching a specific Flavour, I'm enabling the flag on all variants. See my answer. Now just to figure out how to get it to ignore the deprecation where I've @SuppressWarnings("deprecation").