Migrate from Objective-C interop to Swift export
Previously, Kotlin code was available to Swift only indirectly, through an Objective-C framework. Swift export generates a real Swift API from your Kotlin sources instead, so there's no Objective-C layer in between.
This guide compares the two approaches and describes how to migrate an existing Kotlin Multiplatform project from the Objective-C framework to Swift export:
Switch the Gradle build from
binaries.framework {}to theexport { swift {} }block.Switch the Xcode project to the direct integration or the Swift package integration.
Prepare the exported Kotlin API to control which public declarations and dependencies Swift export exposes.
Rewrite the Swift code since almost every Kotlin declaration is different in Swift.
Check the requirements
Swift export is in Beta and has stricter requirements than interoperability through Objective-C. Check all of them before you start the migration:
Requirement | Details |
|---|---|
Kotlin | 2.5.0 or later. Compiler plugins and KSP processors must have versions compatible with that Kotlin version. Earlier versions have the deprecated |
Build tools | Gradle 8.14 or later, and Android Gradle plugin 8.8.2 or later if your project includes an Android target. |
Deployment target | iOS 18.0, macOS 15.0, tvOS 18.0, or watchOS 11.0 and later. The generated concurrency support relies on newer Apple runtime APIs, and the build fails with a lower deployment target. |
Xcode | Xcode 16 or later, with |
Xcode integration type | CocoaPods integration is not supported. Move the project to Swift package integration or direct integration first before the migration. |
If your project can't meet one of these requirements yet, for example because the app must support older iOS versions, postpone the migration instead of partially applying it.
Review your current setup
Before changing anything, collect the following information about your project:
The umbrella Kotlin module that the Apple app consumes, the framework's
baseName, and the dependencies listed inexport().The Xcode project and targets that link the framework, together with their build phases and build settings, and the integration type they use: direct integration, CocoaPods, or a Swift package produced by a custom XCFramework setup.
The Kotlin dependencies and compiler plugins involved in the Objective-C export, especially SKIE and KMP-NativeCoroutines.
Kotlin annotations specific to Objective-C interop:
@ObjCName,@HiddenFromObjC,@ShouldRefineInSwift, and@Throws, plus KMP-NativeCoroutines annotations such as@NativeCoroutines.The Objective-C idioms used in Swift code:
FooKt.file facades,KotlinIntand other boxed types,companion,doCopy(), completion handlers, and SKIE helpers such asonEnum(of:).
Build the project once before you start, so that any later failure is attributable to the migration.
Update the Gradle configuration
Replace the framework declaration in the umbrella module with the export {} block of the kotlin {} extension:
Each exported Gradle project gets its own Swift module. Keeping the old framework name preserves imports only for declarations from that module. In a multimodule app, also update imports for declarations from exported dependencies to their new module names.
When you rewrite the build file, keep in mind the following differences:
Swift export builds its own static library, so the
binaries.framework {}block is no longer necessary. Remove it unless another consumer still relies on the Objective-C framework. ThebaseName,isStatic,export(),transitiveExport, andlinkerOptsoptions have no effect on Swift export.There's no
export()list anymore. Directapidependencies are exported fully, whileimplementationand transitive dependencies are exported only as far as they are reachable from the exported public API. Use theconfigure()function inside thexcodeIntegration {}orswiftPackageIntegration {}block to override the options of a dependency or to exclude it withSwiftExportVisibility.HIDDEN.The umbrella module must declare the integration it uses.
xcodeIntegration()registers theembedSwiftExportForXcodetask for the direct integration, andswiftPackageIntegration()registers theexport<BuildType>SwiftPackagetasks for the Swift package integration. Without one of them, no integration task is registered. You can declare both if different consumers need different outputs.Never combine the
export { swift {} }block with the deprecatedswiftExport {}block, as it's a build error.rootPackagecorresponds toflattenPackageused in the old DSL.Compiler arguments belong to the regular
compilerOptions {}block. The-Xexport-kdocargument isn't needed, as KDoc comments are exported by default.
Remove the tooling related to Objective-C
Most tools that work around the limitations of Objective-C interop have an equivalent in Swift export:
- CocoaPods plugin
Remove the
kotlin("native.cocoapods")plugin and thecocoapods {}block, as well as the pod from thePodfile. Swift export doesn't support CocoaPods, so move the project to a local Swift package as a separate step before the migration, then use the Swift package integration.- SKIE
Remove the plugin and the
skie {}block. Swift export covers the main SKIE features natively: sealed hierarchies become Swiftenumtypes with thesealedType()function, suspending functions becomeasyncfunctions, and flows becomeAsyncSequence.- KMP-NativeCoroutines
Keep the Gradle plugin, the KSP processor, and its annotations in their Swift export compatibility mode during the migration. This keeps the existing Swift call sites working and produces a much smaller diff. You can clean them up later once the call sites use the generated Swift export API directly.
- XCFramework and Swift package distribution
Replace custom XCFramework publishing, KMMBridge, or the
multiplatform-swiftpackageplugin with theswiftPackageIntegration()function in theswift {}block, which is the built-in Swift package integration.- Leftover Gradle properties
Remove properties such as
kotlin.native.cocoapods.*and SKIE-specific ones.
Coroutine support in Swift export is enabled automatically when kotlinx.coroutines is in the dependency graph.
Update the Xcode project
Swift export supports two integration types, and the Xcode changes depend on the one you choose:
The direct integration keeps a Run Script build phase that calls Gradle on every build.
The Swift package integration produces a local Swift package that the Xcode project consumes as a package dependency.
Choose Swift export integration type based on how your Xcode project consumes the Kotlin code now:
- Direct integration
Stay with the direct integration. It's the closest equivalent of your current setup, so the migration only affects the run script phase and the build settings.
- CocoaPods
CocoaPods are deprecated and not supported by Swift export. First, move the project from CocoaPods to a local Swift package, and only then migrate to Swift export. Don't do both at the same time: each step changes a different part of the project.
Remove the pod from the `Podfile` after the migration.
- XCFramework
First, wrap your XCFramework in a Swift package to establish the remote integration and then switch to Swift export. You can also do both at once, as the Swift package generation built into Swift export replaces your custom packaging.
Remove custom XCFramework dependency from the Xcode project after the migration.
Migrate to the direct integration
For every application or extension target that linked the Kotlin framework:
On the Build Phases tab, replace the
embedAndSignAppleFrameworkForXcodetask in the Run Script phase with theembedSwiftExportForXcodetask:cd "$SRCROOT/.." ./gradlew :shared:embedSwiftExportForXcodeThe phase must still run before the Compile Sources phase.
Update the build settings in both the
DebugandReleaseconfigurations:Setting
Value
IPHONEOS_DEPLOYMENT_TARGET18.0or later. For other platforms, use the corresponding setting:15.0for macOS,18.0for tvOS, and11.0for watchOS. Bump theplatformsoption in localPackage.swiftfiles as well.ENABLE_USER_SCRIPT_SANDBOXINGNOOTHER_LDFLAGSRemove the
-framework <baseName>flag. The generated library is autolinked. Add system libraries only if linking fails, for example-lsqlite3.FRAMEWORK_SEARCH_PATHSRemove the path to the
build/xcode-frameworksdirectory of your Kotlin module.Remove the SKIE Swift package once nothing in the project uses it. You can keep the KMP-NativeCoroutines package while the project uses its Swift export compatibility mode and remove it later only after the corresponding cleanup.
The build output lands in the BUILT_PRODUCTS_DIR directory: the lib<Module>.a static library, one .swiftmodule file per exported module, and the KotlinRuntime, KotlinRuntimeSupport, and KotlinCoroutineSupport modules.
Migrate to the Swift package integration
Swift export can produce a local Swift package instead of relying on a build phase. Add the swiftPackageIntegration() call to the swift {} block:
Then update the Xcode project:
Generate the package with the export task for the build type you need:
./gradlew :shared:exportReleaseSwiftPackageUnlike the direct integration, this task doesn't need the Xcode environment, so you can run it from the command line or from CI. Re-run it after every change in the Kotlin code.
In Xcode, remove the Run Script phase and the pod or XCFramework dependency, and then select File | Add Package Dependencies | Add Local and choose the build type subdirectory of the output directory, such as
SwiftPackage/Debug. Link the<Module>Libraryproduct to the application target.If the app already has a
Package.swiftmanifest, declare the dependency there instead:dependencies: [ .package(name: "Shared", path: "../shared/SwiftPackage/Release") ], targets: [ .target(name: "App", dependencies: [.product(name: "SharedLibrary", package: "Shared")]) ]Update the deployment target and
OTHER_LDFLAGSbuild settings as described for the direct integration.Remove the SKIE Swift package once nothing in the project uses it. You can keep the KMP-NativeCoroutines package while the project uses its Swift export compatibility mode and remove it later only after the corresponding cleanup.
The two integrations are configured independently, so if you keep both, repeat the per-dependency configure() overrides in the swiftPackageIntegration {} block as well.
Prepare the Kotlin API
Each exported Gradle project can define its own export.swift {} block to set moduleName and rootPackage in the Gradle build file. Use them to name exported modules; otherwise Swift export derives a long module name from the Gradle coordinates.
Swift export exports every public declaration of the exported modules, plus the parts of dependencies reachable from them. Direct api dependencies are exported in full. For common libraries such as kotlinx.coroutines, SQLDelight, Koin, and kotlinx.serialization, a full export often fails to build or is very slow, so don't make them direct api dependencies of the module consumed by Swift. Start with a thin umbrella module that exposes only the app-facing API and keeps library types out of its public signatures.
Like Objective-C interop, Swift export doesn't prune declarations that Swift never references. A large public API surface leads to slow builds, a lot of generated code, and a confusing Swift API, so it's worth shrinking it:
Make the code that Swift never calls
internal: dependency injection modules, HTTP clients, repository implementations, mappers, and utility extensions.Consider enabling
explicitApi()in exported modules, so that every public declaration is a deliberate choice and has an explicit return type.Consider keeping third-party library types out of public signatures. Expose an interface with a top-level factory function and keep the implementation
internal, useinternalconstructors, and returnUnitor your own types from Swift-facing entry points.Keep the umbrella module thin: prefer
implementationfor common libraries and expose your own interfaces, factory functions, data types, andStateFloworFlowproperties instead of library types. If the app needs a dependency type, import that generated dependency module explicitly in Swift.Mark dependencies that are only used internally as
implementation, notapi. If their types still leak into the exported public API, hide the dependency withxcodeIntegration { configure(libs.example, SwiftExportVisibility.HIDDEN) }. Hidden modules are translated as shallow declarations, so they neither break the build nor pollute the Swift API.
Use Kotlin classes from Swift
Final Kotlin classes are generated as final Swift classes; open and abstract classes are also supported: they are generated as Swift open classes and can be subclassed in Swift. A Swift class that implements a Kotlin interface or subclasses a Kotlin class must inherit from KotlinBase, directly or through the generated Kotlin class. Because the generated <Module>.swift file imports KotlinRuntime without @_exported, add an explicit import KotlinRuntime to every Swift file that names KotlinBase directly.
Override every open Kotlin member that Swift should customize. In particular, missing overrides of abstract members can compile successfully but fail at runtime, so exercise every Swift subclass during verification.
Adjust unsupported constructs
Some Kotlin constructs don't translate well, and the declarations using them are either missing from the generated Swift code or break the build. Update your Kotlin code:
Kotlin pattern | Problem | What to do |
|---|---|---|
| Mutable collection types are not supported | Use read-only |
Custom types that inherit from | The generated bridge compiles, but Swift cannot instantiate the type | Expose a wrapper type or use a read-only collection supertype |
Parameters typed with a generic class with a concrete type argument, like | The function isn't exported | Take the upper bound or a star projection, or introduce a non-generic wrapper type |
Generic classes, interfaces, and sealed hierarchies in the Swift-facing API | Type arguments are erased, so Swift code is full of casts | Expose non-generic wrappers mapped in Kotlin, such as a sealed |
Default argument values | Not exported | Add explicit overloads or secondary constructors for the Swift call shapes |
| Not exported | Add a variant with a |
| Not visible in Swift, including Swift subclasses | Make them public or add a public accessor |
Declarations named like members of NSObject, such as | Dropped from the export because Swift resolves them to inherited | Rename them in Kotlin or use |
Declarations named like Swift and SwiftUI types, such as | Ambiguous in Swift files that also import SwiftUI or Foundation | Rename them in Kotlin, or qualify the name in Swift with the module name |
Exceptions thrown from non-suspending functions | Terminate the app when they reach Swift | Annotate the function with |
You can also drop the workarounds that existed only because of Objective-C interop:
@Throwsfunctions that forced a type into the generated header.@ObjCNameannotations that only fixed Objective-C name mangling.Renames that avoided the
doInitprefix.
@HiddenFromObjC still hides declarations from Swift export, and @ObjCName(swiftName = "...") still renames them.
Update the Swift code
Build the project and then work through the compiler errors file by file. Apply the mechanical changes first, such as imports, file facades, and boxed types, and then the semantic ones, such as coroutines and sealed hierarchies.
Names and namespaces
Objective-C interop | Swift export |
|---|---|
|
|
|
|
|
|
|
|
| The original Kotlin names: |
|
|
|
|
Types and functions
Objective-C interop | Swift export |
|---|---|
|
|
|
|
| Plain Swift values |
|
|
| The generated |
|
|
|
|
|
|
|
|
Coroutines
Suspending functions no longer have completion handler variants. The generated Swift export API supports them directly, without third-party enhancers. If you keep KMP-NativeCoroutines in its Swift export compatibility mode, existing KMP-NativeCoroutines call sites can continue to work during migration. Migrate them to the direct form incrementally:
Flows become AsyncSequence instances with a typed element, and the current value of a StateFlow is available through the value property. As with suspending functions, migrate KMP-NativeCoroutines flow call sites to these direct forms incrementally:
Sealed hierarchies
Replace the chains of as? casts and the SKIE onEnum(of:) function with the sealedType() function, which returns a Swift enum and allows exhaustive switch statements:
Note the value property: each enum case wraps the corresponding Kotlin instance.
Generic sealed interfaces don't have the sealedType() function. Either keep the casts against the nested Swift names or expose a non-generic hierarchy from Kotlin.
Verify the migration
After you've completed the migration steps, check that everything works as expected. Some things are visible only at runtime, so compiling the project isn't enough. To verify the migration:
Build the project through Xcode or
xcodebuild, and review the warnings about casts that always fail or have no effect, as they usually indicate a runtime crash. With the Swift package integration, re-run theexport<BuildType>SwiftPackagetask first, so that the package reflects the current Kotlin code.Run the application in the simulator and go through every screen that touches Kotlin code. Look for missing overrides of abstract members, failing
as!casts, and undeclared Kotlin exceptions.Build the other Apple targets that consumed the framework, as well as the Android and JVM targets. The visibility changes in the Kotlin code affect them as well.
Troubleshooting
Problem | Solution |
|---|---|
| Add |
The build fails because the | Remove the deprecated |
| Raise the deployment target of the Xcode target. |
Two exported modules resolve to the same Swift module name | Rename one of them with |
The compilation of the generated Kotlin bridges fails in your own module | A declaration uses an unsupported construct. Adjust the Kotlin signature or hide the declaration. Help us fix such cases by reporting them to YouTrack. |
The build fails inside the generated code of a dependency | Hide the dependency with |
A declaration is missing from the generated Swift code or is marked as unavailable | Check the Kotlin signature against the limitations and adjust it. |
A function of your module fails at runtime with a fatal error | The declaration mentions a type from a hidden dependency. Replace it with a wrapper that doesn't expose that type. |
The Swift export worker runs out of memory | Shrink the exported API surface, or raise the memory limit with the |
The Swift package export fails because the Apple targets export different module sets | Align the exported modules across all Apple targets, as a Swift package has a single module set. |