KMM with iOS Arm64 Binary Framework (.xcframework) and cinterop

Viewed 780

I'm following the guide here to try and use an iOS framework without CocoaPods in a new KMM project:

https://kotlinlang.org/docs/kmm-add-dependencies.html#without-cocoapods

I have an existing, working .xcframework that I added to the project under shared/src. I added a MyKit.def file in src/nativeInterop/cinterop/ and updated the build.gradle.kts file in same shared dir:

MyKit.def looks like

language = Objective-C
modules = MyKit
package = MyKit

build.gradle.kts

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

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

kotlin {
    android()

    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
            ::iosArm64
        else
            ::iosX64

    iosTarget("ios") {
        binaries {
            framework {
                baseName = "shared"
            }
        }
    }
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
        val iosMain by getting
        val iosTest by getting
    }
    iosArm64() {
        compilations.getByName("main") {
            val MyKit by cinterops.creating {
                // Path to .def file
                defFile("src/nativeInterop/cinterop/MyKit.def")
                compilerOpts("-framework", "MyKit", "-F/src/MyKit.framework")
            }
        }

        binaries.all {
            // Linker options required to link to the library -- the framework binary is located under src/MyKit.framework/ios-arm64/MyKit.framework/
            linkerOpts("-framework", "MyKit", "-F/src/MyKit.framework/ios-arm64/MyKit.framework/")
        }
    }
}

android {
    compileSdkVersion(31)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(26)
        targetSdkVersion(31)
    }
}

After adding import MyKit.* to my MainActivity I get unknown reference errors.

Are iOS binary frameworks supported? They have a . separator in the file name so maybe that's an issue. Problem with my -F paths?? I'm not clear on whether the path should be all the way to the dir with Headers and the binary itself or just to the Framework root. TIA

0 Answers
Related