概述

Kotlin Multiplatform(KMP)是 JetBrains 推出的跨平台开发解决方案,采用"共享代码、原生实现"的架构理念。与传统的"一次编写,到处运行"虚拟机方案不同,KMP 通过编译器将共享代码分别编译为目标平台的原生代码,实现了代码复用性原生性能的完美平衡。

核心特性

特性 说明
编译模式 静态编译为各平台原生代码(JVM 字节码、ARM64、JS 等)
代码复用 业务逻辑复用率可达 70%+
性能表现 无虚拟机开销,直接访问平台原生 API
类型安全 编译期类型检查,跨平台类型一致性保证
渐进迁移 可与现有原生项目无缝集成

一、KMP 工作原理深度解析

1.1 架构全景

运行时层

编译层

开发层

Kotlin 源码

commonMain 共享代码

androidMain 平台代码

iosMain 平台代码

jvmMain/WebMain 其他平台

Kotlin/JVM 编译器

Kotlin/JVM 编译器

Kotlin/Native 编译器

Kotlin/JS 编译器

Android APK
JVM 字节码

iOS Framework
ARM64 二进制

Web 应用
JavaScript/WASM

1.2 编译流程详解

目标平台 Kotlin 编译器 Gradle 构建 开发者 目标平台 Kotlin 编译器 Gradle 构建 开发者 通用代码编译阶段 平台特定编译阶段 触发构建任务 分析 Source Set 依赖关系 解析 commonMain 生成平台无关 IR (中间表示) 链接 androidMain/iosMain expect/actual 机制解析 平台 API 绑定 生成平台原生产物 APK/Framework/可执行文件

1.3 Source Set 依赖层级

共享层

平台聚合层

平台特定层

androidMain

androidTest

iosMain

iosTest

appleMain

iosSimulatorArm64

iosArm64

iosX64

commonMain

jvmMain

webMain

commonTest


二、快速上手实战

2.1 项目结构模板

kotlin-multiplatform-project/
├── gradle/                          # Gradle 配置
│   ├── libs.versions.toml          # 版本目录
│   └── wrapper/
├── shared/                         # 共享模块 
│   ├── build.gradle.kts            # KMP 插件配置
│   └── src/
│       ├── commonMain/             #  通用代码
│       │   └── kotlin/
│       │       ├── domain/         # 业务逻辑
│       │       ├── data/           # 数据层
│       │       └── di/             # 依赖注入
│       ├── androidMain/            #  Android 特定
│       ├── iosMain/                #  iOS 特定
│       └── commonTest/             #  通用测试
├── androidApp/                     # Android 应用
│   └── src/main/
│       └── AndroidManifest.xml
└── iosApp/                         # iOS 应用 (Xcode 项目)
    └── iosApp/

2.2 Gradle 配置详解

// shared/build.gradle.kts
plugins {
    kotlin("multiplatform")
    id("com.android.library")
    kotlin("native.cocoapods")
    id("org.jetbrains.compose")
}
kotlin {
    // 目标平台配置
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "17"
            }
        }
    }
    
    // iOS 目标配置
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "Shared"
            isStatic = true  // 静态库(推荐)
            
            // 导出依赖以便 Swift 访问
            export("dev.icerock.moko:resources:0.23.0")
        }
    }
    // CocoaPods 集成
    cocoapods {
        summary = "Kotlin Multiplatform Shared Module"
        homepage = "https://github.com/your-org/project"
        version = "1.0"
        ios.deploymentTarget = "14.0"
        
        podfile = project.file("../iosApp/Podfile")
        
        framework {
            baseName = "shared"
            isStatic = true
        }
        
        // 依赖原生 Pod
        pod("Alamofire") {
            version = "~> 5.8"
        }
    }
    // 源码集配置
    sourceSets {
        val commonMain by getting {
            dependencies {
                // 核心库
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
                
                // 网络层
                implementation("io.ktor:ktor-client-core:2.3.10")
                implementation("io.ktor:ktor-client-content-negotiation:2.3.10")
                implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.10")
                
                // 依赖注入
                implementation("io.insert-koin:koin-core:3.5.6")
                
                // UI 资源
                implementation("dev.icerock.moko:resources:0.23.0")
                
                // Compose Multiplatform
                implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material3)
            }
        }
        
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.1")
            }
        }
        
        val androidMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-okhttp:2.3.10")
                implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
            }
        }
        
        val iosMain by creating {
            dependsOn(commonMain)
            dependencies {
                implementation("io.ktor:ktor-client-darwin:2.3.10")
            }
        }
        
        // 为所有 iOS 目标共享 iosMain
        listOf(
            iosX64(),
            iosArm64(),
            iosSimulatorArm64()
        ).forEach {
            getByName("${it.targetName}Main") {
                dependsOn(iosMain)
            }
        }
    }
}
android {
    namespace = "com.example.shared"
    compileSdk = 34
    
    defaultConfig {
        minSdk = 24
    }
    
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
}

