23

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.

asked Dec 3, 2014 at 14:44
1

3 Answers 3

31

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"
 }
}
answered Feb 2, 2015 at 2:25
Sign up to request clarification or add additional context in comments.

Comments

6

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.

answered Dec 3, 2014 at 14:44

6 Comments

In which build.gradle file and in which section of that file?
@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 of preBuild { is 0)
that 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
Well, 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.
Yes, 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").
|
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'
 }
 }
}
user905686
4,6748 gold badges41 silver badges61 bronze badges
answered Jan 15, 2019 at 21:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.