Kotlin Multiplatform (KMP) is a powerful tool for sharing common code across platforms, including iOS. But what if you need to call iOS-specific Swift functions directly from Kotlin? this is now possible! Thanks to the Swift Klib Plugin by ttypic to make the process easier, In this blog, we’ll explore how to set up and use this plugin to access Swift functions directly in Kotlin via CInterop, enabling seamless integration between Swift and Kotlin.
Why Call Swift Functions from Kotlin?
Imagine you have a specific iOS feature or library written in Swift that you want to leverage in your shared Kotlin code. With the Swift Klib Plugin, you can:
- Reuse iOS-specific functionality without reinventing the wheel.
- Optimize development by keeping platform-specific code in Swift but accessible in Kotlin.
- Keep code clean by centralizing your Swift and Kotlin interoperation in one place.
Let’s dive into the setup and implementation.
A Swift library can be used in Kotlin code if its API is exported to Objective-C with @objc. Pure Swift modules are not yet supported.Official Link : https://kotlinlang.org/docs/native-objc-interop.html
Step 1: Set Up Your Kotlin Multiplatform Project (Skip if you already have!)
If you’re new to KMP, start by creating a basic Kotlin Multiplatform project in Android Studio. Here’s a quick rundown:
- Open Android Studio and select File > New > New Project.
- Choose Kotlin Multiplatform Application as your project template.
This will create a standard KMP project with Android and iOS targets.
Make sure use kotlin version higher than "2.0.21"
Step 2: Add the Swift Klib Plugin to Your Project
To access Swift functions in Kotlin, integrate the Swift Klib Plugin. Open
your build.gradle.kts
file at the root of the project and add
the plugin as follows:
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidLibrary)
id("io.github.ttypic.swiftklib") version "0.6.4" //add this
}
Add this to the project-level gradle.properties file (if it’s not already included).
#Kotlin Multiplatform
kotlin.mpp.enableCInteropCommonization=true
Step 3: Write Your Swift Code for CInterop
Create a Swift file in the iOS module (iosApp/iosApp/kint
)
example
DeviceInfo.swift
:
// iosApp/iosApp/kint/DeviceInfo.swift
import UIKit
@objc public class DeviceInfo: NSObject {
//example code
@objc public static func getCurrentOSVersion() -> String {
return UIDevice.current.systemVersion
}
}
Step 4: Configure the Swift Klib Plugin
In your build.gradle.kts
, configure the plugin to include
your Swift source files and prepare them for Kotlin’s
cinterop
:
kotlin {
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64(),
).forEach {
it.compilations {
val main by getting {
cinterops {
create("DeviceInfoSwift")
}
}
}
}
//...
}
swiftklib {
create("DeviceInfoSwift") {
path = file("../iosApp/iosApp/kint")
packageName("com.example.swiftkotlininterop")
}
}
1. Task Name : "DeviceInfoSwift". make sure it is match in both creates inside "cintrops" and "swiftklib" above.
2. Path : specify the Folder path of our Swift file.
3. Package Name : Give any package/bundle name you want. It will be used while import in kotlin file.
Step 5: Access Swift Code in Kotlin
After configuring the plugin, Sync the project and then you can now access the Swift class and function directly in Kotlin:
// src/iosMain/kotlin/DeviceInfoAccessor.kt
fun fetchOSVersion(): String {
return DeviceInfo.getCurrentOSVersion() // Calling Swift function from Kotlin
}
Step 6: Build and Run the Project
- Run the following command in your project root to generate the Swift interop bindings and build the project:
./gradlew build
2. Open the project in Xcode and run the iOS app to see the output of
fetchOSVersion()
displaying the current iOS version.
Conclusion
The Swift Klib Plugin is a game-changer for Kotlin Multiplatform projects, bridging the gap between iOS and Kotlin. By using it, you can create versatile, cross-platform apps with shared logic and platform-specific code that’s easier to manage and maintain. Give it a try in your KMP project—Swift and Kotlin just became best friends! 🥂
Happy Coding!
Post a Comment