如何输出Kotlin代码的弃用警告? [英] How to output deprecation warnings for Kotlin code?

查看:603
本文介绍了如何输出Kotlin代码的弃用警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java / Kotlin Android项目的 app / build.gradle 文件中使用以下配置代码段:

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"
    }
}

它生成详细的输出编译项目时 .java 文件中的Lint警告。

我想对 .kt 文件实现相同的效果。我发现Kotlin具有编译器选项

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 :此版本的编译器不支持标志:-Xlint:unchecked

w:此版本的编译器不支持标志:-Xlint:deprecation

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

如何为Kotlin代码输出弃用警告?

How can I output deprecation warnings for Kotlin code?

推荐答案

java编译器和kotlin编译器有完全不同的选择。 -Xlint 选项对于kotlinc不存在。您可以运行 kotlinc -X 显示所有 -X 选项。

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.

-Xjavac-arguments 参数允许您通过kotlinc传递javac参数。例如:

The -Xjavac-arguments argument allows you to pass javac arguments through kotlinc. For example:

kotlinc -Xjavac-arguments='-Xlint:unchecked -Xlint:deprecation' ...

在gradle文件中,您可以构建一个包含一个参数的数组:

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'"
            ]
        }
    }
}

其他语法也可能有效。

此外:默认警告是否不包括这些警告?您可以通过添加以下代码段进行检查,以确保您不抑制警告:

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

这篇关于如何输出Kotlin代码的弃用警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