Konrad Jamrozik Konrad Jamrozik - 4 months ago 25x
Java Question

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

I have a

file with
dependencies { classpath 'com.android.tools.build:gradle:0.13.3'}
apply plugin: 'com.android.application'

When I do a debug build I get:

gradle clean assembleDebug
Note: C:\path\to\MyClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.



How can I add the
to the underlying task? Gradle Plugin User Guide on Java compilation options is unhelpful.


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.