Swift Code Bundling
SKIE can bundle manually written Swift code directly into the generated Kotlin framework similar to how it bundles the generated Swift code.
This feature's primary intended use case is to allow KMP developers to write custom Swift wrappers for their Kotlin API. These wrappers allow you to better work around the remaining limitations of the Kotlin/Swift interop that SKIE currently does not solve automatically.
The main advantage of bundling the wrappers directly into the Kotlin framework is that you can keep the entire API in a single Framework. Having only a single Framework simplifies the distribution process and makes using and maintaining the Kotlin API easier.
The distribution process is simplified because you need to distribute only a single Kotlin framework (instead of distributing two or modifying the code in the Swift repository). Also, the bundled Swift code is easier to keep in sync with the Kotlin code because it is compiled together with the Kotlin code.
How to use
In order to bundle Swift code into the Kotlin framework, you need to put it in one of the swift
source sets created by SKIE.
SKIE derives the location of these source sets based on the kotlin
source sets in the following way:
src/commonMain/kotlin
->src/commonMain/swift
src/iosArm64Main/kotlin
->src/iosArm64Main/swift
src/macosArm64Main/kotlin
->src/macosArm64Main/swift
src/${kotlinSourceSetName}/kotlin
->src/${kotlinSourceSetName}/swift
The swift source sets are created only in the module where the SKIE plugin is applied.
The source sets follow the hierarchy of the kotlin source sets.
So, for example iosArm64Main
target will contain the Swift code from the src/iosArm64Main/swift
, src/commonMain/swift
, and all the appropriate intermediate source sets.
The bundled Swift code shares the same Framework as the Kotlin code and code generated by SKIE. Therefore, you can call the Kotlin code as usual, except without importing the Kotlin Framework.
Swift uses a different default visibility level than Kotlin.
Declarations without an explicit visibility modifier are usually internal
in Swift instead of public
.
Make sure to correctly use the public
modifier in the bundled Swift code; otherwise, it will not be visible outside the Kotlin Framework.