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

15,968

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'
   }
}
Share:
15,968

Related videos on Youtube

Konrad Jamrozik
Author by

Konrad Jamrozik

Engineer @ Microsoft, working at the Security and Compliance Engineering Team of One Engineering System.

Updated on June 24, 2022

Comments

  • Konrad Jamrozik
    Konrad Jamrozik 6 months

    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.

  • Andrew almost 8 years
    In which build.gradle file and in which section of that file?
  • Konrad Jamrozik
    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 of preBuild { is 0)
  • Andrew almost 8 years
    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
  • Andrew almost 8 years
    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.
  • Konrad Jamrozik
    Konrad Jamrozik almost 8 years
    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.
  • Andrew almost 8 years
    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").