2.3 expect/actual 机制深入

缺失

完整

commonMain expect 声明

编译器检查

❌ 编译失败

链接实际实现

androidMain actual

iosMain actual

jvmMain actual

生成平台特定字节码

生成 ARM64 二进制

生成 JVM 字节码

expect/actual 最佳实践:

// commonMain/kotlin/platform/Platform.kt
/**
 * 平台信息接口
 * 使用 sealed class 保证类型安全
 */
expect sealed class PlatformType {
    val displayName: String
}
expect fun getCurrentPlatform(): PlatformType
// 业务代码可直接使用
class PlatformInfoProvider {
    fun getFormattedInfo(): String {
        val platform = getCurrentPlatform()
        return "Running on ${platform.displayName} | Kotlin ${KotlinVersion.CURRENT}"
    }
}
// 平台特定实现
// androidMain/kotlin/platform/Platform.android.kt
actual sealed class PlatformType {
    actual val displayName: String
}
actual data class AndroidPlatform(
    val apiLevel: Int
) : PlatformType() {
    actual val displayName: String = "Android $apiLevel"
}
actual fun getCurrentPlatform(): PlatformType {
    return AndroidPlatform(android.os.Build.VERSION.SDK_INT)
}
// iosMain/kotlin/platform/Platform.ios.kt
import platform.UIKit.UIDevice
import platform.Foundation.NSProcessInfo
actual sealed class PlatformType {
    actual val displayName: String
}
actual data class IOSPlatform(
    val systemVersion: String,
    val deviceModel: String
) : PlatformType() {
    actual val displayName: String = "iOS $systemVersion ($deviceModel)"
}
actual fun getCurrentPlatform(): PlatformType {
    val device = UIDevice.currentDevice
    return IOSPlatform(
        systemVersion = device.systemName,
        deviceModel = device.model
    )
}

三、架构设计与最佳实践

3.1 推荐分层架构

基础设施层 Mixed

平台适配层 Platform-Specific

数据层 Platform-Independent

业务逻辑层 Platform-Independent

表现层 Platform-Specific

Android Jetpack Compose

iOS SwiftUI

Desktop Compose

Web Compose HTML

Use Cases

Domain Models

Business Rules

Repository 接口

Data Transfer Objects

Android Repository Impl

iOS Repository Impl

Network Adapters

Storage Adapters

跨平台库 Ktor/SQLDelight

平台 SDK Android SDK/UIKit

3.2 依赖注入配置

// commonMain/kotlin/di/AppModule.kt
import org.koin.core.module.Module
import org.koin.dsl.module
val sharedModule: Module = module {
    // 单例
    single<PlatformService> { PlatformServiceImpl() }
    
    // 工厂
    factory<UseCase> { (params: UseCaseParams) ->
        UseCaseImpl(get(), params)
    }
    
    // Repository
    single<UserRepository> { UserRepositoryImpl(get(), get()) }
}
// commonMain/kotlin/di/RepositoryModule.kt
expect val repositoryModule: Module
// androidMain/kotlin/di/RepositoryModule.android.kt
actual val repositoryModule = module {
    single { provideOkHttpClient() }
    single { provideAndroidDatabase(get()) }
    single<UserRepository> { 
        AndroidUserRepository(get(), get()) 
    }
}
// iosMain/kotlin/di/RepositoryModule.ios.kt
actual val repositoryModule = module {
    single { provideDarwinClient() }
    single { provideIOSDatabase() }
    single<UserRepository> { 
        IOSUserRepository(get(), get()) 
    }
}

3.3 跨平台数据流设计

