简体   繁体   English

如何抑制特定的 Kotlinc/Javac 编译器警告?

[英]How to suppress specific Kotlinc/Javac compiler warnings?

How to suppress deprecations in for KotlinCompile in Gradle similar to JavaCompile ?如何在类似于JavaCompile Gradle抑制KotlinCompile deprecations

JavaCompile(works): JavaCompile(有效):

tasks.withType(JavaCompile) {
    configure(options) {
        compilerArgs << '-Xlint:-deprecation'
    }
}

KotlinCompile(does not work): KotlinCompile(不起作用):

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    kotlinOptions {
        freeCompilerArgs = ["-Xjavac-arguments=-Xlint:-deprecation"]
    }
}

References:参考:

Similar questions:类似问题:

Currently Kotlin does not support Surpressing compiler warnings.目前 Kotlin 不支持 Surpressing compiler warnings。 They do have some categories to surpress.他们确实有一些类别需要压制。 But different then that the only way none is:但与此不同的是,唯一的方法是:

@Suppress("DEPRECATION")

Sometimes Surpress will not work by just inputing the annotation which should work on runtime.有时,仅通过输入应该在运行时工作的注释,Surpress 将无法工作。 You might need to add something like the following您可能需要添加如下内容

val foo = error.asDynamic().response
if (foo is AxiosResponse<String>) {
    @Suppress("UNCHECKED_CAST")
    val response = foo as AxiosResponse<String>
}

Which is not your case.这不是你的情况。 But apparently other people have had the similar issue.但显然其他人也有类似的问题。 I suggest taking a brief look at reddit.我建议简要看一下reddit。 Which also not the case above but can lead you.这也不是上面的情况,但可以引导你。

https://www.reddit.com/r/Kotlin/comments/bsgk5w/what_do_i_have_to_do_to_suppress_my_unchecked/ https://www.reddit.com/r/Kotlin/comments/bsgk5w/what_do_i_have_to_do_to_suppress_my_unchecked/

It looks like there is a suppressWarnings option on the kotlinOptions property defined on the KotlinCompile task. 看起来在KotlinCompile任务上定义的kotlinOptions属性上有一个suppressWarnings选项。 Setting that to true may do what you want. 将其设置为true可以做你想要的。

This is how you would do that: 这是你怎么做的:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    kotlinOptions {
        suppressWarnings = true
    }
}

If your goal is to suppress deprecations, so you can make the build fail on each other Kotlin warnings, then this is my experience.如果您的目标是抑制弃用,因此您可以使构建在彼此的 Kotlin 警告上失败,那么这就是我的经验。

There is a way to configure the Kotlin compiler to make it fail on each warning.有一种方法可以配置 Kotlin 编译器,使其在每次警告时都失败。 Just add the following configuration to your build.gradle file:只需将以下配置添加到您的build.gradle文件中:

// For Android Project
android {
    kotlinOptions {
        allWarningsAsErrors = true
    }
}
// For non Android Project
compileKotlin {
    kotlinOptions {
        allWarningsAsErrors = true
    }
}

But after enabling the allWarningsAsErrors flag, the Kotlin compiler is going to fail after each deprecated code usage.但是在启用allWarningsAsErrors标志后,Kotlin 编译器将在每次弃用代码使用后失败。 Unfortunately, Kotlin doesn't support ignoring those is deprecated warnings or avoid failing on them.不幸的是,Kotlin 不支持忽略那些已弃用的警告或避免失败。

The following Gradle script offers a solution to this issue.以下 Gradle 脚本为此问题提供了解决方案。

import org.gradle.api.Project
import org.gradle.api.internal.GradleInternal
import org.gradle.configurationcache.extensions.serviceOf
import org.gradle.internal.logging.events.operations.LogEventBuildOperationProgressDetails
import org.gradle.internal.operations.BuildOperationDescriptor
import org.gradle.internal.operations.BuildOperationListener
import org.gradle.internal.operations.BuildOperationListenerManager
import org.gradle.internal.operations.OperationFinishEvent
import org.gradle.internal.operations.OperationIdentifier
import org.gradle.internal.operations.OperationProgressEvent
import org.gradle.internal.operations.OperationStartEvent

val kotlinWarnings = mutableListOf<String>()
val buildOperationListener = object : BuildOperationListener {
    override fun started(buildOperation: BuildOperationDescriptor, startEvent: OperationStartEvent) {
    }

    override fun progress(operationIdentifier: OperationIdentifier, progressEvent: OperationProgressEvent) {
        val log = progressEvent.details
        if (log is LogEventBuildOperationProgressDetails) {
            if (log.message.contains("w:") && !log.message.contains("is deprecated.") && !kotlinWarnings.contains(log.message)) {
                kotlinWarnings.add(log.message)
            }
        }
    }

    override fun finished(buildOperation: BuildOperationDescriptor, finishEvent: OperationFinishEvent) {
    }
}
val gradleInternal = project.gradle as GradleInternal
val buildOperationListenerManager = gradleInternal.serviceOf() as BuildOperationListenerManager?
buildOperationListenerManager?.addListener(buildOperationListener)
project.gradle.buildFinished {
    buildOperationListenerManager?.removeListener(buildOperationListener)
    if (kotlinWarnings.isNotEmpty()) {
        kotlinWarnings.forEach { project.logger.warn(it) }
        throw RuntimeException("Kotlin warning found")
    }
}

Basically, a BuildOperationListener is added to the Gradle build, so you can listen for each console log.基本上,一个BuildOperationListener被添加到 Gradle 构建中,因此您可以监听每个控制台日志。 Each log is inspected searching for Kotlin warnings, but ignoring the deprecated usage warnings.检查每个日志以搜索 Kotlin 警告,但忽略已弃用的使用警告。 Finally, an exception is thrown if there are Kotlin warnings on the project最后,如果项目有 Kotlin 警告会抛出异常


The following article gives more info about this topic: Fail your build on Kotlin warnings以下文章提供了有关此主题的更多信息: 在 Kotlin 上构建失败警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM