Jared Burrows
Jared Burrows

Reputation: 55517

How to suppress specific Kotlinc/Javac compiler warnings?

How to suppress deprecations in for KotlinCompile in Gradle similar to JavaCompile?

JavaCompile(works):

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

KotlinCompile(does not work):

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

References:

Similar questions:

Upvotes: 45

Views: 5613

Answers (2)

Maxi Rosson
Maxi Rosson

Reputation: 109

If your goal is to suppress deprecations, so you can make the build fail on each other Kotlin warnings, then this is my experience.

There is a way to configure the Kotlin compiler to make it fail on each warning. Just add the following configuration to your build.gradle file:

// 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. Unfortunately, Kotlin doesn’t support ignoring those is deprecated warnings or avoid failing on them.

The following Gradle script offers a solution to this issue.

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. Each log is inspected searching for Kotlin warnings, but ignoring the deprecated usage warnings. Finally, an exception is thrown if there are Kotlin warnings on the project


The following article gives more info about this topic: Fail your build on Kotlin warnings

Upvotes: 2

Sherlocker
Sherlocker

Reputation: 355

Currently Kotlin does not support 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. 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. 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/

Upvotes: 3

Related Questions