How to recompile with -Xlint:deprecation
AndroidGradleLintAndroid Problem Overview
I don't use Android Studio but I build everything from the command line using build.gradle
. I generate a Lint report like this:
./gradlew lint
This correctly generates a Lint report but it also says this:
Note: MyActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
This makes me wonder how I can do that? I've tried the following:
./gradlew lint -Xlint:deprecation
But it doesn't work. It says:
Problem configuring task :app:lint from command line.
Unknown command-line option '-X'.
So how can I pass -Xlint:deprecation
to Lint via gradle?
Android Solutions
Solution 1 - Android
To answer my own question, you need to add the following to your project-level build.gradle
file:
allprojects {
...
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:deprecation"
}
}
}
Solution 2 - Android
Just an update from me since I ran into this issue recently:
You can get the details for this deprecation issue by doing as @Andreas suggested in the accepted answer. If you're using Kotlin, the real solution is (in build.gradle):
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs.add("-Xlint:deprecation")
}
}
}
One other solution is to update your AndroidSDK. The issue is with an SDK method overriding a deprecated feature. In your build.gradle file you can change the compileSdkVersion:
android {
compileSdkVersion 29 //Change this to the latest release
...
...
defaultConfig {
...
targetSdkVersion 29 //Change this too
...
}
}
Using the latest SDK version will likely fix your issue. Android does a good job of fixing deprecation issue between releases. If it persists, you might just need to wait until the next SDK release. If the issue isn't in the AndroidSDK but instead in a package you've downloaded, you should see if the package needs upgrading or contact the manager of that package.
Solution 3 - Android
Kotlin (build.gradle.kts) version of the other answers:
allprojects {
gradle.projectsEvaluated {
tasks.withType<JavaCompile> {
options.compilerArgs.addAll(arrayOf("-parameters", "-Xlint:deprecation"))
}
}
}
Solution 4 - Android
I have added in-app level build.gradle and resolved issue
aaptOptions { cruncherEnabled = false }
Solution 5 - Android
This is an accepted answer but solved this with Gradle Java
plugin as below.
Add java plugin to build.gradle
file if not added already.
plugins {
// Apply the java plugin to add support for Java
id "java"
}
Then add task compileJava
and/or compileTestJava
based on your need to check deprecations for only java main source code or java test source code.
You can add various compile options as listed here
compileTestJava {
// options.incremental = true
// options.fork = true
// options.failOnError = false
options.compilerArgs.add("-Xlint:deprecation")
}
Also, we can specify for the main and test sourcecode project as
tasks.withType(JavaCompile) {
options.compilerArgs.add("-Xlint:deprecation")
}
Solution 6 - Android
Adding this code to build.gradle solved my problem
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:deprecation"
}
}