Set up Swift export in your project
To set up Swift export, use the export { swift {} } DSL of your build file. It describes how the module is exported to Swift, for example a Swift module name and a collapse rule. Opt in to the DSL with @OptIn(ExperimentalSwiftExportDsl::class).
You can connect the Swift export output to Xcode in two ways:
With direct Xcode integration, Gradle runs as a build phase of the Xcode build and puts the generated Swift modules into the app's build directory.
With Swift package generation, Gradle produces a self-contained Swift package that you add to an Xcode project or another Swift package as a dependency, without a separate build phase in Xcode.
Direct integration
The direct integration approach that connects the iOS framework to the Xcode project is a standard configuration for Kotlin Multiplatform projects created with the Kotlin Multiplatform plugin in IntelliJ IDEA or through the web wizard.
Configure Kotlin project
For direct integration with Xcode, configure your shared module's build.gradle.kts file. It must have the xcodeIntegration() call to enable Xcode as a consumer of Swift export and set up the integration by:
Registering the
embedSwiftExportForXcodetask that Xcode runs in the Run Script build phase.Adding Gradle configurations and tasks that resolve the export graph, generate the Swift code for the module and its dependencies, and compile it for Xcode.
The declared dependencies of an exported module are exported automatically:
Direct
apidependencies are exported fully, meaning that their entire public API surface is available in Swift.Direct
implementationdependencies and transitive dependencies are exported transitively, meaning that only the API that leaks into the public API of the exported module is available in Swift.
You can use the following build files in your project as a starting point for setting up Swift export:
The direct integration automatically generates all the necessary files, including swiftmodule files, static .a library, a header file, and a modulemap file, and copies them into the Xcode's build directory.
You can override the default moduleName and rootPackage values of an exported dependency in two ways:
In the
export { swift {} }block of the dependency's own build file.In the build file of the module that consumes the dependency, with the
configure()function inside thexcodeIntegration {}block. The function accepts project dependencies, external dependencies, and version catalog accessors.
If several values are present, Swift export applies the first available value in this order:
The value set by the consuming module in the
xcodeIntegration {}block.The value declared by the dependency in its own build file.
The default value derived from the dependency's coordinates.
The configuration declared by a dependency is publishable: if a library author supplies option overrides for the library, Gradle automatically consumes and applies them during Swift export. The overrides declared in the xcodeIntegration {} block apply to the local build only and are never published.
Configure Xcode project
To configure Xcode to integrate Swift export into your project:
In Xcode, open the project settings.
On the Build Phases tab, locate the Run Script phase with the
embedAndSignAppleFrameworkForXcodetask.Replace the script with the
embedSwiftExportForXcodetask in the run script phase. Using the example above:./gradlew :shared:embedSwiftExportForXcode
Build the project.
The build generates Swift modules in the output directory.
Swift package generation
As an alternative to direct integration, Swift Export can generate a Swift package containing Swift API sources, bridge and runtime headers, and an XCFramework with compiled Kotlin libraries.
In this case, you can simply consume the Swift package in the Xcode project. Xcode compiles the Swift sources and no Gradle build phase is needed. Generate the package on macOS with Xcode installed, and set your iOS app's deployment target to iOS 18.0 or later.
In your shared module, enable swiftPackageIntegration instead of xcodeIntegration used for direct integration. If switching from direct integration, also remove the embedSwiftExportForXcode run script from Xcode:
outputDirectory is optional and defaults to SwiftPackage beside the module's build script. The package includes the configured Apple targets, in this example, iOS devices and simulators. To customize dependency exports, place configure() calls inside swiftPackageIntegration, as in the direct-integration example.
An example sequence:
Declare a function in
shared/src/commonMain/kotlin/Greeting.kt:package com.example.sandbox fun greeting(): String = "Hello from Kotlin"Run the export task for the Kotlin binary configuration you need:
# Generates shared/SwiftPackage/Debug ./gradlew :shared:exportDebugSwiftPackage # Generates shared/SwiftPackage/Release ./gradlew :shared:exportReleaseSwiftPackageIn Xcode, choose File | Add Package Dependencies | Add Local and select
shared/SwiftPackage/Debugorshared/SwiftPackage/Release(not their parent directory).Add the SharedLibrary product to your app target. Its name is the configured
moduleNamefollowed byLibrary. The SwiftimportusesSharedalone.Import the module and call the exported function from Swift. The package is specified in
rootPackage, so you can callgreeting()directly:import Shared print(greeting())
When changing Kotlin code or export configuration, rerun the export task, then rebuild your app. Switching Xcode's build configuration does not switch the package directory: a dependency on Debug still uses the Debug Kotlin binary. Select the Release package when you want the release binary.
Known issues with Swift package generation:
Dependencies imported through
swiftPMDependenciesare not supported currently.If SwiftUI previews crash with the generated package, try Editor | Canvas | Use Legacy Previews Execution or make the package product dynamic (see KT-91412).
Current limitations
Known issues general to Swift export:
The Swift export DSL has no API for passing compiler arguments, including arguments for link tasks. Set the options you need at the module level in the
compilerOptions {}block instead. If your project requires passing compiler arguments through the Swift export DSL, describe your use case in YouTrack.When using declarations that require opt-in, add an explicit
optIncompiler option to the Gradle build file in the exporting module. For example, for thekotlinx.datetimelibrary:export { swift { moduleName = "Shared" xcodeIntegration { configure("org.jetbrains.kotlinx:kotlinx-datetime:0.8.0") { moduleName = "KotlinDateTime" rootPackage = "kotlinx.datetime" } } } } // Add a separate opt-in block at the module level compilerOptions { optIn.add("kotlin.time.ExperimentalTime") }