Local Cache API Service Repository Use Case ViewModel UI 层 Local Cache API Service Repository Use Case ViewModel UI 层 alt [缓存命中] [缓存未命中] 所有层级使用 Kotlin Flow 统一响应式编程模型 调用 loadData() execute(UseCaseParams) getData(id) query(id) Cached Data Flow<Data> fetch(id) Network Response save(response) Flow<Data> StateFlow<UiState> observe uiState

四、核心技术深度剖析

4.1 Kotlin/Native 内存模型

新内存模型 Kotlin 2.0+

多线程访问

引用计数

垃圾回收

性能优化

旧内存模型 Kotlin 1.x

Shared Heap

对象冻结

线程隔离

性能开销大

关键改进:

  • 移除冻结机制:对象可在多线程间自由传递
  • 引用计数 + GC:混合内存管理策略
  • Work-stealing 调度器:更高效的协程调度

4.2 序列化策略

// commonMain/kotlin/serialization/Serializer.kt
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
@Serializable
data class User(
    val id: String,
    val name: String,
    val email: String,
    @Contextual val createdAt: Instant
)
class JsonSerializer {
    private val json = Json {
        ignoreUnknownKeys = true
        isLenient = true
        encodeDefaults = true
        prettyPrint = false
    }
    
    fun <T : @Serializable Any> serialize(obj: T): String =
        json.encodeToString(obj)
    
    inline fun <reified T : @Serializable Any> deserialize(
        jsonStr: String
    ): T = json.decodeFromString(jsonStr)
}
// 处理平台特定类型
// commonMain/kotlin/serialization/PlatformSerializers.kt
expect object InstantSerializer : KSerializer<Instant>

4.3 网络层架构

Cross-Platform

Network Engine

Data Layer

Domain Layer

Repository Interface

RemoteDataSource

LocalDataSource

OkHttp Engine
Android

Darwin Engine
iOS

CIO Engine
JVM

Ktor Client Core

Content Negotiation

Serialization

Logging

// commonMain/kotlin/network/HttpClient.kt
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
class ApiClient {
    val client = HttpClient {
        install(ContentNegotiation) {
            json(Json {
                ignoreUnknownKeys = true
                isLenient = true
            })
        }
        
        install(Logging) {
            level = LogLevel.INFO
        }
        
        // 平台特定配置
        expect {
            installPlatformSpecificConfig()
        }
    }
    
    // expect 函数供平台自定义
    expect fun HttpClientConfig<*>.installPlatformSpecificConfig()
    
    suspend inline fun <reified T> get(
        path: String,
        params: Map<String, Any?> = emptyMap()
    ): T = client.get(path) {
        params.forEach { (key, value) ->
            parameter(key, value)
        }
    }.body()
}
// androidMain/kotlin/network/HttpClient.android.kt
import io.ktor.client.engine.okhttp.OkHttp
actual fun HttpClientConfig<*>.installPlatformSpecificConfig() {
    engine {
        OkHttp {
            config {
                connectTimeout(30, TimeUnit.SECONDS)
                readTimeout(30, TimeUnit.SECONDS)
            }
        }
    }
}
// iosMain/kotlin/network/HttpClient.ios.kt
import io.ktor.client.engine.darwin.Darwin
actual fun HttpClientConfig<*>.installPlatformSpecificConfig() {
    engine {
        Darwin {
            configureRequest {
                setTimeoutInterval(30.0)
            }
        }
    }
}

五、性能优化策略

5.1 构建优化

源码

增量编译?

编译变更模块

全量编译

缓存命中

并行编译

产物生成

优化配置:

// gradle.properties
kotlin.code.style=official
kotlin.incremental=true
kotlin.caching.enabled=true
kotlin.parallel.tasks.in.project=true
# Native 优化
kotlin.native.cacheKind=static
kotlin.native.useMemoryCache=true
kotlin.native.optimizationMode=opt

5.2 运行时优化

优化项 策略 预期效果
二进制体积 启用 DCE (Dead Code Elimination) 减少 30-50%
启动速度 延迟初始化 + 懒加载 提升 20-40%
内存占用 对象池 + 值类型 减少 15-25%
网络性能 连接池 + 响应缓存 提升 25-35%

六、调试与测试

6.1 测试金字塔

Platform Tests 平台特定

Shared Tests 跨平台

E2E Tests 平台特定

UI Automation Tests

Integration Tests

