问题
I am using the following configuration snippet in my Java/Kotlin Android project in the app/build.gradle file:
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
It generates a verbose output of Lint warnings in .java files when the project is compiled.
I would like to achieve the same for .kt files. I found out that Kotlin has compiler options:
gradle.projectsEvaluated {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
freeCompilerArgs = ["-Xlint:unchecked", "-Xlint:deprecation"]
}
}
}
However the compiler flags are not supported:
w: Flag is not supported by this version of the compiler: -Xlint:unchecked
w: Flag is not supported by this version of the compiler: -Xlint:deprecation
How can I output deprecation warnings for Kotlin code?
回答1:
The java compiler and kotlin compiler have completely different options. The -Xlint
option does not exist for kotlinc. You can run kotlinc -X
to show all the -X
options.
The -Xjavac-arguments
argument allows you to pass javac arguments through kotlinc. For example:
kotlinc -Xjavac-arguments='-Xlint:unchecked -Xlint:deprecation' ...
In your gradle file, you can build an array of one argument:
gradle.projectsEvaluated {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
freeCompilerArgs = [
"-Xjavac-arguments='-Xlint:unchecked -Xlint:deprecation'"
]
}
}
}
Other syntaxes may also work.
Aside: do the default warnings not include these? You could check by adding this snippet to ensure you're not suppressing warnings:
compileKotlin {
kotlinOptions {
suppressWarnings = false
}
}
来源:https://stackoverflow.com/questions/57209106/how-to-output-deprecation-warnings-for-kotlin-code