How to output deprecation warnings for Kotlin code?

与世无争的帅哥 提交于 2020-05-31 07:34:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!