Kotlin Multiplatform परिचय
In this page:
// common code
expect fun functionName(): Type
// platform code
actual fun functionName(): Type = platformValue
The Idea Behind Multiplatform
Kotlin Multiplatform एक single shared codebase को, एक बार लिखकर, JVM (Android/server) पर, natively (iOS/desktop) पर, और यहां तक कि JavaScript (web) पर चलने के लिए compile होने देता है, platforms में business logic को दोहराने से बचते हुए।
उदाहरण: The Idea Behind Multiplatform
fun calculateTotal(prices: List<Double>): Double = prices.sum()
fun main() {
// This same function could be shared across Android, iOS, and web targets
println("Total: ${calculateTotal(listOf(9.99, 19.99, 4.99))}")
}
Login to try C/C++/Java/PHP code in the editor
expect and actual Declarations
Common code एक expect function या class declare करता है जब implementation को हर platform के लिए अलग होना चाहिए, और हर target असली, platform-specific code के साथ एक matching actual implementation प्रदान करता है। (expect/actual keywords को compile करने के लिए एक असली multiplatform Gradle module चाहिए; नीचे का snippet platform-specific actual की जगह लेते एक साधारण function के साथ वही idea simulate करता है।)
उदाहरण: expect and actual Declarations
fun platformName(): String = "JVM"
fun greetPlatform(): String = "Running on ${platformName()}"
fun main() {
// In a real KMP project, platformName() would be declared
// `expect fun platformName(): String` in commonMain, with each
// target (jvm, iosArm64, js) providing its own `actual` version.
println(greetPlatform())
}
Login to try C/C++/Java/PHP code in the editor
What Is Usually Shared
Kotlin Multiplatform projects आमतौर पर networking, data parsing, और validation rules जैसा business logic share करते हैं, जबकि UI code अक्सर platform-specific रहता है (iOS पर SwiftUI, Android पर Jetpack Compose या Views)।
उदाहरण: What Is Usually Shared
// Define a data class `Product`
data class Product(val name: String, val price: Double)
// Define the function `applyDiscount` taking `product`, `percent` and returning a `Product`
fun applyDiscount(product: Product, percent: Int): Product =
product.copy(price = product.price * (100 - percent) / 100)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `product`, set to `Product("Headphones", 100.0)`
val product = Product("Headphones", 100.0)
// Print `applyDiscount(product, 20)` to the console, with a trailing newline
println(applyDiscount(product, 20))
}
Login to try C/C++/Java/PHP code in the editor
Setting Up a Multiplatform Project
एक Kotlin Multiplatform project को Gradle के ज़रिए एक kotlin { } block से configure किया जाता है जो jvm(), iosArm64(), या js() जैसे targets declare करता है, हर एक की अपनी source set एक shared commonMain source set के साथ।
उदाहरण: Setting Up a Multiplatform Project
kotlin {
jvm()
iosArm64()
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
}
}
}
}
⚠️ Run this in your own terminal or Node.js environment.
- यह मान लेना कि Kotlin Multiplatform UI code भी अपने आप share करता है; आमतौर पर सिर्फ business logic share होता है, जबकि UI आमतौर पर अब भी platform-specific है (जब तक Compose Multiplatform का भी उपयोग न हो)।
- यह भूल जाना कि
expect/actualdeclarations को हर target platform से एक matchingactualimplementation चाहिए, नहीं तो उस target के लिए build fail हो जाता है। - Kotlin Multiplatform (एक codebase से कई platforms को target करना) को अलग-अलग Kotlin/JS या Kotlin/Native के साथ मिला देना, जो सिर्फ single-target compilation backends हैं।
- Kotlin Multiplatform (KMP) shared Kotlin code को एक codebase से JVM, JavaScript, और native targets (iOS, desktop, आदि) में compile होने देता है।
- Common code एक shared module में रहता है, किसी भी चीज़ के लिए
expectdeclarations उपयोग करते हुए जिसे platform-specific implementation चाहिए। - हर target platform common code में हर
expectdeclaration से मेल खाता एकactualimplementation प्रदान करता है। - KMP सबसे आम तौर पर Android और iOS apps के बीच business logic (networking, data models) share करने के लिए उपयोग होता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: