65

Share KMM module with iOS via SPM

 3 years ago
source link: https://www.notion.so/Share-KMM-module-with-iOS-via-SPM-8580006dce61414980216a10a647e9f4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Share KMM module with iOS via SPM
https%3A%2F%2Fwww.notion.so%2Fimages%2Fpage-cover%2Fgradients_2.png?table=block&id=8580006d-ce61-4149-8021-6a10a647e9f4&width=2100&userId=&cache=v2
Drag image to reposition
https%3A%2F%2Fcdn.iconscout.com%2Ficon%2Ffree%2Fpng-256%2Fkotlin-2752140-2284957.png?table=block&id=8580006d-ce61-4149-8021-6a10a647e9f4&userId=&cache=v2
Share KMM module with iOS via SPM
What is KMM?
Kotlin Multiplatform Mobile (KMM) is an SDK that allows you to use the same business logic code in both iOS and Android applications with the power of Kotlin language. The Kotlin/Native compiler produces a framework that can be used in your iOS project.
The problem
KMM can be also applied to existing projects and increase code sharing between Android & iOS.
The main question is how the KMM code is shared with the iOS project and what is the repository strategy you could follow in order to do so and do not distract too much the way your team works. This article will try to give a solution to the above.
Solution overview
Because we are focusing on existing apps, we will assume that you already work with 2 repositories (Android & iOS). We will also assume that you have already integrated KMM at your Android repository. After that we will demonstrate a way to compile your KMM code as a Swift Package and publish it at the Android repository. To help you understand every step, we have created a KotlinMultiplatformSwiftPackageExample and performed every step in a separate commit.
https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fc0c927ec-6f70-4ea5-8266-da0a7bd468ba%2Frepository_diagram.png?table=block&id=fd0b2b78-0d2a-4871-a29c-609654803b2a&width=3210&userId=&cache=v2
Diagram 1: Repository strategy and Swift package sharing
Swift Package Manager
https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F9f896957-55c6-4cc4-a1df-71408e8fdfa4%2Frendered2x-1588803767.png?table=block&id=7b59f7de-abcc-48f0-9be9-9d50fa931192&width=2490&userId=&cache=v2
Let's quote some basic info about SPM from the official documents.
The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. The Package Manager is included in Swift 3.0 and above.
Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code that developers can use in their projects. They bundle source files, binaries, and resources in a way that’s easy to use in your app’s project. Xcode supports creating and publishing Swift packages, as well as adding, removing, and managing package dependencies. Its support for Swift packages is built on top of the open source Swift Package Manager project.
Every package lives in a repository and it'a accessible from Xcode through its URL.
We will make use of the Multiplatform Swift Package gradle plugin in order to generate a XCFramework and create a matching Package.swift file to distribute it as a binary target.
Step 1: Install Multiplatform Swift Package plugin (946c545)
Spot the build.gradle.kts inside your shared module. Add the following lines in order to install the plugin. After that perform a gradle sync.
plugins { ... id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3" ... }
Shell
Step 2: Setup the plugin (1d65c14)
The swift package plugin can be configured in order to match your needs. The configuration takes place inside the kotlin block at build.gradle.kts. You can find more info in plugin's readme file in GitHub. We will demonstrate here some basic setup the suited our needs.
kotlin { ... multiplatformSwiftPackage { packageName("YourModuleName") swiftToolsVersion("5.3") targetPlatforms { iOS { v("13") } } outputDirectory(File(rootDir, "/")) } ... }
Shell
Check that the output directory is set to rootDir, that's the key in order to upload the KMM package at the Android repository and still be accessible via Xcode. After that perform a gradle sync.
Step 3: Build and publish the package (15e0c6d)
Now we should run a gradlew command in order to create the XCFramework, ZIP file and Package.swift. You should run the createSwiftPackage gradlew command. You can run it either from the gradle panel on the right of Android Studio or via a terminal command.
./gradlew yourModuleName:createSwiftPackage
Shell

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK