KMP: Work with existing project

Serhii Hulenko
3 min readAug 27, 2023

--

Greetings, all! I understand that you may have found yourself in a situation where you need to work with KMP not just from the start but also on ongoing projects. In this article, I will demonstrate how to effortlessly include the KMP library in an existing project.

First of all, you need to create a KMP module in your Android existing project. It is very easy.

File > New > New Module.
IOS framework distribution: Cocoapods dependency manager

That is all that you need to do to add a KMP module to your Android existing module. However, we need to add a KMP module to the IOS project.

Let's go to ../shared/build.gradle

Add new plugin:

plugins {
...
id("io.github.luca992.multiplatform-swiftpackage") version "2.1.2"
}

Repository with this plugin:

https://github.com/luca992/multiplatform-swiftpackage

Add config:

multiplatformSwiftPackage {
packageName("KmpAndroidExistingProjectShared")
swiftToolsVersion("5.8.1")
targetPlatforms {
iOS { v("14.1") }
}
outputDirectory(File(rootDir, "/"))
}

KmpAndroidExistingProjectShared — it is named for you library

After you need to run the command in your terminal:

./gradlew shared:createXCFrameworkD

You can see the new directory in root.

It is the framework for your IOS project.

OK, it is great. But how does it help our IOS developers? We can push this framework with the Android project to git and get into the IOS project from git.

For it, we need to create a *.podspec file:

Named file like name framework in config. Write config data to file:

Pod::Spec.new do |spec|
spec.name = 'KmpAndroidExistingProjectShared'
spec.version = '1.0.0'
spec.homepage = 'https://www.cocoapods.org'
spec.source = { :git => "git@github.com:marazmone/kmp-android-existing-project.git" }
spec.authors = 'Serhii Hulenko'
spec.summary = 'KMM shared code pod'
spec.static_framework = true
spec.vendored_frameworks = "KmpAndroidExistingProjectShared.xcframework"
spec.ios.deployment_target = '14.1'
end

Thats all. After pushing this library to git you can add this pod to your IOS project.

pod 'KmpAndroidExistingProjectShared', :git => 'git@github.com:marazmone/kmp-android-existing-project.git'

Now you can use the KMP module in your IOS project.

For example, you can see these repositories:

Android:

IOS:

--

--