【问题标题】:KMM, after involving XCTest, get build error "Building for iOS Simulator, but linking in dylib built for iOS..."KMM,在涉及 XCTest 后,得到构建错误\“为 iOS 模拟器构建,但在为 iOS 构建的 dylib 中链接......\”
【发布时间】:2022-11-09 19:39:19
【问题描述】:

我做了一个实验,将 iOS UI 测试添加到使用 Kotlin Multiplatform Mobile(KMM) 创建的项目中。通过开始遵循官方指南,我能够在 Xcode 中连接共享库并启动 iOS 应用程序或从 Android Studio 执行单元测试。但是当我尝试让 XCTest 添加一些 UI 测试时,Xcode 抱怨如下截图。

我已经在互联网上搜索了很多,仍然没有运气。伙计们,如果你以前也遇到过同样的问题,请给我一些关于如何追踪这个拱门问题的提示。

从构建日志错误来看,我认为首先 Gradle Task :shared:linkDebugFrameworkIosSimulatorArm64 FAILED 和下面说 XCTest 是为 iOS arm64 arch 构建的,它与 iOS Simulator 不一致。

我正在使用 Mac M1 机器,这可能是原因。所以我将 Xcode 切换到 Rosetta 模式,这次来自 Run Script 的命令 embedAndSignAppleFrameworkForXcodeNO-SOURCE 并遵循一个 iOS 模拟器版本对齐抱怨。

XCTest.def

language = Objective-C
package = platform.XCTest
depends = UIKit
modules = XCTest
linkerOpts= -weak_framework XCTest -L/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/UIKit.framework -F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks/
compilerOpts= -weak_framework XCTest -F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/

build.gradle 文件

import com.android.build.gradle.internal.scope.ProjectInfo.Companion.getBaseName

plugins {
    kotlin("multiplatform")
    id("com.android.library")
}

kotlin {
    android {

    }

    listOf(
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
            embedBitcode = org.jetbrains.kotlin.gradle.plugin.mpp.Framework.BitcodeEmbeddingMode.DISABLE
        }
        it.compilations.getByName("main") {
            val xctest by cinterops.creating {
                // Def-file describing the native API.
                defFile(project.file("src/iosMain/xctest.def"))
            }
        }
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
//                implementation(
//                    "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.5-native-mt"
//                )
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-stdlib")
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.5-native-mt")

                implementation("androidx.test.espresso:espresso-core:3.2.0")
                implementation("androidx.test.espresso:espresso-contrib:3.2.0")

                implementation("androidx.test:core:1.4.0")
                implementation("androidx.test.ext:junit:1.1.3")
                implementation("androidx.test.uiautomator:uiautomator:2.2.0")
            }
        }
        val androidTest by getting {
            dependencies {
            }
        }

        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }

        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    namespace = "com.bsc.radiant_hope_test"
    compileSdk = 32
    defaultConfig {
        minSdk = 21
        targetSdk = 32
    }
}

共享库已连接。

【问题讨论】:

    标签: swift xcode xctest kotlin-multiplatform kotlin-multiplatform-mobile


    【解决方案1】:

    在这种情况下,您不需要使用 Rosetta

    正如我在您的 XCTest.def 文件中看到的那样,您正试图将使用 Kotlin 工具创建的所有二进制文件与不包含运行模拟器所需部件的 iPhoneOS.platform 链接。

    请尝试将以下行添加到您的 XCTest.def 文件中

    #设备的链接器选项

    linkerOpts.ios_arm64 = -weak_framework XCTest -L/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/UIKit.framework -F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks/
    

    #Linker 模拟器选项

    linkerOpts.ios_x64 = -weak_framework XCTest -L/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/UIKit.framework -F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks/
    
    linkerOpts.ios_simulator_arm64 = -weak_framework XCTest -L/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/UIKit.framework -F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks/
    

    【讨论】:

      猜你喜欢
      • 2022-06-23
      • 2014-05-30
      • 2013-09-11
      • 2021-01-27
      • 2021-01-07
      • 2021-05-04
      • 2021-05-27
      • 2016-05-25
      • 1970-01-01
      相关资源
      最近更新 更多