41

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

Jeffrey Charles
  • 773
  • 5
  • 10

6 Answers6

47

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

compileKotlin {
    kotlinOptions.allWarningsAsErrors = true
}
Alexander Udalov
  • 31,429
  • 6
  • 80
  • 66
  • 4
    Which block of `build.gradle` does this need to be added to? – karl Dec 13 '17 at 23:22
  • 1
    @karl I think this should be on the top level. Please refer to the official documentation: https://kotlinlang.org/docs/reference/using-gradle.html#compiler-options – Alexander Udalov Dec 20 '17 at 12:37
  • 4
    Thanks. From the docs, for future reference: for an Android project, the task name will include the build variant (i.e. `compileDebugKotlin`), but you can add it to all tasks by using `tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions { ... } }` – karl Dec 20 '17 at 20:20
  • 5
    For Android: inside `allprojects` block of project level `build.gradle` file, add: `tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions.allWarningsAsErrors = true }` – Piotr Aleksander Chmielowski Sep 02 '19 at 19:54
11

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

android {
    kotlinOptions {
        kotlinOptions.allWarningsAsErrors = true
    }

    ...
}
Enselic
  • 4,434
  • 2
  • 30
  • 42
9

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.

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
2

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
    }
}
Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
  • 1
    For AndroidStudio 3.5, I put the above block within `allprojects` block at top level of ./app/build.gradle file, and that worked for me. – Daniel Mar 13 '20 at 18:38
2

With the Kotlin DSL:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        kotlinOptions.allWarningsAsErrors = true
    }
}
LiorH
  • 18,524
  • 17
  • 70
  • 98
0

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")
}
Zaffy
  • 16,801
  • 8
  • 50
  • 77