I have a Kotlin project where I'd like to have Kotlin warnings treated as errors. How can I do that?
-
In what build system, or IDE? the answer might be different. (I've never seen this option available, but to be sure be clear about in what build system this would be applicable) – Jayson Minard Jan 02 '16 at 03:34
-
Do you have the source of this project on Github? Are you using Gradle and Intellij? – Jared Burrows Jan 02 '16 at 05:42
-
@JaysonMinard it's using Gradle as the build system – Jeffrey Charles Jan 04 '16 at 01:53
-
@JaredBurrows https://github.com/jeffcharles/visitor-detector. A very early and still in development version. Yes and yes to Gradle and Intellij. – Jeffrey Charles Jan 04 '16 at 01:54
6 Answers
Since Kotlin 1.2, the command line argument -Werror
is supported. In Gradle, it's named allWarningsAsErrors
:
compileKotlin {
kotlinOptions.allWarningsAsErrors = true
}

- 31,429
- 6
- 80
- 66
-
4
-
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
-
4Thanks. 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
-
5For 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
With recent versions of the Android Gradle plug-in, one can simply do this:
android {
kotlinOptions {
kotlinOptions.allWarningsAsErrors = true
}
...
}

- 4,434
- 2
- 30
- 42
This does not appear to be currently available in the Kotlin command-line help, or arguments available to the Kotlin compiler:
and
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.

- 84,842
- 38
- 184
- 227
-
5Feature request filed at https://youtrack.jetbrains.com/issue/KT-10563 – Jeffrey Charles Jan 04 '16 at 02:04
-
Writing something in Gradle in the meantime is a reasonable idea. It's not high enough priority for me to justify the effort for that right now but I'll consider it in the future. – Jeffrey Charles Jan 04 '16 at 02:09
-
they schedule this improvement to 1.2? why there is no any VOTE button there – William Leung Mar 10 '17 at 09:49
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
}
}

- 5,122
- 7
- 40
- 67
-
1For 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
With the Kotlin DSL:
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
kotlinOptions.allWarningsAsErrors = true
}
}

- 18,524
- 17
- 70
- 98
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")
}

- 16,801
- 8
- 50
- 77