2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Kotlin Multiplatform is a tool that allows us to write code in the same programming language (Kotlin) and run it on different devices at the same time, such as mobile phones, computers, and web pages. This saves time because you don't need to repeat the same functions for each device.
Feature 1: Code Sharing – You can write logic code once, such as a calculation function, and use it in both Android and iOS applications.
Feature 2: Platform-specific code – You can write specific code for each platform to handle platform-specific features, such as using the device's camera. For example, in an Android app, use Android's Camera API to take a photo, and in an iOS app, use iOS's UIImagePickerController to take a photo.
Feature 3: Interoperability – Kotlin Multiplatform can interoperate with existing Java, Swift, or Objective-C code, making it easy to integrate into existing projects.
Feature 4: Different compilation time – The code in androidMain is compiled into code that can be understood by Android using kotlin/jvm, and the code in iosMain is compiled into code that can be understood by iOS using kotlin/native
step:
1. Define shared logic: First, define common business logic in a shared module (that is, commonMain). These codes do not depend on any platform, so they can run on any platform. This shared module is also equivalent to the basis for cross-end sharing.
Shared logic (commonMain):
import kotlin.random.Random
class Greeting {
private val platform: Platform = getPlatform()
fun greet(): String {
val firstWord = if (Random.nextBoolean()) "Hi!" else "Hello!"
return "$firstWord Guess what this is! > ${platform.name.reversed()}!"
}
}
The above code is the shared code in kmp cross-end. These codes can run on both Android and iOS. However, this code finally returns a string format. The platform.name needs to be obtained from this string, which involves cross-end. Because the kotlin syntax for obtaining platform.name is different on Android and iOS, we need to define an excuse to tell androidMain and iosMain to handle the logic of obtaining platform.name respectively, and then they start to implement this logic. The code is as follows
interface Platform {
val name: String
}
expect fun getPlatform(): Platform
2. Platform-specific implementation: In androidMain and iosMain, provide specific implementations of the interfaces in commandMain, and write some platform-specific code
Android platform implementation (androidMain):
import android.os.Build
class AndroidPlatform : Platform {
override val name: String = "Android ${Build.VERSION.SDK_INT}"
}
actual fun getPlatform(): Platform = AndroidPlatform()
iOS platform implementation (iosMain):
import platform.UIKit.UIDevice
class IOSPlatform: Platform {
override val name: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
}
actual fun getPlatform(): Platform = IOSPlatform()
They both said obediently, "Okay, big brother, we will implement it for you." The Android side said, "Big brother, I implemented it this way." The iOS side said, "Big brother, I implemented it that way."