简体   繁体   中英

How to configure Kotlin jvmTarget in a Multiplatform Android module?

I'm getting this build error:

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.

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)

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 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:

kotlinOptions in kotlin multiplatform project

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.

As per the documentation: https://kotlinlang.org/docs/mpp-dsl-reference.html#compilation-parameters

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 {
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM