Kotlin Help

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:

  1. Switch the Gradle build from binaries.framework {} to the export { swift {} } block.

  2. Switch the Xcode project to the direct integration or the Swift package integration.

  3. Prepare the exported Kotlin API to control which public declarations and dependencies Swift export exposes.

  4. 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 swiftExport {} DSL.

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 ENABLE_USER_SCRIPT_SANDBOXING set to NO.

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 in export().

  • 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, KotlinInt and other boxed types, companion, doCopy(), completion handlers, and SKIE helpers such as onEnum(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:

kotlin { listOf(iosArm64(), iosSimulatorArm64()).forEach { it.binaries.framework { baseName = "Shared" isStatic = true export(project(":models")) } } }
kotlin { iosArm64() iosSimulatorArm64() export { swift { // Keeps the old framework name for this module; exported dependencies have their own Swift module names moduleName = "Shared" // Removes the package prefix from the generated Swift code rootPackage = "com.example.shared" // Enables the Xcode integration for this module xcodeIntegration { // Optional: overrides the options of an exported dependency configure(project(":models")) { moduleName = "Models" rootPackage = "com.example.models" } } } } sourceSets { commonMain.dependencies { // Direct api dependencies are exported fully api(project(":models")) } } }

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. The baseName, isStatic, export(), transitiveExport, and linkerOpts options have no effect on Swift export.

  • There's no export() list anymore. Direct api dependencies are exported fully, while implementation and transitive dependencies are exported only as far as they are reachable from the exported public API. Use the configure() function inside the xcodeIntegration {} or swiftPackageIntegration {} block to override the options of a dependency or to exclude it with SwiftExportVisibility.HIDDEN.

  • The umbrella module must declare the integration it uses. xcodeIntegration() registers the embedSwiftExportForXcode task for the direct integration, and swiftPackageIntegration() registers the export<BuildType>SwiftPackage tasks 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 deprecated swiftExport {} block, as it's a build error. rootPackage corresponds to flattenPackage used in the old DSL.

  • Compiler arguments belong to the regular compilerOptions {} block. The -Xexport-kdoc argument isn't needed, as KDoc comments are exported by default.

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 the cocoapods {} block, as well as the pod from the Podfile. 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 Swift enum types with the sealedType() function, suspending functions become async functions, and flows become AsyncSequence.

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-swiftpackage plugin with the swiftPackageIntegration() function in the swift {} 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:

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:

  1. On the Build Phases tab, replace the embedAndSignAppleFrameworkForXcode task in the Run Script phase with the embedSwiftExportForXcode task:

    cd "$SRCROOT/.." ./gradlew :shared:embedSwiftExportForXcode

    The phase must still run before the Compile Sources phase.

  2. Update the build settings in both the Debug and Release configurations:

    Setting

    Value

    IPHONEOS_DEPLOYMENT_TARGET

    18.0 or later. For other platforms, use the corresponding setting: 15.0 for macOS, 18.0 for tvOS, and 11.0 for watchOS. Bump the platforms option in local Package.swift files as well.

    ENABLE_USER_SCRIPT_SANDBOXING

    NO

    OTHER_LDFLAGS

    Remove the -framework <baseName> flag. The generated library is autolinked. Add system libraries only if linking fails, for example -lsqlite3.

    FRAMEWORK_SEARCH_PATHS

    Remove the path to the build/xcode-frameworks directory of your Kotlin module.

  3. 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:

export { swift { moduleName = "Shared" swiftPackageIntegration { // Optional: the default is the SwiftPackage directory of the module outputDirectory = layout.projectDirectory.dir("SwiftPackage") } } }

Then update the Xcode project:

  1. Generate the package with the export task for the build type you need:

    ./gradlew :shared:exportReleaseSwiftPackage

    Unlike 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.

  2. 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>Library product to the application target.

    If the app already has a Package.swift manifest, declare the dependency there instead:

    dependencies: [ .package(name: "Shared", path: "../shared/SwiftPackage/Release") ], targets: [ .target(name: "App", dependencies: [.product(name: "SharedLibrary", package: "Shared")]) ]
  3. Update the deployment target and OTHER_LDFLAGS build settings as described for the direct integration.

  4. 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, use internal constructors, and return Unit or your own types from Swift-facing entry points.

  • Keep the umbrella module thin: prefer implementation for common libraries and expose your own interfaces, factory functions, data types, and StateFlow or Flow properties 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, not api. If their types still leak into the exported public API, hide the dependency with xcodeIntegration { 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

MutableList, MutableSet, and MutableMap parameters

Mutable collection types are not supported

Use read-only List, Set, and Map parameters

Custom types that inherit from List, Set, or Map

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 Box<String>

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 ProfileState hierarchy or a ProfileRepository interface

Default argument values

Not exported

Add explicit overloads or secondary constructors for the Swift call shapes

inline fun with a reified type parameter

Not exported

Add a variant with a KClass parameter

protected members

Not visible in Swift, including Swift subclasses

Make them public or add a public accessor

Declarations named like members of NSObject, such as hash or description

Dropped from the export because Swift resolves them to inherited NSObject members (KT-90497)

Rename them in Kotlin or use @ObjCName(swiftName = "...")

Declarations named like Swift and SwiftUI types, such as Shape, Color, or Result

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 @Throws

You can also drop the workarounds that existed only because of Objective-C interop:

  • @Throws functions that forced a type into the generated header.

  • @ObjCName annotations that only fixed Objective-C name mangling.

  • Renames that avoided the doInit prefix.

@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

import shared

import Shared for declarations from the umbrella module. Each exported Gradle module has its own Swift module name, so import the module that owns the declaration you use. Add import KotlinRuntime to files that use KotlinBase. Runtime modules are imported by the generated module but are not re-exported to your Swift files

Item for any package

Item only for declarations in rootPackage; otherwise packageName.Declaration or ExportedKotlinPackages.packageName.Declaration

PlatformKt.platformName()

platformName() in the root package, or packageName.platformName() otherwise

UiStateSuccess

UiState.Success, as nested types stay nested

doInit(), doCopy(), doSomething()

The original Kotlin names: init(), copy(), something()

MyObject()

MyObject.shared

Foo.companion.bar

Foo.Companion.shared.bar

Types and functions

Objective-C interop

Swift export

KotlinInt(value: 1), [KotlinInt], KotlinBoolean(bool: b)

Int32(1), [Int32], b. Optional values are passed as is, for example as Int32?

KotlinUnit(), KotlinNothing

(), Never

NSString, NSNumber, NSArray, and NSDictionary wrappers

Plain Swift values

MyEnum.entryOne, .name, .ordinal, values()

MyEnum.ENTRY_ONE with the exact Kotlin name, .description, .rawValue, allCases

Box<NSString>(value:), Repository<User>

The generated Box(value:) and Repository types are non-generic, with an as! cast when reading erased values

f(param_: 2.0) for overloads

f(param: 2.0 as Float) with the real parameter label

a.plus(other: b), a.get(index: i)

a + b, a[i]

receiver.extensionFunction() for any package

receiver.extensionFunction() for root-package extensions, pkg.extensionFunction(receiver) otherwise

catch let error as NSError

catch let error as MyKotlinException, because kotlin.Throwable conforms to Swift.Error

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:

// Before, with the Objective-C completion handler repository.loadItem(id: 1) { item, error in /* ... */ } // Before, with KMP-NativeCoroutines let item = try await asyncFunction(for: repository.loadItem(id: 1)) // After Task { do { let item = try await repository.loadItem(id: 1) } catch is CancellationError { // The Swift task was cancelled, which also cancels the coroutine } catch { print(error) } }

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:

// Before, with the Objective-C collector try await repository.numbers().collect(collector: NumberCollector()) // Before, with SKIE for await value in repository.numbers() { print(value) } // After for try await value in repository.numbers().asAsyncSequence() { print(value) } let current = viewModel.state.value

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:

// Before if let success = state as? UiStateSuccess { // ... } else if let error = state as? UiStateError { // ... } // After switch state.sealedType() { case .success(let success): print(success.value.items) case .error(let error): print(error.value.message) case .loading: break }

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:

  1. 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 the export<BuildType>SwiftPackage task first, so that the package reflects the current Kotlin code.

  2. 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.

  3. 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

Task 'embedSwiftExportForXcode' not found

Add xcodeIntegration() to the export { swift {} } block of the module.

The build fails because the swiftExport {} and export { swift {} } blocks conflict

Remove the deprecated swiftExport {} block.

Swift export needs IPHONEOS_DEPLOYMENT_TARGET to be 18.0 or newer

Raise the deployment target of the Xcode target.

Two exported modules resolve to the same Swift module name

Rename one of them with configure(dependency) { moduleName = "..." }.

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 configure(dependency, SwiftExportVisibility.HIDDEN) and expose only the wrappers your Swift code needs from your own module.

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 kotlin.native.swiftExportWorkerJvmArgs=-Xmx4g Gradle property. The default worker heap is -Xmx1g.

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.

What's next

02 December 2026