How to add java compiler options when compiling with Android Gradle Plugin?
Asked Answered
H

3

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.

Highbrow answered 3/12, 2014 at 14:44 Comment(1)
Does this answer your question? Recompile with -Xlint in Android studioMonoclinous
H
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.

Highbrow answered 3/12, 2014 at 14:44 Comment(6)
In which build.gradle file and in which section of that file?Aeschines
@Aeschines 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)Highbrow
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 objectAeschines
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.Aeschines
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.Highbrow
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").Aeschines
A
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"
    }
}
Aeschines answered 2/2, 2015 at 2:25 Comment(0)
H
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.

Highbrow answered 3/12, 2014 at 14:44 Comment(6)
In which build.gradle file and in which section of that file?Aeschines
@Aeschines 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)Highbrow
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 objectAeschines
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.Aeschines
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.Highbrow
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").Aeschines
D
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'
      }
   }
}
Detach answered 15/1, 2019 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.