How to add -Xlint:unchecked to my Android Gradle based project?

64,515

Solution 1

This is what worked for me: (in your project's build.gradle)

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}

Solution 2

Disclaimer: Even though this answer has more than 10 upvotes, it does not address the problem in the context of an Android project. However, Google finds this question in the context of non-Android projects. Thus, I keep this answer for those folks.

According to JavaCompile, the following seems to be solution:

compileJava {
    options.encoding = 'UTF-8'
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}

If you want it to have for the test cases, use compileTestJava

compileTestJava {
    options.encoding = 'UTF-8'
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}

Solution 3

For everyone using gradle.kts use the following to match up with the simple build.gradle file

build.gradle.kts

afterEvaluate {
        tasks.withType(JavaCompile::class) {
            options.compilerArgs.add("-Xlint:unchecked")
            options.compilerArgs.add("-Xlint:deprecation")
        }
    }

build.gradle

 gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }

Solution 4

Put this in your build.gradle file (root directory):

allprojects { // Projects
   gradle.projectsEvaluated {
      tasks.withType(JavaCompile) {
         options.encoding = 'UTF-8'
         options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
      }
   }
}

Solution 5

I had a different compilation argument to set. The following works for me.

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-XDignore.symbol.file"
        options.bootClasspath = "$System.env.JAVA_HOME/jre/lib/rt.jar"
    }
}

You have to set the boot classpath for JDK 1.8 and above for things like Unsafe and sun.swing.* classes. Fix the source code especially for the latter, because Jigsaw Java 9, the up and coming modularity implementation for the JRE, will finally make these methods inaccessible(!). Consider yourself warned.

Share:
64,515
rfgamaral
Author by

rfgamaral

Updated on March 27, 2021

Comments

  • rfgamaral
    rfgamaral about 3 years

    I tried to add the following to the root build.gradle file:

    subprojects {
        gradle.projectsEvaluated {
            tasks.withType(Compile) {
                options.compilerArgs << "-Xlint:unchecked -Xlint:deprecation"
            }
        }
    }
    

    But I'm getting this:

    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':Libraries:ActionBarSherlock:compileRelease'.
    > invalid flag: -Xlint:unchecked -Xlint:deprecation
    

    What am I doing wrong?

    • shakalaca
      shakalaca over 10 years
      Is everything ok with only one parameter such as options.compilerArgs << "-Xlint:deprecation" ??
    • rfgamaral
      rfgamaral over 10 years
      Yes, it works. I've changed to "-Xlint:unchecked" << "-Xlint:deprecation" and it worked for both :) If you want to create an answer with this, I'll gladly mark it as accepted.
    • Dandre Allison
      Dandre Allison over 10 years
      @RicardoAmaral maybe you should just answer it yourself formally and refer this shakalaca's comment.
  • aleb
    aleb almost 10 years
    How would you do it for only the main module, excluding the dependencies?
  • TheChrisPratt
    TheChrisPratt almost 10 years
    @aleb, I assume you mean only apply the setting to the current build.gradle and not to all build.gradle's (i.e. allprojects). In that case, don't wrap it in an allprojects closure.
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    Is this supposed to make a difference on Lint runs? For some reason, my Lint is displaying the exact same warning message regardless of -Xlint:unchecked setting.
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    Does this go in the top-level build.gradle, or the module?
  • Dr. Simon Harrer
    Dr. Simon Harrer over 8 years
    In the build.gradle file of the module.
  • not2qubit
    not2qubit over 7 years
    Can you expand the context of that first snippet? Where in the gradle.build does it go?
  • Maxim
    Maxim about 7 years
    I think it is better to add it to "build-extras.gradle" instead of "build.gradle".
  • Adil Hussain
    Adil Hussain almost 7 years
    This solution works for modules that are applying the java plugin in their build.gradle file but not for modules that are applying the com.android.application or com.android.library plugin.
  • koppor
    koppor almost 7 years
    @downvoters: Please comment why you downvote and please explain the solution you found.
  • JHS
    JHS about 6 years
    Thanks, this worked for me although I am not sure where this answer came from. It seems ridiculous to me that by default the linter will raise an error with no information about the problem until you add these options.
  • Robin Davies
    Robin Davies almost 6 years
    @koppor: see previous comment. Doesn't work for android developers. Which is a problem for a thread entitled "... Android gradle based project".
  • Ayyappa
    Ayyappa about 4 years
    for those who want to add parameters tag you need to add options.compilerArgs << "-parameters"
  • Sridhar Sarnobat
    Sridhar Sarnobat over 3 years
    uggggh, why can't these be added to a single invocation at the command prompt? Gradle has been a step back from maven.
  • CoolMind
    CoolMind over 3 years
    Run ./gradlew lint instead of ./gradlew lint -Xlint:Xlint:unchecked or similar.
  • Hakanai
    Hakanai almost 3 years
    You may also be able to write this as tasks.withType<JavaCompile> for 7 less characters.