xcode KMM,在涉及XCTest后,得到构建错误“为iOS模拟器构建,但在为iOS构建的dylib中链接...”

qybjjes1  于 2022-11-17  发布在  iOS
关注(0)|答案(1)|浏览(190)

我做了一个实验,将iOS UI测试添加到使用KotlinMultiplatform移动的(KMM)创建的项目中。通过开始遵循官方指南,我能够连接Xcode中的共享库并启动iOS应用程序或从Android Studio执行单元测试。但当我尝试使用XCTest添加一些UI测试时,Xcode投诉如下截图所示。
我已经在网上搜索了很多,仍然没有运气。伙计们,如果你们以前也遇到过同样的问题,请给予我一些关于如何追踪这个拱门问题的提示。

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

我使用的是Mac M1机器,这可能是原因。所以我把Xcode切换到Rosetta模式,这一次命令embedAndSignAppleFrameworkForXcode,这是从运行脚本有NO-SOURCE,并遵循一个iOS模拟器版本对齐投诉。

XC测试定义

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/

构建.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
    }
}

已连接共享库。


指令集

ql3eal8s

ql3eal8s1#

在这种情况下,您不需要使用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/

模拟器的链接器选项

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/

相关问题