首页 > 解决方案 > 无法将 ios 应用程序构建到设备中,因为“嵌入式框架 'SharedCode.framework' 是为 iOS 模拟器构建的。”

问题描述

我使用 Kotlin Native(多平台)并且我创建了一个 TestKotinNative 应用程序,但只有在模拟器上运行时才能工作。当我在我的设备(iphone 7、13.4.1)上运行时,xcode 显示错误“TestKotlinNative.xcodeproj Building for iOS,但链接和嵌入式框架 'SharedCode.framework' 是为 iOS Simulator 构建的。” 我的 Xcode 版本:11.4.1 请帮帮我!感谢

这是我的 build.gradle.kts (与https://play.kotlinlang.org/hands-on/Targeting%20iOS%20and%20Android%20with%20Kotlin%20Multiplatform/06_SettingUpKotlinFramework相同)

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
}

kotlin {
    //select iOS target platform depending on the Xcode environment variables
    val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
            ::iosArm64
        else
            ::iosX64

    iOSTarget("ios") {
        binaries {
            framework {
                baseName = "SharedCode"
            }
        }
    }

    jvm("android")

    sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
    }

    sourceSets["androidMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib")
    }

}

val packForXcode by tasks.creating(Sync::class) {
    val targetDir = File(buildDir, "xcode-frameworks")

    /// selecting the right configuration for the iOS
    /// framework depending on the environment
    /// variables set by Xcode build
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val framework = kotlin.targets
        .getByName<KotlinNativeTarget>("ios")
        .binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)

    from({ framework.outputDirectory })
    into(targetDir)

    /// generate a helpful ./gradlew wrapper with embedded Java path
    doLast {
        val gradlew = File(targetDir, "gradlew")
        gradlew.writeText("#!/bin/bash\n"
                + "export 'JAVA_HOME=${System.getProperty("java.home")}'\n"
                + "cd '${rootProject.rootDir}'\n"
                + "./gradlew \$@\n")
        gradlew.setExecutable(true)
    }
}

tasks.getByName("build").dependsOn(packForXcode)

在此处输入图像描述

标签: ioskotlinkotlin-native

解决方案


在 Kotlin 多平台框架中继续使用新版本的 xCode 的一种肮脏方式是……在更改平台(模拟器和 iphone)以运行您的项目之前,只需进入框架位置并删除框架文件。XCode 将重新构建您的框架,应用程序将部署在您选择的平台上


推荐阅读