Jeffrey Charles
Jeffrey Charles

Reputation: 793

How do I make the Kotlin compiler treat warnings as errors?

I have a Kotlin project where I'd like to have Kotlin warnings treated as errors. How can I do that?

Upvotes: 43

Views: 11905

Answers (6)

user823738
user823738

Reputation: 17531

Extending LiorH's answer, here's kotlin DSL if you want to enable the warnings as errors only for release builds:

tasks.withType<KotlinCompile>().configureEach {
    kotlinOptions.allWarningsAsErrors = name.contains("Release")
}

Upvotes: 1

Jayson Minard
Jayson Minard

Reputation: 86016

This does not appear to be currently available in the Kotlin command-line help, or arguments available to the Kotlin compiler:

K2JVMCompilerArguments.java

and

CommonCompilerArguments.java

But some people in Gradle do things like this to scan logging of the compiler to know when a warning was generated. How to fail gradle build on Javadoc warnings

Within the IDE Plugins available for Kotlin (Intellij IDEA and Eclipse) there is no such option.

You should file a feature request (or check if one already exists) in YouTrack which has all the issue tracking for the Kotlin project. And if you do so, please post the issue here so it can be tracked.

Upvotes: 9

Enselic
Enselic

Reputation: 4912

With recent versions of the Android Gradle plug-in, one can simply do this:

android {
    kotlinOptions {
        kotlinOptions.allWarningsAsErrors = true
    }

    ...
}

Upvotes: 10

LiorH
LiorH

Reputation: 18834

With the Kotlin DSL:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        kotlinOptions.allWarningsAsErrors = true
    }
}

Upvotes: 1

Alireza Ahmadi
Alireza Ahmadi

Reputation: 5348

If you are using android use this instead. In Android we don't have compileKotlin but rather compileDebugKotlin, compileReleaseKotlin, etc. So we have to loop through them and add this configuration to all of them.

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

Upvotes: 2

Alexander Udalov
Alexander Udalov

Reputation: 32826

Since Kotlin 1.2, the command line argument -Werror is supported. In Gradle, it's named allWarningsAsErrors:

compileKotlin {
    kotlinOptions.allWarningsAsErrors = true
}

Upvotes: 51

Related Questions