Recompile with -Xlint in Android Studio

·1 min read·paskov

Avoiding deprecated APIs is a good habit. Plus, clean build output is always nice :)

When you build an app, the Java compiler may report that some input files use unchecked or unsafe operations, or use or override a deprecated API:

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

As the messages suggest, we can recompile with -Xlint:unchecked and -Xlint:deprecation to get more details about these warnings.

Open the app module’s build.gradle file:

App module's build.gradle file

App module’s build.gradle file

Add an allprojects {} block if one isn’t already present. Inside it, configure Gradle to pass the following compiler arguments to each JavaCompile task:

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

Now, each time we build the app, we’ll see details about unchecked or unsafe operations and deprecated APIs used in our code.

Tip: If you need to keep using a deprecated API, you can suppress its deprecation warnings by adding the following annotation to the method that uses it:

@SuppressWarnings("deprecation")

Comments 3

  1. AkamashiDesu

    Thank you

  2. Renan
    tasks.withType(JavaCompile) {
      options.compilerArgs << "-Xlint:unchecked" << "- } = Error
     
    
    tasks.withType(JavaCompile) {
      options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"}
     = OK
    
    Android Gradle Plugin = 3.5.0
    Gradle Version = 5.4.1
  3. Jim Nickerson

    Thank you.