Created by Paris Tsiogas, Head of Mobile Development @ Desquared April 21, 2021

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 private 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 XCFramework, setup a podspec file 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.

<aside> 💡 This repository already supports sharing a KMM module via Swift Package Manager and you can find the detailed guide here: ‣ It is not mandatory to also support sharing via SPM.

</aside>

Diagram 1: Repository strategy and Cocoa pod sharing

Diagram 1: Repository strategy and Cocoa pod sharing

Cocoa pods

Let's quote some basic info about cocoa pods from the official documents.

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 82 thousand libraries and is used in over 3 million apps. CocoaPods can help you scale your projects elegantly.

Install

CocoaPods is built with Ruby and is installable with the default Ruby available on macOS. We recommend you use the default ruby. Using the default Ruby install can require you to use sudo when installing gems. Further installation instructions are in the guides.

$ sudo gem install cocoapods

We will make use of the Multiplatform Swift Package gradle plugin in order to generate a XCFramework and create a matching .podspec file to distribute our KMM shared module as a cocoa pod.

At this article we will not cover the integration of cocoa pods to an iOS project. You can find more info at https://cocoapods.org/.

Step 1: Install Multiplatform Swift Package plugin (946c545)