【问题标题】:Can not build ios app into device because "embedded framework 'SharedCode.framework' was built for iOS Simulator."无法将 ios 应用程序构建到设备中,因为“嵌入式框架 'SharedCode.framework' 是为 iOS 模拟器构建的。”
【发布时间】:2020-09-08 23:12:56
【问题描述】:

我使用 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)

enter image description here

【问题讨论】:

    标签: ios kotlin kotlin-native


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

    在您的脚本中,您声明iOSTarget 变量始终设置为iosX64 目标,这是一个模拟器。在动手示例中,想法是根据 Xcode 构建设置选择目标。但在您的代码中,这部分被注释为默认设置唯一的模拟器。尝试将其修复为与实际操作最终版本完全相同(请参阅here

    【讨论】:

    • 是的,我使用赞誉 ``` // if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true) // ::iosArm64 // else / / ::iosX64 ``` 但不起作用。所以我努力尝试 ::iosX64 或 ::iosArm64,它适用于模拟器或设备时使用“if else”构建总是失败
    • “构建总是失败”是什么意思?如果您设置了 iosArm64 并开始为设备构建,它是否可以正常工作?
    • 这是错误的,自 XCode 11.4 起,动手示例不再有效,无论初始检查如何,XCode 都将无法构建应用程序。
    • 嗨,@theolll!您能否在此处详细说明 Xcode 11.4 的不兼容性?第一次听说,这个问题好像很严重。
    • 你好,@Georg!目前,不支持开箱即用的 XCFrameworks,但正在开展工作来支持它们(请参阅youtrack.jetbrains.com/issue/KT-42667)。作为另一种选择,您可以声明机器人模拟器和设备目标,并按照here 的描述制作一个胖框架。
    猜你喜欢
    • 1970-01-01
    • 2021-05-30
    • 2020-07-07
    • 1970-01-01
    • 2020-11-25
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多