Hello everyone, I am trying to embed Swift xcFramework into a shared KMP library. I have created a w...
a

adjorno

9 months ago
Hello everyone, I am trying to embed Swift xcFramework into a shared KMP library. I have created a wrapper with Obj-C compatible API and proper public header file, made an cinterop to bridge the API to Kotlin. So the shared module compiles nicely. But when I try to launch the iOS app it fails with
> Task :shared:linkDebugFrameworkIosArm64 FAILED
error: KLIB resolver: Could not find "/Users/developer/Developer/Sources/KMP/shared/src/iosMain/c_interop/module/KMPWrapper.xcframework/ios-arm64_x86_64-simulator" in [/Users/developer/Developer/Sources/KMP, /Users/developer/.konan/klib, /Users/developer/.konan/kotlin-native-prebuilt-macos-aarch64-2.0.0/klib/common, /Users/developer/.konan/kotlin-native-prebuilt-macos-aarch64-2.0.0/klib/platform/ios_arm64]
error: Compilation finished with errors
as I am trying to export KMPWrapper xcFramework as a part of the shared module:
targets
        .filterIsInstance<KotlinNativeTarget>()
        .filter { it.konanTarget.family == Family.IOS }
        .forEach {
            val archXCFrameworkPath = when {
                it.targetName.equals("arm64", ignoreCase = true) -> "$projectDir/src/iosMain/c_interop/module/KMPWrapper.xcframework/ios-arm64"
                else -> "$projectDir/src/iosMain/c_interop/module/KMPWrapper.xcframework/ios-arm64_x86_64-simulator"
            }

            it.binaries.framework {
                baseName = "shared"
                isStatic = true
                @OptIn(ExperimentalKotlinGradlePluginApi::class)
                transitiveExport = true
                export(files(archXCFrameworkPath))
            }

            it.compilations.getByName("main") {
                val KMPWrapper by cinterops.creating {
                    // Path to the .def file
                    definitionFile.set(project.file("src/iosMain/c_interop/KMPWrapper.def"))


                    compilerOpts(
                        "-framework",
                        "KMPWrapper",
                        "-F$archXCFrameworkPath",
                        "-fmodules",
                    )
                }
            }

            it.binaries.all {
                linkerOpts(
                    "-framework",
                    "KMPWrapper",
                    "-F$archXCFrameworkPath",
                    "-fmodules"
                )
            }

        }
What am I doing wrong?