如何在Android Studio项目的Java应用程序中使用kotlinOptions?

huangapple go评论67阅读模式
英文:

How can i use kotlinOptions in Java Application in AndroidStudioProject?

问题

我不理解 KotlinOptions 是什么。
我正在使用 Kohii 库,该库告诉我在 gradle 文件中添加这个 KotlinOptions 代码?
但是我在我的 Android Studio 项目中主要使用 Java 作为主要语言,我如何配置下面的这段代码?

compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
  jvmTarget = "1.8"
  freeCompilerArgs += [
      '-Xjvm-default=enable'
  ]
}
英文:

I do not understand what KotlinOptions is.
I am using Kohii Library and this library tell me to add this KotlinOptions code in gradle file?
But I am using Java as main language for my android studio project How can i config this code below?

compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
  jvmTarget = "1.8"
  freeCompilerArgs += [
      '-Xjvm-default=enable'
  ]
}

答案1

得分: 2

看起来 Kohii 使用了带有 JAVA8 特性的 Kotlin,您需要在 build.gradle 中为 Kotlin 使用指定 Java 版本。即使您的项目使用 Java 作为主要语言,您选择的库使用的是 Kotlin。好消息是 Java 和 Kotlin 是可互操作的,只需要告诉编译器如何编译 Kotlin 部分即可。

kotlinOptions 用于告诉编译器有关 Kotlin 的不同配置。例如:

jvmTarget = "1.8"(Java 8)- 生成的 JVM 字节码的目标版本(1.6、1.8、9、10、11 或 12),默认为 1.6。

freeCompilerArgs - 附加编译器参数的列表。

所有配置参数和解释可以在这里找到

将配置放入 build.gradle 中,如下所示:

android {
    ...
    compileOptions {...}
    kotlinOptions {...}
    ...

}

可能您还需要为项目启用 Kotlin 支持

英文:

It seems Kohii uses Kotlin with JAVA8 features and you need to specify version of Java for Kotlin use in build.gradle. Even though your project is using Java as its primary language, the library you chose uses Kotlin. The good news is that Java and Kotlin are interoperable, the compiler just needs to be told how the Kotlin parts should be compiled.

kotlinOptions tells compiler different configurations for Kotlin. E.g:

jvmTarget = "1.8" (Java 8) - target version of the generated JVM bytecode (1.6, 1.8, 9, 10, 11 or 12), default is 1.6

freeCompilerArgs - A list of additional compiler arguments

All configuration parametrs and explanations can be found here

Put the configuration in build.gradle like this

android {
    ...
    compileOptions {...}
    kotlinOptions {...}
    ...

}

Probably you also need to enable Kotlin support to your project

huangapple
  • 本文由 发表于 2020年9月1日 23:06:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63690382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定