๐ŸŸฃ

KMM & KMP (Kotlin Multiplatform) ํฌ๋กœ์Šคํ”Œ๋žซํผ ์น˜ํŠธ์‹œํŠธ

Kotlin Multiplatform (KMM/KMP) ์™„์ „ ์ •๋ณต ๊ฐ€์ด๋“œ: commonMain/androidMain/iosMain ์†Œ์Šค์…‹ ์•„ํ‚คํ…์ฒ˜, expect/actual ํ”Œ๋žซํผ ๋ธŒ๋ฆฟ์ง€, Ktor ๋น„๋™๊ธฐ ๋„คํŠธ์›Œํฌ ํ†ต์‹ , kotlinx.coroutines & serialization, SQLDelight ๋กœ์ปฌ DB, Swift ์ƒํ˜ธ ์šด์šฉ์„ฑ(XCFramework/CocoaPods) ๋ฐ Compose Multiplatform UI ๊ณต์œ 

--views
๋‹จ์ˆ˜:
#kmm#kmp#kotlin-multiplatform#kotlin#android#ios#swift#ktor#compose-multiplatform#sqldelight์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-10-11

1. ์‚ฌ์ „ ์š”๊ฑด & ์‹œ์Šคํ…œ ์š”๊ตฌ์‚ฌ์–‘ (Prerequisites & System Requirements)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Windows 10/11 64-bit (Android ๊ฐœ๋ฐœ ์ „์šฉ), macOS 13+ (iOS ๋นŒ๋“œ ํ•„์ˆ˜)macOS Sonoma 14+ (Apple Silicon M1/M2/M3)Kotlin/Native iOS ํ”„๋ ˆ์ž„์›Œํฌ(XCFramework) ๋นŒ๋“œ๋Š” Mac ํ•„์ˆ˜
CPU4 ์ฝ”์–ด ์ด์ƒ (Intel Core i5/i7, Apple Silicon M1+)8 ์ฝ”์–ด ~ 16 ์ฝ”์–ด ์ด์ƒKotlin/Native LLVM ์ปดํŒŒ์ผ ๋ฐ Gradle ์บ์‹œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ
๋ฉ”๋ชจ๋ฆฌ (RAM)8 GB ์ด์ƒ (์ตœ์†Œ)16 GB ~ 32 GB ์ด์ƒAndroid Studio + Xcode ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ + Gradle ๋ฐ๋ชฌ ๋™์‹œ ๊ตฌ๋™
๋””์Šคํฌ (Storage)25 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„60 GB ~ 100 GB ์ด์ƒ์˜ ๊ณ ์† NVMe SSDXcode(30GB+), Android SDK/NDK, Gradle ์•„ํ‹ฐํŒฉํŠธ ์บ์‹œ
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„JDK 17 LTS, Kotlin 1.9.20+ / 2.0+, GitJDK 21 LTS, Android Studio Hedgehog+, Xcode 15+, CocoaPodskdoctor ๋„๊ตฌ๋กœ ์‚ฌ์ „ ํ™˜๊ฒฝ ์ง„๋‹จ ํ•„์ˆ˜

๐Ÿฉบ ํ™˜๊ฒฝ ์ง„๋‹จ ๋„๊ตฌ kdoctor ์‹คํ–‰

JetBrains๊ฐ€ ์ œ๊ณตํ•˜๋Š” KMP ๊ณต์‹ ํ™˜๊ฒฝ ์ง„๋‹จ ๋„๊ตฌ๋กœ Android/iOS ๋นŒ๋“œ ํˆด์ฒด์ธ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ตฌ์„ฑ๋˜์—ˆ๋Š”์ง€ 1์ดˆ ๋งŒ์— ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

# macOS (Homebrew)
brew install kdoctor
kdoctor

# ์ ๊ฒ€ ํ•ญ๋ชฉ:
# [v] Java (JDK 17+)
# [v] Android Studio & Android SDK
# [v] Xcode & Command Line Tools
# [v] CocoaPods
# -> Your system is ready for Kotlin Multiplatform Mobile development!

2. ํ•ต์‹ฌ ๋ฉ˜ํƒˆ ๋ชจ๋ธ (Core Mental Model: ๋กœ์ง ๊ณต์œ  ์•„ํ‚คํ…์ฒ˜)