Unit Tests
commonTest

Use Case Tests

Repository Tests

Android Instrumented Tests

iOS XCTests

6.2 跨平台测试示例

// commonTest/kotlin/repository/UserRepositoryTest.kt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlinx.coroutines.test.runTest
import io.mockk.every
import io.mockk.mockk
class UserRepositoryTest {
    
    private val mockApi = mockk<ApiService>()
    private val mockCache = mockk<CacheDataSource>()
    private val repository = UserRepositoryImpl(mockApi, mockCache)
    
    @Test
    fun `should return user from cache when available`() = runTest {
        // Given
        val expectedUser = User(id = "1", name = "Test")
        every { mockCache.getUser("1") } returns expectedUser
        
        // When
        val result = repository.getUser("1")
        
        // Then
        assertEquals(expectedUser, result)
        verify(exactly = 0) { mockApi.fetchUser("1") }
    }
    
    @Test
    fun `should fetch from api when cache empty`() = runTest {
        // Given
        val expectedUser = User(id = "1", name = "Test")
        every { mockCache.getUser("1") } returns null
        every { mockApi.fetchUser("1") } returns expectedUser
        every { mockCache.saveUser(expectedUser) } just Runs
        
        // When
        val result = repository.getUser("1")
        
        // Then
        assertEquals(expectedUser, result)
        verify { mockApi.fetchUser("1") }
        verify { mockCache.saveUser(expectedUser) }
    }
}

七、未来展望与生态发展

7.1 技术路线图

2024-04-01 2024-07-01 2024-10-01 2025-01-01 2025-04-01 2025-07-01 2025-10-01 2026-01-01 2026-04-01 2026-07-01 2026-10-01 Compose Multiplatform Stable Wasm Compose Support 新内存模型稳定版 Kotlin 2.1 发布 Compose iOS 原生渲染 KSP 全面支持 WASI 原生支持 2024 2025 2026+ KMP 发展路线图

7.2 生态成熟度评估

类别 成熟度 代表库
核心工具链 ⭐⭐⭐⭐⭐ Kotlin Compiler, Gradle Plugin
网络通信 ⭐⭐⭐⭐⭐ Ktor Client, Apollo GraphQL
数据持久化 ⭐⭐⭐⭐⭐ SQLDelight, Realm, Multiplatform Settings
依赖注入 ⭐⭐⭐⭐ Koin Multiplatform, Kodein-DI
UI 框架 ⭐⭐⭐⭐ Compose Multiplatform, Moko UI
测试工具 ⭐⭐⭐⭐ Kotest, Turbine, Mockk
序列化 ⭐⭐⭐⭐⭐ kotlinx.serialization
异步编程 ⭐⭐⭐⭐⭐ kotlinx.coroutines

八、决策指南:何时选择 KMP?

8.1 适用场景决策树

简单

中等/复杂

Kotlin 经验丰富

混合技能

一般

Android + iOS

多平台

项目启动

业务逻辑复杂度

考虑跨平台框架
Flutter/React Native

团队技能储备

推荐 KMP

性能要求

考虑原生开发
或评估其他方案

平台目标

KMM 最佳选择

KMP 全平台方案

8.2 投资回报分析

指标 原生开发 KMP 开发 ROI 改善
开发成本 100% 65% -35%
维护成本 100% 50% -50%
代码一致性 基准 +40% 更高
发布效率 基准 +30% 更快
Bug 修复 2x 工作量 1x 工作量 -50%

九、学习资源与社区支持

9.1 推荐学习路径

语言基础
Kotlin 语法

KMP 核心概念
expect/actual

工具链掌握
Gradle 配置

架构设计
分层架构

高级特性
Compose Multiplatform

性能优化
Native 调优

9.2 资源清单

官方资源:

结语

Kotlin Multiplatform 代表了跨平台开发的第三代演进——既不像 Java 虚拟机那样牺牲性能,也不像第一代跨平台框架那样受限于 JavaScript 生态。通过编译器层面的创新,KMP 实现了真正的代码复用与原生性能的统一
随着 Compose Multiplatform 的成熟、新内存模型的稳定,以及 Kotlin 生态的持续繁荣,KMP 正在成为构建跨平台应用的战略选择。对于已有 Kotlin 技术储备的团队,现在正是深入学习和实践 KMP 的最佳时机。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