Kotlin KMM — Share Code between iOS and Android

prerequisite :

  • classpath “com.android.tools.build:gradle:4.2.0-rc01”
  • classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32”

1- Create android library :

apply plugin: 'com.android.application'
  • Change it to the following:
apply plugin: 'com.android.library'

2- Create KMM Shared Module :

3- Add the Lib as git submodule for android

git submodule add <git repository URL>
git submodule init
implementation project(':KMMSharedModule')
include ':KMMSharedModule'

4- Distribute the KMM shared module using Cocoa-pods

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
kotlin("multiplatform")
id("com.android.library")
}

kotlin {
android()

targets {

ios() {

binaries {
framework {
baseName = "KMMSharedModule"

}
}

}


}
sourceSets {
val commonMain by getting
val androidMain by getting
val iosMain by getting
}
}

android {
compileSdkVersion(29)//Should be like your Android app
defaultConfig {
minSdkVersion(21)// Should be like your Android app
targetSdkVersion(29)//Should be like your Android app
}
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
}
//create iOS framework for real device (arm64 only)
val packForXcodeArm by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val framework = kotlin.targets.getByName<KotlinNativeTarget>("iosArm64").binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
val targetDir = File(buildDir, "xcode-framework-arm")
from({ framework.outputDirectory })
into(targetDir)
}

//create iOS framework for simulators
val packForXcodeX64 by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val framework = kotlin.targets.getByName<KotlinNativeTarget>("iosX64").binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
val targetDir = File(buildDir, "xcode-framework-X64")
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcodeArm)
tasks.getByName("build").dependsOn(packForXcodeX64)
./gradlew packForXCodeArm -PXCODE_CONFIGURATION=Release
./gradlew packForXCodeX64 -PXCODE_CONFIGURATION=Release
FRAMEWORK_NAME="KMMSharedModule"
ARM64PATH="./KMMSharedModule/build/xcode-framework-arm/${FRAMEWORK_NAME}.framework"
X64PATH="./KMMSharedModule/build/xcode-framework-X64/${FRAMEWORK_NAME}.framework"
UNIVERSAL_PATH="./xcode-frameworks-universal/"
rm -r "${UNIVERSAL_PATH}"
xcodebuild -create-xcframework -framework "${ARM64PATH}" -framework "${X64PATH}" -output "${UNIVERSAL_PATH}/${FRAMEWORK_NAME}.xcframework"
Pod::Spec.new do |spec|
spec.name = 'KmmSharedLib'
spec.version = '0.1.0'
spec.homepage = ''
spec.source = { :git => "Android Library git repository URL ", :tag => "#{spec.version}" }
spec.authors = ''
spec.license = ''
spec.summary = ''

spec.static_framework = true
spec.vendored_frameworks = "xcode-frameworks-universal/KMMSharedModule.xcframework"
spec.libraries = "c++"
spec.module_name = "KMMSharedModule_umbrella"
spec.ios.deployment_target = '11.0'


spec.pod_target_xcconfig = {
'KOTLIN_TARGET[sdk=iphonesimulator*]' => 'ios_x64',
'KOTLIN_TARGET[sdk=iphoneos*]' => 'ios_arm',
'KOTLIN_TARGET[sdk=watchsimulator*]' => 'watchos_x64',
'KOTLIN_TARGET[sdk=watchos*]' => 'watchos_arm',
'KOTLIN_TARGET[sdk=appletvsimulator*]' => 'tvos_x64',
'KOTLIN_TARGET[sdk=appletvos*]' => 'tvos_arm64',
'KOTLIN_TARGET[sdk=macosx*]' => 'macos_x64'
}

spec.pod_target_xcconfig = {
'ONLY_ACTIVE_ARCH' => 'YES'
}

end

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Android Gradle plugin with Test Fixtures support

Text Recognition Using Firebase ML Kit — Text Recognition in Android

Handling Flutter Colors like a Pro

Room Database with Kotlin Coroutines in Android

Chip navigation bottom bar in android studio | Java

Flappy Adventure Terms & Conditions

Interview questions for Android Developer job in India

Now in Android #52

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Emran shikh ahmad

Emran shikh ahmad

More from Medium

fragment stuck cannot move to another fragment in Android Navigation

Link C/C++ library dependencies to your own C/C++ code in an Android application using CMake

Quest to a refined Outdoor Experience Android App (Strava and Relive inspired) — Part 3

Rainbow🌈🌦 toast library