← Back to Kotlin Course | Chapter 14: JVM Interop & Best Practices | Lesson 4 of 7

Kotlin Multiplatform परिचय

Kotlin Multiplatform आपको एक shared logic का टुकड़ा लिखने देता है जो Android, iOS, web, और desktop पर एक साथ चल सके।
Syntax
kotlin
// 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

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

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

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

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

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

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

bash
kotlin {
    jvm()
    iosArm64()
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
            }
        }
    }
}

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि Kotlin Multiplatform UI code भी अपने आप share करता है; आमतौर पर सिर्फ business logic share होता है, जबकि UI आमतौर पर अब भी platform-specific है (जब तक Compose Multiplatform का भी उपयोग न हो)।
  2. यह भूल जाना कि expect/actual declarations को हर target platform से एक matching actual implementation चाहिए, नहीं तो उस target के लिए build fail हो जाता है।
  3. 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 में रहता है, किसी भी चीज़ के लिए expect declarations उपयोग करते हुए जिसे platform-specific implementation चाहिए।
  • हर target platform common code में हर expect declaration से मेल खाता एक actual implementation प्रदान करता है।
  • 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.