sushma nayak

    sushma nayak

    1 year ago
    Building a KMM shared library linked iOS project through Xcode on Apple M1 mac failing.
    Interesting thing, it runs on Android studio.
    yousefa2

    yousefa2

    1 year ago
    Since you are on M1 chip which is arm64 you need to switch this part of your config from
    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
            if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
                ::iosArm64
            else
                ::iosX64
        iosTarget("ios") {}
    to just
    iosArm64("ios") {}
    Dmitriy Tarasevich

    Dmitriy Tarasevich

    1 year ago
    or you can build a fat version of it by doing the following
    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()
        }
    }