Kotlin Multiplatform library exported to .framework crashes on Xcode

122 views Asked by At

I'm trying to implement a library using Kotlin Multiplatform to use in my iOS project so I download the official library template, which implements a Fibonacci sequence.

I did only 2 changes in this project:

  1. I changed the Kotlin version from 1.9.0 to 1.9.21
  2. In the file library/build.gradle.kts I updated the target iosSimulatorArm64() to output a .framework so I could use it on my Xcode iOS project, like this:
    iosSimulatorArm64() {
        binaries {
            framework {
                baseName = "TestLibrary"
            }
        }
    }

Then I ran the command ./gradlew iosSimulatorArm64Binaries and the TestLibrary.framework was properly generated for me.

However, after I include this .framework in my project, my app crashes as soon as try to run the it (the Xcode project builds without any problem; it only crashes when I actually try to run the app), throwing this error:

Xcode crash

I not even tried to run the Fibonacci code in the iOS project yet; the app crashes by simply adding the framework and trying to run the app. Any idea what is happening here?

1

There are 1 answers

0
vegidio On

I couldn't find a reason why my app is crashing when I create a .framework and use it in Xcode, however I found a different solution that lets me achieve the same thing that I wanted to do originally: create a code in Kotlin Multiplatform and invoke this code in my iOS project using Swift.

So, instead of creating a .framework file I decided to create a .xcframework. Then when I added xcframework in my iOS project the app no longer crashed.

To make your KMP library output a .xcframework you just need to update your build.gradle.kts file and add this:

val frameworkName = "TestLibrary"
val xcf = XCFramework(frameworkName)
val iosTargets = listOf(iosSimulatorArm64()) // Include more targets here if you want

iosTargets.forEach {
    it.binaries.framework {
        baseName = frameworkName
        xcf.add(this)
    }
}