swalkner
swalkner

Reputation: 17359

KMM xcframework used in Xcode needs Rosetta: "building for iOS Simulator-arm64 but attempting to link with file built for iOS Simulator-x86_64"

I create a xcframework out of an Android KMM project via

#!/bin/bash

./gradlew :shared:packForXCodeArm -PXCODE_CONFIGURATION=Release
./gradlew :shared:packForXCodeX64 -PXCODE_CONFIGURATION=Release
FRAMEWORK_NAME="shared"
ARM64PATH="shared/build/xcode-framework-arm/${FRAMEWORK_NAME}.framework"
X64PATH="shared/build/xcode-framework-X64/${FRAMEWORK_NAME}.framework"
UNIVERSAL_PATH="shared/build/xcode-framework-universal/"

xcodebuild -create-xcframework -framework "${ARM64PATH}" -framework "${X64PATH}" -output "${UNIVERSAL_PATH}/${FRAMEWORK_NAME}.xcframework"

My build.gradle.kts contains:

val packForXcodeArm by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val framework = kotlin.targets.getByName<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget>("iosArm64").binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-framework-arm")
    from({ framework.outputDirectory })
    into(targetDir)
}

val packForXcodeX64 by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val framework = kotlin.targets.getByName<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget>("iosX64").binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-framework-X64")
    from({ framework.outputDirectory })
    into(targetDir)
}

In Xcode on my M1 when not using Rosetta I get

ld: warning: ignoring file /Users/.../Library/Developer/Xcode/DerivedData/froeling_ios-dwqfcfqkcvofjtgtuipjhkedehfx/Build/Products/Debug-iphonesimulator/shared.framework/shared, building for iOS Simulator-arm64 but attempting to link with file built for iOS Simulator-x86_64
Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_SharedNetworkRequests", referenced from:
      objc-class-ref in LoginView.o

Is there a way to create an xcframework that also works without Rosetta in Xcode?

Upvotes: 0

Views: 839

Answers (1)

kakaiikaka
kakaiikaka

Reputation: 4487

That error is normal because you are are missing the symbols built for iosSimulatorArm64 .

I'm not using the KMM, but I checked. Since version 1.5.30, Kotlin supports 4 new targets:

  1. macosArm64
  2. iosSimulatorArm64
  3. watchosSimulatorArm64
  4. tvosSimulatorArm64

See its release notes here: Kotlin 1.5.30

So to get rid of that error, build against iosSimulatorArm64 and join the built result with other platform to get a 'real' universal xcframwork please.

Upvotes: 1

Related Questions