如何在多平台Android模块中配置Kotlin jvmTarget? [英] How to configure Kotlin jvmTarget in a Multiplatform Android module?

查看:83
本文介绍了如何在多平台Android模块中配置Kotlin jvmTarget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此构建错误:

不能将用JVM target 1.8构建的字节码内联到用JVM target 1.6构建的字节码中.请指定正确的"-jvm-target"选项添加对Java 8语言功能的支持可以解决此问题.

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中为多平台模块编译此构建脚本:

Trying to compile this build script for a multiplatform module in 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)

这是一个非常漂亮的Multiplatform构建脚本,大部分由Jetbrain的Mobile插件自动生成.我无法通过在Android块底部插入此代码来解决此常规方法:

This is a pretty vanilla Multiplatform build script, mostly auto-generated by Jetbrain's Mobile plugin. I cannot solve this the usual way by inserting this at the bottom of the Android block:

    kotlinOptions {
        jvmTarget = "1.8"
    }

kotlinOptions在这里是未解决的参考.考虑到此错误的普遍性,令我惊讶的是,互联网上解决该错误的地方很少.有几篇jvmTarget错误相同或相似的帖子,但它们都是从与此版本不同的构建上下文中发布的.错误完全相同的人的唯一实例是在这里:

kotlinOptions is an unresolved reference here. Considering how generic this error is, I'm surprised how little there is on the internet addressing it. There are a few posts of same or similar jvmTarget errors, but they were all posted from build contexts different to this one. The only instance of somebody having exactly the same error is here:

kotlin多平台项目中的kotlinOptions

奇怪的是,据称原始海报仅通过修补androidx导入就设法解决了这个问题.可能值得注意的是,我的所有"Cannot inline bytecode"(无法内嵌字节码)中的全部六个与对新的多平台Okio库的方法调用相关联.但是,肯定是我自己的设置出现了错误,而不是杰克·沃顿帮派所做的事情.

Strangely enough, the original poster allegedly managed to solve the problem just by tinkering with his androidx imports. It is perhaps worth noting that all six of my "Cannot inline bytecode" are associated with method calls to the new multiplatform Okio library. But it's surely more likely there's an error with my own setup rather than something the Jake-Wharton-gang have done.

推荐答案

根据文档: https://kotlinlang.org/docs/mpp-dsl-reference.html#compilation-parameters

不要被Gradle文档的代码示例所迷惑,以为很多这种编译语法仅适用于Java块.这是您为Android构建指定jvm版本目标的方式:

Don't be fooled by the Gradle docs' code examples into thinking a lot of this compilation syntax applies only to the Java block. This is how you specify the jvm version target for an Android build:

kotlin {
    android {
        compilations.all {
            kotlinOptions.jvmTarget = "1.8"
        }
    }
    ios {
        ...

这篇关于如何在多平台Android模块中配置Kotlin jvmTarget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