Kotlin SwiftImport

SwiftImport Migration in Kotlin Multiplatform (KMP): Import Swift Packages Directly into Kotlin 🚀

One of the long-standing wishes in the Kotlin Multiplatform (KMP) ecosystem has been the ability to directly use Swift Package Manager (SPM) libraries from Kotlin. Until recently, developers often had to create wrappers in Swift or rely on CocoaPods integrations.

With the introduction of Swift Package import support, this workflow becomes significantly easier.

In this article, I’ll share my experience migrating to this new feature and the challenges I encountered while integrating a real-world Swift library.


Why SwiftImport is Exciting for KMP Developers

The new Swift import support allows Kotlin code to directly access libraries distributed through Swift Package Manager.

Official documentation:
https://kotlinlang.org/docs/multiplatform/multiplatform-spm-import.html#configure-build-file

This is huge because it eliminates the need to:

  • Write Swift wrappers
  • Maintain bridging layers
  • Depend on CocoaPods for every Swift dependency

Instead, we can consume Swift libraries directly in Kotlin.’


My Real Use Case

I’m currently migrating my Digital Diary app to Kotlin Multiplatform.

You can download the Android version here:
https://play.google.com/store/apps/details?id=com.bob.offlinediary

For the iOS version, I wanted to integrate the Swift library:

ZipArchive
https://github.com/ZipArchive/ZipArchive

This library is extremely useful for handling zip compression and extraction using secured password, which I use for backup and restore features in the diary app.

Previously, integrating this library from Kotlin was complicated.

With SwiftImport, the process becomes much simpler.


Following the Official Sample

To integrate the Swift package, I followed the official sample project provided by JetBrains:

https://github.com/Kotlin/kmp-with-cocoapods-compose-sample/tree/spm_import

The setup process involved:

  1. Configuring the Swift package import in the Gradle build file
  2. Syncing the project
  3. Letting the Kotlin build system generate the required Swift package files

During this process, Kotlin generates a file at:

build/kotlin/swiftImport/Package.swift

This file defines the Swift package configuration required for the import.

The Issue I Encountered

While building the project using Xcode 15.2 on macOS 13, I encountered a compilation error.

After investigating, I discovered that the generated file:

build/kotlin/swiftImport/Package.swift

contained syntax issues, specifically:

  • Incorrect commas in the package definition
  • Errors in the exact and package lines

These syntax issues caused Swift Package Manager to fail during compilation.

From my observation, this appears to be an issue in the Kotlin compiler’s generated Swift package configuration.

/Users/apple/AndroidStudioProjects/digitaldiary/ZipArchive/build/kotlin/swiftImport/Package.swift:19:5: error: unexpected ',' separator
    )
    ^
/Users/apple/AndroidStudioProjects/digitaldiary/ZipArchive/build/kotlin/swiftImport/Package.swift:28:9: error: unexpected ',' separator
        )
        ^

2026-03-19 06:11:40.646 xcodebuild[56271:370717] [MT] DVTAssertions: Warning in IDEFrameworks/IDEFoundation/Execution/RunContextManager/IDERunContextManager.m:848
Details:  Error deleting scheme: Cannot modify data because the process disallows saving.
Object:   <IDERunContextManager: 0x600002c3fba0>
Method:   -deleteRunContexts:completionQueue:completionBlock:
Thread:   <_NSMainThread: 0x600000b043c0>{number = 1, name = main}
Pl

Temporary Workaround

To fix the issue, I manually corrected the generated Package.swift file.

However, there was another problem.

Every time I ran Gradle sync, the file was automatically regenerated, overwriting my fixes.

To work around this, I created a custom Gradle task that restores the corrected Package.swift file whenever the build runs.

// build.gradle.kts

kotlin {
      // Post-processing to fix generated Package.swift syntax errors
    tasks.configureEach {
        if (name.contains("fetchSyntheticImportProjectPackages", ignoreCase = true) ||
            name.contains("generateSyntheticLinkageSwiftPMImportProject", ignoreCase = true)) {
    
            doLast {
                val manifest = project.layout.buildDirectory.file("kotlin/swiftImport/Package.swift").get().asFile
                if (manifest.exists()) {
                    val content = manifest.readText()
                    // Fix for trailing comma before closing parenthesis or bracket
                    val fixedContent = content
                        .replace(",\\s*\\)".toRegex(), "\n    )")
                        .replace(",\\s*\\]".toRegex(), "\n    ]")
    
                    if (content != fixedContent) {
                        manifest.writeText(fixedContent)
                        logger.lifecycle("Successfully fixed trailing commas in: ${manifest.absolutePath}")
                    }
                }
            }
        }
    }
}

This approach allows the project to compile successfully while still using the Swift package import feature.

Recommendation for JetBrains

This feature is extremely promising and opens up powerful possibilities for Kotlin Multiplatform developers.

However, the generated Package.swift file needs to be corrected in the Kotlin compiler to avoid these syntax issues.

Hopefully, JetBrains will address this in an upcoming release so developers can use Swift Package imports seamlessly without manual fixes.

Final Thoughts

Despite the issue, I’m very excited about this new capability.

Being able to directly access third-party Swift libraries from Kotlin will dramatically improve the developer experience when building Kotlin Multiplatform applications for iOS.

Once this feature becomes stable, it will remove a lot of interoperability friction between Kotlin and Swift.

For developers migrating existing apps to Kotlin Multiplatform, this is a feature worth exploring.


Leave a Comment

Your email address will not be published. Required fields are marked *