Skip to content
项目

ttypic/swift-klib-plugin

在 GitHub 查看

关于

Gradle Plugin for injecting Swift code into Kotlin Multiplatform Mobile shared module

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
-1
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文
Swift Klib library logo

Swift Klib Gradle Plugin

badge-ios badge-mac badge-tvos badge-watchos

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:

plugins {
    id("io.github.ttypic.swiftklib") version "0.6.4"
}

Using legacy plugin application:

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).

#Kotlin Multiplatform
kotlin.mpp.enableCInteropCommonization=true

Usage

Plugin works together with Kotlin Multiplatform plugin.

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.

@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 {
    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.

swiftklib {
    create("HelloSwift") {
        path = file("native/HelloSwift")
        packageName("com.ttypic.objclibs.greeting")
    }
}

Examples

  • More samples can be found in the examples/ folder.
  • Component demonstrating a multipurpose Kotlin Multiplatform and Swift Package audio player: radioplayer-kt

License

This package is licensed under MIT license.

探索相关项目

加载中…