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.

Konrad Jamrozik
  • 3,254
  • 5
  • 29
  • 59
  • Does this answer your question? [Recompile with -Xlint in Android studio](https://stackoverflow.com/questions/47740812/recompile-with-xlint-in-android-studio) – lcnicolau Sep 10 '20 at 02:33

3 Answers3

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"
    }
}
Community
  • 1
  • 1
Andrew
  • 1,057
  • 11
  • 19
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.

Konrad Jamrozik
  • 3,254
  • 5
  • 29
  • 59
  • In which build.gradle file and in which section of that file? – Andrew Feb 01 '15 at 10:25
  • @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) – Konrad Jamrozik Feb 01 '15 at 13:51
  • 1
    that doesn't seem to work. the top of my modules build.gradle file looks like: apply plugin: 'com.android.application'
    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 Feb 01 '15 at 20:43
  • Well, the
    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.
    – Andrew Feb 01 '15 at 20:50
  • Hmm, I cannot replicate the failre. Does the project build with `assembleDebug` without the `preBuild` task? Have you tried printing out the names of existing `android.ApplicationVariants` ? Does it have one named `debug`? I have two: `debug` and `release`. – Konrad Jamrozik Feb 01 '15 at 21:41
  • 1
    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"). – Andrew Feb 02 '15 at 02:16
2

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,491
  • 8
  • 39
  • 60
Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64