如何在多平台 Android 模块中配置 Kotlin jvmTarget?
How to configure Kotlin jvmTarget in a Multiplatform Android module?
我遇到这个构建错误:
Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option
Adding support for Java 8 language features could solve this issue.
尝试在 Android Studio 中为多平台模块编译此构建脚本:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
}
repositories {
mavenCentral()
}
kotlin {
android()
ios {
binaries {
framework {
baseName = "bandit"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.1.1")
implementation("com.squareup.okio:okio-multiplatform:3.0.0-alpha.3")
implementation("com.squareup.okio:okio-fakefilesystem-multiplatform:3.0.0-alpha.3")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting
val iosTest by getting
all {
languageSettings.apply {
enableLanguageFeature("InlineClasses")
useExperimentalAnnotation("kotlin.time.ExperimentalTime")
useExperimentalAnnotation("okio.ExperimentalFileSystem")
}
}
}
}
android {
compileSdkVersion(30)
defaultConfig {
minSdkVersion(23)
targetSdkVersion(30)
}
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
val packForXcode by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
val framework =
kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
val targetDir = File(buildDir, "xcode-frameworks")
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
这是一个非常普通的多平台构建脚本,大部分由 Jetbrain 的移动插件自动生成。我无法通过将其插入 Android 块的底部以通常的方式解决此问题:
kotlinOptions {
jvmTarget = "1.8"
}
kotlinOptions 是此处未解决的引用。考虑到此错误的普遍性,令我感到惊讶的是互联网上解决该错误的内容如此之少。有一些相同或相似的 jvmTarget 错误的帖子,但它们都是从与此不同的构建上下文中发布的。有人有完全相同的错误的唯一实例是在这里:
奇怪的是,据称原始发布者仅通过修补他的 androidx 导入就设法解决了这个问题。也许值得注意的是,我的所有六个“无法内联字节码”都与对新的多平台 Okio 库的方法调用相关联。但肯定更有可能是我自己的设置出了问题,而不是 Jake-Wharton-gang 所为。
根据文档:
https://kotlinlang.org/docs/mpp-dsl-reference.html#compilation-parameters
不要被 Gradle 文档的代码示例所迷惑,认为很多此编译语法仅适用于 Java 块。这是为 Android 构建指定 jvm 版本目标的方式:
kotlin {
android {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
}
ios {
...
我遇到这个构建错误:
Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option Adding support for Java 8 language features could solve this issue.
尝试在 Android Studio 中为多平台模块编译此构建脚本:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
}
repositories {
mavenCentral()
}
kotlin {
android()
ios {
binaries {
framework {
baseName = "bandit"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.1.1")
implementation("com.squareup.okio:okio-multiplatform:3.0.0-alpha.3")
implementation("com.squareup.okio:okio-fakefilesystem-multiplatform:3.0.0-alpha.3")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting
val iosTest by getting
all {
languageSettings.apply {
enableLanguageFeature("InlineClasses")
useExperimentalAnnotation("kotlin.time.ExperimentalTime")
useExperimentalAnnotation("okio.ExperimentalFileSystem")
}
}
}
}
android {
compileSdkVersion(30)
defaultConfig {
minSdkVersion(23)
targetSdkVersion(30)
}
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
val packForXcode by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
val framework =
kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
val targetDir = File(buildDir, "xcode-frameworks")
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
这是一个非常普通的多平台构建脚本,大部分由 Jetbrain 的移动插件自动生成。我无法通过将其插入 Android 块的底部以通常的方式解决此问题:
kotlinOptions {
jvmTarget = "1.8"
}
kotlinOptions 是此处未解决的引用。考虑到此错误的普遍性,令我感到惊讶的是互联网上解决该错误的内容如此之少。有一些相同或相似的 jvmTarget 错误的帖子,但它们都是从与此不同的构建上下文中发布的。有人有完全相同的错误的唯一实例是在这里:
奇怪的是,据称原始发布者仅通过修补他的 androidx 导入就设法解决了这个问题。也许值得注意的是,我的所有六个“无法内联字节码”都与对新的多平台 Okio 库的方法调用相关联。但肯定更有可能是我自己的设置出了问题,而不是 Jake-Wharton-gang 所为。
根据文档: https://kotlinlang.org/docs/mpp-dsl-reference.html#compilation-parameters
不要被 Gradle 文档的代码示例所迷惑,认为很多此编译语法仅适用于 Java 块。这是为 Android 构建指定 jvm 版本目标的方式:
kotlin {
android {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
}
ios {
...