How to suppress specific Kotlinc/Javac compiler warnings?

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

Leave a Comment