Kotlin Multiplatform(KMP) Masterclass - KMM
- Master iOS & Android app development with KMP
- Jetpack Compose & SwiftUI
- Ktor
- SQLDelight
- Clean Architecture
- MVI
Kotlin Multiplatform is a Kotlin-based framework that allows code sharing and frameworks between different platforms.
- JetBrains developed Kotlin Multiplatform, and using it to target mobile platforms is stable and production-ready
- Android
- iOs
- Web
- Desktop
- Please follow this link to complete this step.
- If you are running on Mac you can verify your environment requirements are successfully set by running
$ kdoctor
- Your response should be as follows. In case you run into an issue with
Cocoapods
for the Apple Silicon Chip please check this resource and follow the steps.
Each Kotlin Multiplatform project includes three modules:
-
shared is a Kotlin module containing the logic common for Android and iOS applications – the code you share between platforms. It uses Gradle as the build system to help automate your build process.
-
androidApp is a Kotlin module that builds into an Android application. It uses Gradle as the build system. The
androidApp
module depends on and uses the shared module as a regular Android library. -
iosApp is an Xcode project that builds into an iOS application. It depends on and uses the shared module as an iOS framework. The shared module can be used as a regular framework or a CocoaPods dependency, based on your chosen iOS framework distribution. For now, we focus on regular framework dependency.
-
The shared module consists of three source sets:
androidMain
,commonMain
, andiosMain
. Source set is a Gradle concept for several files logically grouped where each group has its dependencies. In Kotlin Multiplatform, different source sets in a shared module can target different platforms
- The common source set uses common Kotlin code, and platform source sets use Kotlin code specific to each target. Kotlin/JVM is used for
androidMain
and Kotlin/Native foriosMain
:
- When the shared module is built into an Android library, common Kotlin code is treated as Kotlin/JVM. When it is built into an iOS framework, common Kotlin is treated as Kotlin/Native
Take a look at from commonMain
:
interface Platform {
val name: String
}
expect fun getPlatform(): Platform
- The
expect
instructs the Kotlin compiler that the implementation must exist in bothandroidMain
andiosMain
folders.Platform
is similar to an abstract class in kt. - The
actual
instructs the Kotlin compiler the actual platform-based code to return. In this case, the features from each platform are independent of their respective platform.
Android:
class AndroidPlatform : Platform {
override val name: String = "Android ${android.os.Build.VERSION.SDK_INT}"
}
actual fun getPlatform(): Platform = AndroidPlatform()
iOS:
import platform.UIKit.UIDevice
class IOSPlatform: Platform {
override val name: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
}
actual fun getPlatform(): Platform = IOSPlatform()
Takes KMP libraries that work for both Android and iOS.
Android libs that exist in the shared module
iOS libs that exist in the shared module
To add in above we use the build.gradle.kts
that lives inside our shared module. Inside sourceSets
add both androidMain
and iosMain
dependencies declaration sections respectively
sourceSets {
// ... other code
androidMain.dependencies {
// Android dependencies here (only used in androidMain module)
}
iosMain.dependencies {
// iOS dependencies here (only used in iosMain module)
}
}