Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ttypic/swift-klib-plugin
Gradle Plugin for injecting Swift code into Kotlin Multiplatform Mobile shared module
https://github.com/ttypic/swift-klib-plugin
gradle gradle-plugin ios kotlin kotlin-multiplatform kotlin-multiplatform-mobile kotlin-native swift swift-kotlin
Last synced: 4 days ago
JSON representation
Gradle Plugin for injecting Swift code into Kotlin Multiplatform Mobile shared module
- Host: GitHub
- URL: https://github.com/ttypic/swift-klib-plugin
- Owner: ttypic
- License: mit
- Created: 2021-11-07T09:10:14.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-27T18:37:03.000Z (about 1 month ago)
- Last Synced: 2024-12-18T10:02:31.961Z (11 days ago)
- Topics: gradle, gradle-plugin, ios, kotlin, kotlin-multiplatform, kotlin-multiplatform-mobile, kotlin-native, swift, swift-kotlin
- Language: Kotlin
- Homepage:
- Size: 282 KB
- Stars: 235
- Watchers: 4
- Forks: 12
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-kotlin-multiplatform - Swift Klib - This is a Gradle Plugin to build Swift code as part of your Kotlin Multiplatform project. With this plugin, you can access Swift-only iOS libraries, such as CryptoKit and experiment with Swift to Kotlin interoperability. (Libraries / Build & Development Tools)
README
# Swift Klib Gradle Plugin
![badge-ios](https://img.shields.io/badge/platform-ios-light)
![badge-mac](https://img.shields.io/badge/platform-macos-light)
![badge-tvos](https://img.shields.io/badge/platform-tvos-light)
![badge-watchos](https://img.shields.io/badge/platform-watchos-light)This gradle plugin provides easy way to include your Swift source files in your **Kotlin Multiplatform Mobile**
shared module and access them in Kotlin via `cinterop` for iOS targets. It is useful for:* Accessing Swift-only libraries _(e.g. CryptoKit)_
* Creating a Kotlin-Friendly Swift API
* Learning how Swift <-> Kotlin interoperability works**Note:** _Plugin has been tested on Gradle 7.5+, Xcode 15+_
## Installation
Using the [plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block):
```kotlin
plugins {
id("io.github.ttypic.swiftklib") version "0.6.4"
}
```Using [legacy plugin application](https://docs.gradle.org/current/userguide/plugins.html#sec:old_plugin_application):
```kotlin
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("io.github.ttypic:plugin:0.6.4")
}
}apply(plugin = "io.github.ttypic.swiftklib")
```
Add this to the project-level gradle.properties file (if it’s not already included).
```properties
#Kotlin Multiplatform
kotlin.mpp.enableCInteropCommonization=true```
## Usage
Plugin works together with [Kotlin Multiplatform plugin](https://plugins.gradle.org/plugin/org.jetbrains.kotlin.multiplatform).
### Prepare Swift code
Place your Swift files inside your project in separate folder (e.g. `native/HelloSwift`).
Make sure that Swift API you want to expose to Kotlin is [compatible with Objective-C](https://lazarevzubov.medium.com/compatible-with-objective-c-swift-code-e7c3239d949).
```swift
@objc public class HelloWorld : NSObject {
@objc public class func helloWorld() -> String {
return "HeLLo WorLd!"
}
}
```### Setup swiftklib extension
Then you need to add `cinterop` for target platforms in your **Kotlin Multiplatform Plugin**. There is
no need to configure it or add `.def` file, all configuration will be done automatically by **Swift Klib**.```kotlin
kotlin {
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64(),
).forEach {
it.compilations {
val main by getting {
cinterops {
create("HelloSwift")
}
}
}
}//...
}
```And finally provide settings for it in `swiftklib` extension.
You need to specify `path` and `packageName` parameters.```kotlin
swiftklib {
create("HelloSwift") {
path = file("native/HelloSwift")
packageName("com.ttypic.objclibs.greeting")
}
}
```### Examples
- More samples can be found in the [examples/](https://github.com/ttypic/swift-klib-plugin/tree/main/examples) folder.
- Component demonstrating a multipurpose Kotlin Multiplatform and Swift Package audio player: [radioplayer-kt](https://github.com/markst/radioplayer-kt)## License
This package is licensed under MIT license.