19

When building Java or Groovy using Gradle, it's possible to define source encoding like this:

compileJava {
    options.encoding = 'UTF-8'
}

compileTestJava {
    options.encoding = 'UTF-8'
}

compileGroovy {
    groovyOptions.encoding = 'UTF-8'
}

compileTestGroovy {
    groovyOptions.encoding = 'UTF-8'
}

However, similar approach with Kotlin doesn't work:

compileKotlin {        
    kotlinOptions.jvmTarget = '1.8'
    kotlinOptions.encoding = 'UTF-8'
}

It fails with the error:

* What went wrong:
A problem occurred evaluating root project 'backend'.
> No such property: encoding for class: org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl

I actually can't find any info about Kotlin compiler's encoding at all. Does it mean that there is no such option? What charset does it use then, UTF-8, system default (I hope not)?

Natix
  • 14,017
  • 7
  • 54
  • 69
  • I'm pretty sure it's always UTF-8, although I can't find it documented. – ephemient Aug 08 '17 at 17:02
  • 5
    `Does it mean that there is no such option?` - After checking there source code I can tell you: No. There is no such option. You can check the DSL for the plugin [here](https://github.com/JetBrains/kotlin/tree/master/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl). Specially the `KotlinJvmOptions*` are interested for you for what is available and whats not. Unfortunately I can't say you which default encoding they use... – StefMa Aug 10 '17 at 14:21
  • I found this statement `All Kotlin source files use the UTF-8 character encoding` at https://medium.com/@napperley/unofficial-kotlin-style-guide-a44cb899a84f. But it seems to be best practices. I guess that if UTF-8 works and other encodings don't and there is no way to specify the actual encoding, you are stuck to UTF-8. – Xvolks Aug 10 '17 at 15:22
  • @Natix what is your gradle wrapper version? – JTeam Aug 17 '17 at 10:30
  • @JTeam Depends, I usually try to use the latest Gradle available. Does it matter? – Natix Aug 17 '17 at 12:07

1 Answers1

17

Kotlin source files are always UTF-8 by design. There is no way to specify any other encoding.

Roman Elizarov
  • 27,053
  • 12
  • 64
  • 60