I have a build.gradle file with dependencies { classpath 'com.android.tools.build:gradle:0.13.3'} and apply 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.
-
Does this answer your question? Recompile with -Xlint in Android studiolcnicolau– lcnicolau2020年09月10日 02:33:24 +00:00Commented Sep 10, 2020 at 2:33
3 Answers 3
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"
}
}
Comments
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.
6 Comments
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 of preBuild { is 0)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'
}
}
}
Comments
Explore related questions
See similar questions with these tags.