React Native๋‚˜ Flutter๊ฐ€ ์ž์ฒด ๋ Œ๋”๋ง ์—”์ง„์ด๋‚˜ ๋Ÿฐํƒ€์ž„์„ ์ž„๋ฒ ๋”ฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ, KMP๋Š” 100% ์ˆœ์ˆ˜ ๋„ค์ดํ‹ฐ๋ธŒ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ์ง์ ‘ ์ปดํŒŒ์ผ๋ฉ๋‹ˆ๋‹ค.

+-----------------------------------------------------------------------------------+
|                        Kotlin Multiplatform (KMP) ์•„ํ‚คํ…์ฒ˜                        |
|                                                                                   |
|  [commonMain: ๊ณต์œ  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง (์ˆœ์ˆ˜ ์ฝ”ํ‹€๋ฆฐ)]                                    |
|    - ๋„๋ฉ”์ธ ์—”ํ‹ฐํ‹ฐ & ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง                                                |
|    - Ktor ๋น„๋™๊ธฐ HTTP ํ†ต์‹  ํด๋ผ์ด์–ธํŠธ                                            |
|    - kotlinx.coroutines (StateFlow / SharedFlow ์ƒํƒœ ๊ด€๋ฆฌ)                        |
|    - SQLDelight / Room Multiplatform ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์บ์‹œ                       |
|    - expect ์„ ์–ธ: "ํ”Œ๋žซํผ๋ณ„ ๊ณ ์œ  ๊ธฐ๋Šฅ(์นด๋ฉ”๋ผ, UUID ๋“ฑ)์˜ ์ธํ„ฐํŽ˜์ด์Šค ์„ ์–ธ"         |
|                                  โ”‚                                                |
|         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                       |
|         โ–ผ (Kotlin/JVM ์ปดํŒŒ์ผ)                             โ–ผ (Kotlin/Native LLVM)   |
|  [androidMain (Android ๊ตฌํ˜„)]                      [iosMain (iOS ๊ตฌํ˜„)]           |
|    - actual ๊ตฌํ˜„ (Android Context)                   - actual ๊ตฌํ˜„ (UIKit / iOS API)|
|    - Android AAR ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํŒจํ‚ค์ง•                 - Apple XCFramework / Objective-C|
|         โ”‚                                                 โ”‚                       |
|         โ–ผ                                                 โ–ผ                       |
|  [Android ์•ฑ ๊ณ„์ธต]                                 [iOS ์•ฑ ๊ณ„์ธต (Swift)]          |
|    - Jetpack Compose UI                              - SwiftUI ๋„ค์ดํ‹ฐ๋ธŒ UI        |
|    - ViewModel์—์„œ shared ๋ชจ๋“ˆ ํ˜ธ์ถœ                  - Swift `@Published` ๋ฐ”์ธ๋”ฉ  |
+-----------------------------------------------------------------------------------+

3. [์ดˆ๋ณด์ž 1๋‹จ๊ณ„] build.gradle.kts ์†Œ์Šค์…‹(Source Sets) ์„ค์ •

๊ณตํ†ต ๋กœ์ง ๋ชจ๋“ˆ(shared)์˜ Gradle ์„ค์ •์ž…๋‹ˆ๋‹ค.

// shared/build.gradle.kts
plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
    alias(libs.plugins.kotlinxSerialization)
}

kotlin {
    // 1. Android ๋Œ€์ƒ ํƒ€๊นƒ ์„ ์–ธ
    androidTarget {
        compilations.all {
            kotlinOptions { jvmTarget = "17" }
        }
    }

    // 2. iOS ๋Œ€์ƒ ํƒ€๊นƒ ์„ ์–ธ (XCFramework ์ƒ์„ฑ)
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach { iosTarget ->
        iosTarget.binaries.framework {
            baseName = "SharedKit"
            isStatic = true
        }
    }

    // 3. ์†Œ์Šค์…‹๋ณ„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์˜์กด์„ฑ ์„ ์–ธ
    sourceSets {
        commonMain.dependencies {
            // ๋น„๋™๊ธฐ ์ฝ”๋ฃจํ‹ด & JSON ์ง๋ ฌํ™”
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")

            // Ktor ๊ณตํ†ต HTTP ์—”์ง„
            implementation("io.ktor:ktor-client-core:2.3.9")
            implementation("io.ktor:ktor-client-content-negotiation:2.3.9")
            implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.9")
        }

        androidMain.dependencies {
            // Android ์ „์šฉ Ktor OkHttp ์—”์ง„
            implementation("io.ktor:ktor-client-okhttp:2.3.9")
        }

        iosMain.dependencies {
            // iOS ์ „์šฉ Ktor Darwin ์—”์ง„ (Apple URLSession ๊ธฐ๋ฐ˜)
            implementation("io.ktor:ktor-client-darwin:2.3.9")
        }
    }
}

