Due to iOS linking issues that only emerge after release builds are published, and only with dynamic frameworks being built by Kotlin, you need to build static frameworks when publishing. However, SwiftUI previews require dynamic frameworks.
As a workaround, you can configure Gradle to create dynamic frameworks when developing, and static frameworks when releasing.
For more context, see this discussion in the Kotlin Slack.
Direct Embedding
This workaround is for projects that build and link Kotlin by using the direct embedding method. If you are build XCFrameworks and integrating through another mechanism, you’ll need to write your own Gradle, but it should be straightforward to do.
targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
val isRelease = System.getenv()["KOTLIN_FRAMEWORK_BUILD_TYPE"] == "release"
if (isRelease == false) {
compilations["main"].kotlinOptions.freeCompilerArgs += listOf(
"-linker-options",
"-U _FIRCLSExceptionRecordNSException"
)
}
binaries.framework {
isStatic = isRelease
}
}
When Xcode creates a build, it passes environment variables to scripts that it calls. KOTLIN_FRAMEWORK_BUILD_TYPE
can be checked to see if this is a debug or release build. Release builds become dynamic framework, which require some custom linker flags.
The full workaround has not been tested by our team. That will happen in the near future. You may need to adjust the script.