What is -Xlint:deprecation and how to use it

How am I going to use this? I tried searching this but cannot find one.
-Xlint:deprecation

When doing a gradlew lint this is what it shown to me

Task :app:compileDebugJavaWithJavac
Note: [1] Wrote GeneratedAppGlideModule with:
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

This is Android which also use Android Gradle Plugin so i assumed this is under Plugin Portal topic.

This doesn’t actually have anything to do with the Android plugin or Android itself. This is just an option for the Java compiler to provide more details about which classes/methods you’re using that are marked as @Deprecated in code that is not also marked as @Deprecated. If you’ve marked something @Deprecated in your code that you plan to remove usage of soon, this would be expected. If it’s in a library, you would generally want to try to reduce your dependencies on the deprecated library code as that will usually prevent you from upgrading to the next major version.

If you’re interested in these details, you can set a flag on all the JavaCompile tasks:

tasks.withType(JavaCompile) {
    options.deprecation = true
}

or if you rather explicitly set the compiler argument, you can do so as well:

tasks.withType(JavaCompile) {
    options.compilerArgs += ['-Xlint:deprecation']
}

Otherwise, you can just ignore the warning until you’re ready to do something about it.

1 Like

I never seen this anywhere and it is very helpful, thanks a lot.
How about if written in Kotlin is it still JavaCompile?

This can be separated by comma right?

tasks.withType(JavaCompile) {
    options.compilerArgs += ['-Xlint:deprecation']
}

Generally the second example with compilerArgs is what’s shown in the docs. The text normally just says to reference JavaCompile, which includes CompileOptions, for the comprehensive list.

If the project code is Kotlin, you won’t be using JavaCompile. The Kotlin plugin is provided by JetBrains and you’d have to reference their plugin docs rather than the Gradle docs to determine what options are available on a specific version. As of now, you’d need to use the compiler arguments directly rather than a property like you can for Java.

Yes, you can add multiple compilerArgs in the same line, i.e.:
options.compilerArgs += ['-Xlint:deprecation', '-Xlint:unchecked']

1 Like

The kts (Kotlin) version of this is…

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