Skip to content

RocqJones/Explore-KMP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Explore-KMP

Kotlin Multiplatform(KMP) Masterclass - KMM

Delve into:

  • Master iOS & Android app development with KMP
  • Jetpack Compose & SwiftUI
  • Ktor
  • SQLDelight
  • Clean Architecture
  • MVI

What is KMP?

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

image

Supported Platforms.

  • Android
  • iOs
  • Web
  • Desktop

Environment setup.

$ kdoctor

image

Project structure

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, and iosMain. 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

image

  • 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 for iosMain:

image

  • 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

Understanding Platform - expect and actual.

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 both androidMain and iosMain 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()

Adding Third Party Libraries

1. CommonMain

Takes KMP libraries that work for both Android and iOS.

2. AndroidMain

Android libs that exist in the shared module

3. iOSMain

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)
    }
}

Resources.

  1. Official KMM overview.
  2. How to run CocoaPods on Apple Silicon (M1/M2)

About

Kotlin Multiplatform(KMP) Masterclass - KMM

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published