Bridge the Native API
The bridge is a folder of Swift files that acts as the glue between your Swift/Apple SDK code and Kotlin. The plugin compiles these files and exposes them to Kotlin via cinterop.
How It Works
When syncing the project, the plugin creates the bridge folder at:
src/swift/[cinteropName]— whencinteropNameis setsrc/swift/[targetName]— otherwise (e.g.src/swift/iosArm64)
Everything inside this folder is copied to the build directory and compiled into the bridge.
Getting Started
When the bridge folder is empty, the plugin adds a StartYourBridgeHere.swift template with examples to help you get started.
To disable this behavior, add spmforkmp.disableStartupFile=true to your gradle.properties.
Gradle Configuration
A minimal bridge setup — no external dependencies, just the Apple native SDK:
kotlin {
listOf(
iosArm64(),
iosSimulatorArm64()
// and more Apple targets...
).forEach { target ->
target.swiftPackageConfig(cinteropName = "[cinteropName]") {
}
}
}
Writing Your Bridge
Objective-C Compatibility Required
Swift code must be annotated with @objc / @objcMembers and declared public to be visible in Kotlin. Pure Swift types are not bridgeable.
See the playground for practical interoperability examples.
import UIKit
@objcMembers public class MySwiftBridge: NSObject {
public func exportedMethod() -> String {
return "value"
}
public func exportedView() -> NSObject {
return UIView()
}
}
import [cinteropName].MySwiftBridge
val contentFromSwift = MySwiftBridge().exportedMethod()
val aView = MySwiftBridge().exportedView() as UIView
Bundle Resources (since v1.9.0)
To bundle resources (images, fonts, data files, etc.) alongside your bridge:
- Choose a resource type and create the corresponding folder:
| Type | Folder Name | Documentation |
|---|---|---|
| Process (Recommended) | Resources-process |
Process |
| Copy | Resources-copy |
Copy |
| Embed | Resources-embed |
Embed |
- Place your files in the chosen folder (e.g.
bridgeString.txt) - Access them from Swift using
Bundle.module: