Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Kotlin source encoding in Gradle?

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)?

like image 953
Natix Avatar asked Aug 08 '17 09:08

Natix


People also ask

How do I change the encoding in Gradle?

If we want to set an explicit encoding for the Java compiler in Gradle we can use the options. encoding property. For example we could add the following line to our Gradle build file to change the encoding for the compileJava task: ?

What is DSL in Gradle?

Simply, it stands for 'Domain Specific Language'. IMO, in gradle context, DSL gives you a gradle specific way to form your build scripts. More precisely, it's a plugin-based build system that defines a way of setting up your build script using (mainly) building blocks defined in various plugins.

How do I change my Kotlin compiler version?

Go to Intellij Preferences -> Build, Execution, Deployment -> Kotlin Compiler. Update Language version and Api version to the one you wish.

What is Kotlin Gradle DSL?

Kotlin DSL brings the simplicity of the Kotlin language syntax and rich API set right into the script files on top of that code completion makes it perfect to work with Gradle script files. We will use this to manage our dependencies and project settings configurations more elegantly.


1 Answers

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

like image 51
Roman Elizarov Avatar answered Sep 20 '22 19:09

Roman Elizarov