https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

sushma nayak

04/08/2021, 1:32 PM
Building a KMM shared library linked iOS project through Xcode on Apple M1 mac failing.
Interesting thing, it runs on Android studio.
y

yousefa2

04/08/2021, 8:54 PM
Since you are on M1 chip which is arm64 you need to switch this part of your config from
Copy code
val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
            ::iosArm64
        else
            ::iosX64
    iosTarget("ios") {}
to just
Copy code
iosArm64("ios") {}
d

Dmitriy Tarasevich

04/08/2021, 11:16 PM
or you can build a fat version of it by doing the following
Copy code
val buildXcFramework by tasks.registering {
    dependsOn("deleteXcFramework")
    group = "build"
    val mode = "Release"
    val frameworks = arrayOf("iosArm64", "iosX64")
        .map {
            kotlin.targets.getByName<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget>(it).binaries.getFramework(
                mode
            )
        }
    inputs.property("mode", mode)
    dependsOn(frameworks.map { it.linkTask })
    doLast { buildXcFramework(frameworks) }
}

fun Task.buildXcFramework(frameworks: List<org.jetbrains.kotlin.gradle.plugin.mpp.Framework>) {
    val buildArgs: () -> List<String> = {
        val arguments = mutableListOf("-create-xcframework")
        frameworks.forEach {
            arguments += "-framework"
            arguments += "${it.outputDirectory}/$frameworkName.framework"
        }
        arguments += "-output"
        arguments += xcFrameworkPath
        arguments
    }
    exec {
        executable = "xcodebuild"
        args = buildArgs()
    }
}
2
8 Views