4. [2๋‹จ๊ณ„] expect / actual ํ”Œ๋žซํผ ๋ธŒ๋ฆฟ์ง€ ํŒจํ„ด

OS ๊ณ ์œ  API(๋””๋ฐ”์ด์Šค ๋ชจ๋ธ๋ช…, UUID, ๋„ค์ดํ‹ฐ๋ธŒ ๋‹ค์ด์–ผ๋กœ๊ทธ)๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์ธํ„ฐํŽ˜์ด์Šค ์—†์ด ์ฝ”ํ‹€๋ฆฐ ํ‚ค์›Œ๋“œ๋กœ ์ง๊ด€์ ์œผ๋กœ ๋ธŒ๋ฆฟ์ง•ํ•ฉ๋‹ˆ๋‹ค.

โ‘  commonMain: expect ์„ ์–ธ (๊ธฐ๋Œ€์น˜)

// shared/src/commonMain/kotlin/Platform.kt
package com.luckytech.shared

expect class Platform() {
    val name: String
    val osVersion: String
}

expect fun getDeviceUniqueId(): String

โ‘ก androidMain: actual ๊ตฌํ˜„ (Android ๋„ค์ดํ‹ฐ๋ธŒ)

// shared/src/androidMain/kotlin/Platform.kt
package com.luckytech.shared

import android.os.Build
import java.util.UUID

actual class Platform actual constructor() {
    actual val name: String = "Android ${Build.MODEL}"
    actual val osVersion: String = "API Level ${Build.VERSION.SDK_INT}"
}

actual fun getDeviceUniqueId(): String = UUID.randomUUID().toString()

โ‘ข iosMain: actual ๊ตฌํ˜„ (iOS ๋„ค์ดํ‹ฐ๋ธŒ UIKit/Swift API)

// shared/src/iosMain/kotlin/Platform.kt
package com.luckytech.shared

import platform.UIKit.UIDevice
import platform.Foundation.NSUUID

actual class Platform actual constructor() {
    actual val name: String = UIDevice.currentDevice.systemName()
    actual val osVersion: String = UIDevice.currentDevice.systemVersion()
}

actual fun getDeviceUniqueId(): String = NSUUID().UUIDString()

5. [3๋‹จ๊ณ„] Ktor ๊ธฐ๋ฐ˜ ๊ณตํ†ต ๋น„๋™๊ธฐ API ํด๋ผ์ด์–ธํŠธ ๊ฐœ๋ฐœ

Android์™€ iOS๊ฐ€ 100% ๋™์ผํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ๊ณผ DTO๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.

// shared/src/commonMain/kotlin/ApiClient.kt
package com.luckytech.shared

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

@Serializable
data class UserProfile(
    val id: Long,
    val username: String,
    val email: String,
    val rank: String
)

class LuckyRepository {
    private val httpClient = HttpClient {
        install(ContentNegotiation) {
            json(Json {
                prettyPrint = true
                isLenient = true
                ignoreUnknownKeys = true
            })
        }
    }

    @Throws(Exception::class) // Swift์—์„œ try-catch๋กœ ์žก์„ ์ˆ˜ ์žˆ๋„๋ก ์• ๋„ˆํ…Œ์ด์…˜ ํ•„์ˆ˜
    suspend fun fetchUserProfile(userId: Long): UserProfile {
        return httpClient.get("https://api.luckytech.com/users/$userId").body()
    }
}

6. [4๋‹จ๊ณ„] Swift (iOS) ๋ฐ Kotlin (Android)์—์„œ์˜ ์‚ฌ์šฉ๋ฒ•

โ‘  Android (Jetpack Compose)์—์„œ์˜ ํ˜ธ์ถœ

// androidApp/src/main/java/MainActivity.kt
class MainViewModel : ViewModel() {
    private val repository = LuckyRepository()
    private val _userState = MutableStateFlow<UserProfile?>(null)
    val userState: StateFlow<UserProfile?> = _userState.asStateFlow()

