Kotlin Multiplatform (KMP)
欢迎来到 Kotlin Multiplatform 知识库!
🔷 KMP 简介
Kotlin Multiplatform (KMP) 是 JetBrains 和 Google 共同推广的跨平台解决方案。不同于 Flutter 和 React Native,KMP 专注于共享业务逻辑,而 UI 层可以使用各平台的原生技术(SwiftUI、Jetpack Compose 等),实现"逻辑一次编写,UI 原生体验"。
🎯 核心理念
KMP 的独特之处
传统跨平台(Flutter/RN):
└─ UI + 逻辑全部共享
└─ 优点:开发快
└─ 缺点:UI 受限
KMP 方式:
├─ 共享:业务逻辑、网络、数据库
└─ 原生:UI(SwiftUI、Jetpack Compose)
└─ 优点:UI 完全原生
└─ 缺点:UI 需要写两遍适用场景
✅ 适合 KMP:
- 业务逻辑复杂的应用
- 需要完全原生 UI 体验
- 已有原生团队
- 追求极致性能
❌ 不适合 KMP:
- 快速原型开发
- 小团队快速交付
- UI 为主的应用
🎯 学习路线
第一阶段:Kotlin 基础 (1-2个月)
Kotlin 语言
- 基础语法
- 协程 (Coroutines)
- Flow
了解平台
- iOS 开发基础
- Android 开发基础
第二阶段:KMP 入门 (1-2个月)
共享代码
- expect/actual 机制
- 共享模块创建
- 平台特定代码
常用库
- Ktor(网络)
- SQLDelight(数据库)
- Koin(依赖注入)
第三阶段:实战应用 (2-3个月)
架构设计
- 共享 ViewModel
- 数据层共享
- 业务逻辑共享
Compose Multiplatform
- 跨平台 UI(可选)
- iOS、Android、Desktop 共享 UI
✨ KMP 特点
✅ 核心优势
- 真正的原生 - UI 100% 原生,性能最佳
- 渐进式采用 - 可以逐步迁移现有项目
- Kotlin 生态 - 利用 Kotlin 的强大特性
- Google 背书 - Google 官方支持
- 未来趋势 - Google 大力推广
⚠️ 注意事项
- UI 需要写两遍(iOS 和 Android)
- 学习曲线较陡峭
- 生态相对较新
- iOS 开发需要 Mac
📊 与其他框架对比
| 特性 | KMP | Flutter | React Native |
|---|---|---|---|
| UI | ✅ 原生 | ❌ 自绘 | ⚠️ 桥接 |
| 性能 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| 开发速度 | 🟡 中 | 🟢 快 | 🟢 快 |
| 学习成本 | 🔴 高 | 🟡 中 | 🟢 低 |
| 代码共享 | 逻辑层 | UI+逻辑 | UI+逻辑 |
| 适合团队 | 原生团队 | 新项目 | Web 团队 |
🏗️ 项目结构
my-kmp-app/
├── shared/ # 共享模块
│ ├── src/
│ │ ├── commonMain/ # 共享代码
│ │ │ ├── kotlin/
│ │ │ │ ├── data/
│ │ │ │ │ ├── repository/
│ │ │ │ │ ├── api/
│ │ │ │ │ └── model/
│ │ │ │ ├── domain/
│ │ │ │ │ └── usecase/
│ │ │ │ └── util/
│ │ ├── androidMain/ # Android 特定
│ │ │ └── kotlin/
│ │ ├── iosMain/ # iOS 特定
│ │ │ └── kotlin/
│ │ └── commonTest/ # 共享测试
│ └── build.gradle.kts
├── androidApp/ # Android 应用
│ ├── src/main/
│ │ └── kotlin/
│ │ └── ui/ # Jetpack Compose UI
│ └── build.gradle.kts
├── iosApp/ # iOS 应用
│ └── iosApp/
│ ├── ContentView.swift # SwiftUI UI
│ └── iOSApp.swift
└── build.gradle.kts🔧 核心概念
expect/actual 机制
共享代码中声明期望的平台特定功能:
kotlin
// commonMain
expect class Platform {
val name: String
}
expect fun getPlatform(): Platform各平台提供实际实现:
kotlin
// androidMain
actual class Platform {
actual val name: String = "Android"
}
actual fun getPlatform(): Platform = Platform()
// iosMain
actual class Platform {
actual val name: String = "iOS"
}
actual fun getPlatform(): Platform = Platform()📖 学习资源
官方资源
推荐课程
常用库
- Ktor - 网络请求
- SQLDelight - 数据库
- Koin - 依赖注入
- kotlinx.serialization - JSON 序列化
- kotlinx.coroutines - 协程
🎯 开始学习
选择一个方向开始深入学习:
- 👉 Kotlin 语法 - 掌握 Kotlin 语言
- 👉 共享代码 - 学习 expect/actual
- 👉 Compose Multiplatform - 跨平台 UI(可选)
💡 学习建议
- 先学 Kotlin - KMP 基于 Kotlin
- 了解原生开发 - 至少了解 iOS 或 Android
- 从小项目开始 - 先共享简单的业务逻辑
- 关注官方 - KMP 发展很快
- 加入社区 - Kotlin Slack、Reddit
🚀 第一个 KMP 项目
创建项目
使用 KMP Wizard:
bash
# 访问 https://kmp.jetbrains.com/
# 或使用 Android Studio / IntelliJ IDEA 的 KMP 插件共享代码示例
kotlin
// commonMain/kotlin/Greeting.kt
class Greeting {
private val platform: Platform = getPlatform()
fun greet(): String {
return "Hello, ${platform.name}!"
}
}
expect class Platform {
val name: String
}
expect fun getPlatform(): Platformkotlin
// androidMain/kotlin/Platform.android.kt
actual class Platform {
actual val name: String = "Android ${android.os.Build.VERSION.SDK_INT}"
}
actual fun getPlatform(): Platform = Platform()kotlin
// iosMain/kotlin/Platform.ios.kt
import platform.UIKit.UIDevice
actual class Platform {
actual val name: String =
UIDevice.currentDevice.systemName() + " " +
UIDevice.currentDevice.systemVersion
}
actual fun getPlatform(): Platform = Platform()Android 使用
kotlin
// androidApp/MainActivity.kt
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Text(text = Greeting().greet())
}
}
}iOS 使用
swift
// iosApp/ContentView.swift
import shared
struct ContentView: View {
let greeting = Greeting().greet()
var body: some View {
Text(greeting)
}
}你的第一个 KMP 项目完成了!🎉
准备好了吗?让我们用 Kotlin 实现真正的跨平台!