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 ํ์ |
| CPU | 4 ์ฝ์ด ์ด์ (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 SSD | Xcode(30GB+), Android SDK/NDK, Gradle ์ํฐํฉํธ ์บ์ |
| ๋คํธ์ํฌ / ๋ฐํ์ | JDK 17 LTS, Kotlin 1.9.20+ / 2.0+, Git | JDK 21 LTS, Android Studio Hedgehog+, Xcode 15+, CocoaPods | kdoctor ๋๊ตฌ๋ก ์ฌ์ ํ๊ฒฝ ์ง๋จ ํ์ |
๐ฉบ ํ๊ฒฝ ์ง๋จ ๋๊ตฌ 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)์ ๋ํ ์ด์ ํ์ ์ถ๊ฐ.
- ์์ธ: Kotlin
- ์ฆ์ 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๊ณผ ํจ๊ป ์ฌ์ฉ ์ ๊ณต์ ํธํ์ฑ ๋งคํธ๋ฆญ์ค๋ฅผ ๋ฐ๋์ ํ์ธํ์ญ์์ค.
- ์ต์ KMP ํ๋ก์ ํธ๋
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์