    fun loadUser() {
        viewModelScope.launch {
            try {
                _userState.value = repository.fetchUserProfile(101L)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}

โ‘ก iOS (SwiftUI)์—์„œ์˜ ํ˜ธ์ถœ

Kotlin์˜ suspend fun์€ Swift์—์„œ ๋„ค์ดํ‹ฐ๋ธŒ async/await ํ•จ์ˆ˜๋กœ ์ž๋™ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค!

// iosApp/ContentView.swift
import SwiftUI
import SharedKit // KMP๊ฐ€ ๋นŒ๋“œํ•ด ์ค€ ๋„ค์ดํ‹ฐ๋ธŒ ํ”„๋ ˆ์ž„์›Œํฌ

@MainActor
class UserViewModel: ObservableObject {
    @Published var user: UserProfile? = nil
    private let repository = LuckyRepository()

    func loadUser() async {
        do {
            // ์ฝ”ํ‹€๋ฆฐ suspend ํ•จ์ˆ˜๋ฅผ Swift์˜ async/await๋กœ ์ง์ ‘ ํ˜ธ์ถœ!
            let result = try await repository.fetchUserProfile(userId: 101)
            self.user = result
        } catch {
            print("์‚ฌ์šฉ์ž ์กฐํšŒ ์‹คํŒจ: \(error)")
        }
    }
}

struct ContentView: View {
    @StateObject private var viewModel = UserViewModel()

    var body: some View {
        VStack(spacing: 16) {
            if let user = viewModel.user {
                Text("ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค, \(user.username)๋‹˜!")
                    .font(.title)
                Text("๋“ฑ๊ธ‰: \(user.rank)")
                    .foregroundColor(.green)
            } else {
                Text("๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์ค‘...")
            }
        }
        .task {
            await viewModel.loadUser()
        }
    }
}

7. ํ”„๋กœ๋•์…˜ ๋นŒ๋“œ & ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ… ๊ฐ€์ด๋“œ

๐Ÿ› ๏ธ iOS XCFramework ๋นŒ๋“œ ๋ช…๋ น์–ด

# 1. ๋””๋ฒ„๊ทธ์šฉ XCFramework ๋นŒ๋“œ (์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ/์‹ค๊ธฐ๊ธฐ ํ†ตํ•ฉ ํ”„๋ ˆ์ž„์›Œํฌ)
./gradlew :shared:assembleSharedKitDebugXCFramework

# 2. ๋ฆด๋ฆฌ์ฆˆ ๋ฐฐํฌ์šฉ XCFramework ๋นŒ๋“œ
./gradlew :shared:assembleSharedKitReleaseXCFramework
# ์‚ฐ์ถœ๋ฌผ ๊ฒฝ๋กœ: shared/build/XCFrameworks/release/SharedKit.xcframework

โš ๏ธ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” ์žฅ์•  ์œ ํ˜• ๋ฐ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ

  • ์ฆ์ƒ 1: Swift์—์„œ Unhandled Kotlin Exception์œผ๋กœ ์•ฑ ํฌ๋ž˜์‹œ
    • ์›์ธ: Kotlin suspend ํ•จ์ˆ˜๋‚˜ ์ผ๋ฐ˜ ํ•จ์ˆ˜์— @Throws ์• ๋„ˆํ…Œ์ด์…˜์„ ๋ถ™์ด์ง€ ์•Š์•„ Swift ๋Ÿฐํƒ€์ž„์ด ์˜ˆ์™ธ๋ฅผ ์บ์น˜ํ•˜์ง€ ๋ชปํ•จ.
    • ํ•ด๊ฒฐ: ๊ณตํ†ต ๋ชจ๋“ˆ ํ•จ์ˆ˜์— @Throws(Exception::class) ์• ๋„ˆํ…Œ์ด์…˜ ํ•„์ˆ˜ ์ถ”๊ฐ€.
  • ์ฆ์ƒ 2: CocoaPods ์—ฐ๋™ ์‹œ Pod not found ๋˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ ๋งํฌ ์‹คํŒจ
    • ํ•ด๊ฒฐ:
      cd iosApp
      pod repo update
      pod install --repo-update
  • ์ฆ์ƒ 3: Kotlin 2.0 K2 ์ปดํŒŒ์ผ๋Ÿฌ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ˜ธํ™˜์„ฑ
    • ์ตœ์‹  KMP ํ”„๋กœ์ ํŠธ๋Š” kotlin("multiplatform") version "2.0.0" ์ด์ƒ์„ ๊ถŒ์žฅํ•˜๋ฉฐ, Compose Multiplatform๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ ์‹œ ๊ณต์‹ ํ˜ธํ™˜์„ฑ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๋ฐ˜๋“œ์‹œ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500