【问题标题】:How to set Kotlin source encoding in Gradle?如何在 Gradle 中设置 Kotlin 源编码?
【发布时间】:2017-08-08 09:23:33
【问题描述】:

使用 Gradle 构建 Java 或 Groovy 时,可以像这样定义源编码:

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

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

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

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

但是,使用 Kotlin 的类似方法不起作用:

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

它失败并出现错误:

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

我实际上根本找不到任何关于 Kotlin 编译器编码的信息。这是否意味着没有这样的选择?那么它使用什么字符集,UTF-8,系统默认值(我希望不是)?

【问题讨论】:

  • 我很确定它始终是 UTF-8,尽管我找不到它的文档。
  • Does it mean that there is no such option? - 检查源代码后,我可以告诉你:不,没有这样的选项。您可以检查插件here 的 DSL。特别是KotlinJvmOptions* 对您有兴趣了解什么是可用的,什么是不可用的。不幸的是,我不能告诉你他们使用哪种默认编码......
  • 我在medium.com/@napperley/… 找到了这个声明All Kotlin source files use the UTF-8 character encoding。但这似乎是最佳实践。我猜如果 UTF-8 有效而其他编码无效并且无法指定实际编码,那么您将被困在 UTF-8 上。
  • @Natix 你的 gradle 包装器版本是什么?
  • @JTeam 视情况而定,我通常会尝试使用可用的最新 Gradle。有关系吗?

标签: gradle character-encoding kotlin


【解决方案1】:

Kotlin 源文件在设计上始终为 UTF-8。无法指定任何其他编码。

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    相关资源
    最近更新 更多