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

AndroidGradleJavacLint

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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 - Android

> 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 - Android

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 - Android

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 - Android

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.

Solution 6 - Android

For deprecation, you can now use this in gradle kotlin script, which is better than modifying compilerArgs because it's type-safe:

tasks.withType<JavaCompile> {
    options.isDeprecation = true
}

In higher version of Gradle I'd recommend using the new API:

tasks.withType<JavaCompile>().configureEach {
    options.isDeprecation = true
}

Solution 7 - Android

I'm not sure the problem was about using the Gradle subprojects configuration parameter, but the syntax you used:

options.compilerArgs << "-Xlint:unchecked -Xlint:deprecation"

This worked for me:

subprojects {
  gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
      options.compilerArgs += [
        '-Xlint:unchecked', // Shows information about unchecked or unsafe operations.
        '-Xlint:deprecation', // Shows information about deprecated members.
      ]
    }
  }
}

or

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

If you only want to add one option (you would normally add more), inside the task JavaCompile you just need to add:

options.compilerArgs << "-Xlint:unchecked"

You can find more information about Lint here and here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrfgamaralView Question on Stackoverflow
Solution 1 - AndroidFelipe LimaView Answer on Stackoverflow
Solution 2 - AndroidkopporView Answer on Stackoverflow
Solution 3 - AndroidDipali ShahView Answer on Stackoverflow
Solution 4 - AndroidYousha AleayoubView Answer on Stackoverflow
Solution 5 - Androidpeter_pilgrimView Answer on Stackoverflow
Solution 6 - Androidice1000View Answer on Stackoverflow
Solution 7 - AndroidcesardsView Answer on Stackoverflow