This gradle plugin provides two-way interoperability between Kotlin dependencies and Kotlin-based Swift Package Multiplatform.
plugins {
java
kotlin("multiplatform")
id("com.github.pagr0m.kotlin.native.spm")
}
-
Define multiplatform targets (
macos
,ios
,tvos
,watchos
). -
Declare a
spm
section with all the necessary package dependencies for each platform. -
Add package dependencies with URL link, version and name. This name would be used as an import in the Kotlin project.
build.gradle.kts
example:kotlin { macosX64() spm { macos("11") { dependencies { packages( url = "https://github.com/AFNetworking/AFNetworking.git", version = "4.0.0", name = "AFNetworking" ) } } } }
-
Run the
spmImport
gradle tasks for each dependency and platform../gradlew spmImport
NOTE: At the moment, Kotlin is not directly compatible with Swift. Therefore, you can only connect Objective-C libraries that have a Swift package file.
To create a Kotlin library as a swift package dependency, you need to add the Kotlin multiplatform settings and specify the name of the resulting library (this is the name that will be used during import).
-
Extend targets configuration with framework baseName (
KlibIOS
in this example. Could be any). This name would be used as Swift package name in XCode project. -
Declare a
spm
section with needed platforms. Note that you can add Swift package dependencies too (see first section).build.gradle.kts
example:kotlin { iosArm64 { binaries { framework { baseName = "KlibIOS" } } } spm { ios("11") { } } }
-
Create xcframework
./gradlew bundleXCFramework
XCFramework will be located in
build/spmUtils/xcframework/KotlinLibrary.xcframework
-
Connect xcframework to XCode project.
-
Option 1: via XCode GUI
-
Option 2: via
Package.swift
file// swift-tools-version:5.3 import PackageDescription let package = Package( name: "ExampleWithKotlinLibrary", products: [ .library( name: "ExampleWithKotlinLibrary", targets: [ "ExampleWithKotlinLibrary", "KotlinLibrary" ] ) ], targets: [ .target( name: "ExampleWithKotlinLibrary", dependencies: [] ), .binaryTarget( name: "KotlinLibrary", path: "./xcframework/KotlinLibrary.xcframework" ) ] )
-
-
Complete 1-3 steps in section above.
-
Add credentials to
local.properties
file.git.credentials.username = USERNAME git.credentials.password = PASSWORD git.credentials.giturl = URL_TO_GIT_REPOSITORY.git
NOTE: Do not forget to add this file to
.gitignore
. -
Run publishXCFramework task to zip and publish it.
./gradlew publishXCFramework
-
Add binaryTarget in
Package.swift
file.let package = Package( // some code .binaryTarget( name: "KotlinLibrary", url: "pathToGitUrl", checksum: "..." ) )
NOTE: the binaryTarget name must match with name of the xcframework that is packed into the archive. By default, this is
KotlinLibrary
.
- Kotlin Multiplatform Mobile project using network libraries (AFNetworking for iOS, OkHttp for Android)
- Kotlin Multiplatform Project adding Kotlin library as XCFramework to Xcode project
- Kotlin Multiplatform Project for macOS target with Swift Package dependency
Feel free to open an issue or submit a pull request for any bugs/improvements.