Skip to content

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] — when cinteropName is set
  • src/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:

build.gradle.kts
kotlin {
    listOf(
        iosArm64(),
        iosSimulatorArm64()
        // and more Apple targets...
    ).forEach { target ->
        target.swiftPackageConfig(cinteropName = "[cinteropName]") {
        }
    }
}
Legacy (< 1.1.0)
build.gradle.kts
swiftPackageConfig {
    create("[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.

src/swift/[cinteropName]/mySwiftFile.swift
import UIKit

@objcMembers public class MySwiftBridge: NSObject {
    public func exportedMethod() -> String {
        return "value"
    }
    public func exportedView() -> NSObject {
        return UIView()
    }
}
iosMain/kotlin/com/example/myKotlinFile.kt
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:

  1. 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
  1. Place your files in the chosen folder (e.g. bridgeString.txt)
  2. Access them from Swift using Bundle.module:
let url = Bundle.module.url(forResource: "bridgeString", withExtension: "txt")