目录
- [Android 原生开发](#Android 原生开发)
- [Kotlin Multiplatform (KMP) 跨平台](#Kotlin Multiplatform (KMP) 跨平台)
- 后端开发与服务端
- 桌面应用开发
- [Web 前端开发](#Web 前端开发)
- [iOS 开发](#iOS 开发)
- 数据科学与机器学习
- 物联网与嵌入式
- 自动化测试
- [构建工具与 DevOps](#构建工具与 DevOps)
- 企业级应用与集成
- 安全与密码学
- 快速选型指南
- [2024-2025 热门技术组合](#2024-2025 热门技术组合)
一. Android 原生开发
核心框架与架构
| 技术 |
用途 |
关键库 |
| Jetpack Compose |
现代声明式 UI 框架 |
androidx.compose.ui, androidx.compose.material3 |
| ViewModel |
生命周期感知的数据管理 |
androidx.lifecycle:lifecycle-viewmodel-compose |
| Room |
本地数据库 ORM |
androidx.room:room-ktx, androidx.room:room-compiler |
| DataStore |
类型安全数据存储 |
androidx.datastore:datastore-preferences |
| Navigation |
页面导航组件 |
androidx.navigation:navigation-compose |
| WorkManager |
后台任务调度 |
androidx.work:work-runtime-ktx |
| Hilt |
依赖注入 |
com.google.dagger:hilt-android, hilt-compiler |
| Paging |
分页加载 |
androidx.paging:paging-runtime-ktx |
网络与数据
// Retrofit + OkHttp + Kotlin Serialization
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
// Coil - 图片加载
implementation("io.coil-kt:coil-compose:2.6.0")
// Apollo - GraphQL
implementation("com.apollographql.apollo3:apollo-runtime:3.8.2")
异步与响应式
| 库 |
用途 |
kotlinx.coroutines |
Kotlin 官方协程库 |
kotlinx.coroutines-android |
Android 主线程调度器 |
kotlinx.coroutines-play-services |
Google Play 服务集成 |
ReactiveX/RxKotlin |
RxJava 的 Kotlin 封装 |
Flow |
Kotlin 原生响应式流(推荐) |
核心概念
KMP 允许在 Android、iOS、桌面 (JVM)、Web (JS/Wasm) 之间共享代码,同时保持原生性能 ^22^^26^。
项目结构
project/
├── shared/ # 共享模块
│ ├── src/
│ │ ├── commonMain/ # 平台无关代码(业务逻辑、数据层)
│ │ ├── androidMain/ # Android 平台实现
│ │ ├── iosMain/ # iOS 平台实现
│ │ ├── jvmMain/ # JVM 桌面实现
│ │ └── jsMain/ # Web 前端实现
│ └── build.gradle.kts
├── composeApp/ # Compose Multiplatform UI
├── androidApp/ # Android 应用入口
├── iosApp/ # iOS 应用入口(Xcode 项目)
└── desktopApp/ # 桌面应用入口
KMP 核心库生态 ^22^^30^
| 库名 |
功能 |
支持平台 |
| Ktor Client |
HTTP 客户端 |
Android, iOS, JVM, JS, Native |
| SQLDelight |
类型安全 SQL 数据库 |
Android, iOS, JVM, JS, Native |
| kotlinx.serialization |
序列化/反序列化 |
全平台 |
| kotlinx.coroutines |
协程 |
全平台 |
| kotlinx.datetime |
日期时间 API |
全平台 |
| Koin |
依赖注入 |
Android, iOS, JVM |
| Napier |
日志库 |
全平台 |
| Multiplatform Settings |
键值存储 |
Android, iOS, JVM, JS |
| KStore |
数据流存储 |
Android, iOS, JVM |
| Okio |
文件 I/O |
全平台 |
| Apollo Kotlin |
GraphQL 客户端 |
全平台 |
// build.gradle.kts (shared module)
plugins {
kotlin("multiplatform")
id("org.jetbrains.compose") version "1.8.0" // iOS 稳定版
}
kotlin {
androidTarget()
iosX64()
iosArm64()
iosSimulatorArm64()
jvm("desktop")
sourceSets {
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material3)
implementation(compose.ui)
implementation(compose.components.resources)
}
}
}
}
平台互操作 ^37^
| 目标平台 |
互操作方式 |
工具 |
| iOS (Swift/Objective-C) |
expect/actual + 导出框架 |
SKIE 工具优化 Swift 互操作 |
| Android |
直接调用 Android SDK |
- |
| JVM |
调用 Java 库 |
100% Java 互操作 |
| JavaScript |
TypeScript 声明文件 |
kotlin-wrappers |
| WebAssembly |
Wasm 模块导出 |
Kotlin/Wasm (Alpha) |
三. 后端开发与服务端
Web 框架 ^28^^31^
| 框架 |
特点 |
核心库 |
| Ktor |
JetBrains 官方、协程原生、轻量灵活 |
io.ktor:ktor-server-core, ktor-server-netty, ktor-server-content-negotiation |
| Spring Boot |
企业级标准、生态丰富 |
spring-boot-starter-web, spring-boot-starter-data-jpa |
| Quarkus |
云原生、GraalVM 原生编译 |
quarkus-resteasy-reactive, quarkus-kotlin |
| Micronaut |
AOT 编译、低内存、微服务 |
micronaut-runtime, micronaut-http-server-netty |
| Vert.x |
响应式、事件驱动 |
vertx-core, vertx-lang-kotlin |
| http4k |
纯函数式、轻量级 |
org.http4k:http4k-core |
| Javalin |
极简 API、WebSocket 支持 |
io.javalin:javalin |
Ktor 生态详解 ^31^
// build.gradle.kts
plugins {
kotlin("jvm") version "2.1.0"
id("io.ktor.plugin") version "2.3.12"
kotlin("plugin.serialization") version "2.1.0"
}
dependencies {
// 核心服务器
implementation("io.ktor:ktor-server-core-jvm")
implementation("io.ktor:ktor-server-netty-jvm")
// 内容协商与序列化
implementation("io.ktor:ktor-server-content-negotiation-jvm")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm")
// 路由与模板
implementation("io.ktor:ktor-server-resources")
implementation("io.ktor:ktor-server-html-builder")
// 安全与认证
implementation("io.ktor:ktor-server-auth-jvm")
implementation("io.ktor:ktor-server-auth-jwt-jvm")
// 监控与指标
implementation("io.ktor:ktor-server-metrics-micrometer")
implementation("io.micrometer:micrometer-registry-prometheus")
// 测试
testImplementation("io.ktor:ktor-server-test-host")
testImplementation("org.jetbrains.kotlin:kotlin-test")
}
数据库与持久层
| 类型 |
库 |
说明 |
| ORM |
Exposed (JetBrains 官方) |
类型安全 SQL DSL,支持 R2DBC ^31^ |
| ORM |
KTorm |
轻量级 ORM |
| JPA |
Hibernate + Spring Data JPA |
传统方案 |
| SQL 生成 |
jOOQ |
类型安全 SQL |
| NoSQL |
MongoDB Kotlin Driver, Redis Lettuce |
异步驱动 |
| Reactive |
R2DBC, jasync-sql |
响应式数据库 |
Exposed 1.0 示例 ^31^
// 类型安全 SQL DSL
object Users : Table("users") {
val id = integer("id").autoIncrement()
val name = varchar("name", 50)
val email = varchar("email", 255).uniqueIndex()
override val primaryKey = PrimaryKey(id)
}
// 使用
val user = Users.select { Users.email eq "user@example.com" }
.singleOrNull()
四. 桌面应用开发
技术方案
| 方案 |
框架 |
特点 |
适用场景 |
| Compose for Desktop |
org.jetbrains.compose |
跨平台、声明式 UI、共享 Android 代码 |
现代跨平台桌面应用 ^42^ |
| TornadoFX |
no.tornado:tornadofx |
JavaFX 的 Kotlin DSL 封装 |
传统桌面应用 |
| Swing + Kotlin |
标准库 |
成熟稳定、组件丰富 |
企业级工具 |
Compose for Desktop 核心库
// build.gradle.kts
plugins {
kotlin("jvm")
id("org.jetbrains.compose") version "1.8.0"
}
dependencies {
implementation(compose.desktop.currentOs)
implementation(compose.material3)
implementation(compose.materialIconsExtended)
// 窗口管理
implementation("org.jetbrains.compose.ui:ui-util-desktop")
// 系统托盘
implementation("org.jetbrains.compose.ui:ui-awt")
// 文件拖拽
implementation("org.jetbrains.compose.ui:ui-desktop")
}
// 打包配置
compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "MyApp"
packageVersion = "1.0.0"
}
}
}
桌面特定功能
| 功能 |
库 |
| 系统托盘 |
compose.ui:ui-awt |
| 通知 |
java.awt.Desktop 或第三方库 |
| 文件选择器 |
JFileChooser 或 Compose 自定义 |
| 全局快捷键 |
JNativeHook |
| 自动更新 |
update4j |
五. Web 前端开发
Kotlin/JS 与 Kotlin/Wasm
| 目标 |
技术 |
状态 |
| JavaScript 编译 |
kotlin-js |
稳定,生产可用 |
| WebAssembly |
kotlin-wasm |
Alpha,高性能 ^32^ |
前端框架集成
// React 集成
implementation("org.jetbrains.kotlin-wrappers:kotlin-react:18.2.0-pre.346")
implementation("org.jetbrains.kotlin-wrappers:kotlin-react-dom:18.2.0-pre.346")
implementation("org.jetbrains.kotlin-wrappers:kotlin-emotion:11.10.6-pre.346")
// Compose HTML (Kotlin DSL 生成 HTML)
implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.9.1")
Ktor 服务端渲染 ^28^
// kotlinx.html DSL
fun HTML.index() {
head {
title("Welcome")
}
body {
h1 { +"Hello from Kotlin" }
div {
id = "root"
+"Loading..."
}
script(src = "/static/main.js") {}
}
}
六. iOS 开发
KMP 在 iOS 的应用 ^22^^37^
KMP 编译为 iOS 原生二进制,通过 Kotlin/Native 实现与 Swift/Objective-C 的互操作。
架构模式
iOS App (Xcode)
├── UI Layer (SwiftUI / UIKit) <- 平台原生
├── ViewModel (Swift) <- 可选项
└── Shared Framework (Kotlin) <- KMP 编译
├── Domain Logic (Kotlin)
├── Data Layer (Kotlin)
└── Network (Ktor Client)
Swift 互操作工具
| 工具 |
用途 |
| SKIE |
优化 Swift API 导出,支持协程、Flow、密封类 ^37^ |
| Kotlin/Native |
编译为 Framework,自动生成 Objective-C 头文件 |
| CocoaPods/SPM |
依赖管理集成 |
配置示例
// build.gradle.kts
kotlin {
iosTarget {
binaries.framework {
baseName = "Shared"
export("dev.icerock.moko:mvvm-core:0.16.1") // 导出给 Swift
}
}
}
// 使用 SKIE 优化导出
plugins {
id("co.touchlab.skie") version "0.8.0"
}
七. 数据科学与机器学习
数据处理与分析 ^42^
| 库 |
用途 |
特点 |
| Kotlin DataFrame |
表格数据处理 |
类似 pandas,类型安全,JetBrains 官方 ^42^ |
| KotlinDL |
深度学习 |
Deeplearning4j 的 Kotlin API,支持 CNN、RNN ^42^ |
| Koma |
科学计算 |
类似 NumPy,矩阵运算 |
| Multik |
多维数组 |
Kotlin 原生 NumPy 替代 |
| Krangl |
数据操作 |
受 R dplyr 启发 |
| Smile |
机器学习 |
Java 库,Kotlin 友好 API |
Kotlin DataFrame 示例
import org.jetbrains.kotlinx.dataframe.api.*
// 读取数据
val df = DataFrame.read("data.csv")
// 操作
val summary = df
.groupBy { city }
.aggregate {
mean { age } into "avg_age"
count() into "count"
}
.sortByDesc("count")
深度学习
// KotlinDL
import org.jetbrains.kotlinx.dl.api.core.*
import org.jetbrains.kotlinx.dl.api.core.layer.*
val model = Sequential.of(
Input(28, 28, 1),
Flatten(),
Dense(128, Activation.ReLU),
Dense(10, Activation.Softmax)
)
model.compile(
optimizer = Adam(),
loss = Losses.SOFT_MAX_CROSS_ENTROPY_WITH_LOGITS,
metric = Metrics.ACCURACY
)
Jupyter Notebook 支持
// 使用 Kotlin Jupyter 内核
%use dataframe
%use kotlin-dl
val data = DataFrame.read("sample.csv")
data.head(10)
八. 物联网与嵌入式
Kotlin/Native ^42^
编译为机器码,无需 JVM,支持嵌入式设备。
平台支持
| 平台 |
说明 |
| Linux (ARM/x86) |
完整支持 |
| Raspberry Pi |
通过 Pi4J 控制 GPIO |
| 微控制器 |
实验性支持,通过 LLVM |
| WebAssembly |
浏览器外 Wasm (WASI) |
嵌入式库
// Pi4J - Raspberry Pi GPIO
implementation("com.pi4j:pi4j-core:2.4.0")
implementation("com.pi4j:pi4j-plugin-raspberrypi:2.4.0")
implementation("com.pi4j:pi4j-plugin-pigpio:2.4.0")
// MQTT 物联网协议
implementation("org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5")
// 串口通信
implementation("com.fazecast:jSerialComm:2.10.4")
物联网平台集成
| 平台 |
SDK |
| AWS IoT |
aws-sdk-kotlin |
| Azure IoT |
azure-sdk-for-java (Kotlin 兼容) |
| ThingsBoard |
REST API |
九. 自动化测试
测试框架
| 类型 |
库 |
特点 |
| 单元测试 |
kotlin-test |
官方标准库 |
| BDD |
Kotest |
功能丰富,DSL 友好 |
| Mocking |
MockK |
Kotlin 原生 Mock,支持协程 |
| UI 测试 |
Compose UI Test |
Compose 专用 |
| Android UI |
Espresso |
Android 原生 |
| iOS UI |
XCTest (Swift) |
平台原生 |
| KMP 测试 |
kotlinx-coroutines-test |
多平台协程测试 |
Kotest 示例
// build.gradle.kts
testImplementation("io.kotest:kotest-runner-junit5:5.8.0")
testImplementation("io.kotest:kotest-assertions-core:5.8.0")
testImplementation("io.kotest:kotest-property:5.8.0") // 属性测试
// 测试代码
class MyTests : StringSpec({
"length should return size of string" {
"hello".length shouldBe 5
}
"startsWith should test for a prefix" {
"world".shouldStartWith("wor")
}
})
MockK 用法
testImplementation("io.mockk:mockk:1.13.9")
// 使用
val mock = mockk<UserRepository>()
coEvery { mock.getUser(any()) } returns User("test")
十. 构建工具与 DevOps
构建工具
| 工具 |
用途 |
特点 |
| Gradle |
官方构建工具 |
Kotlin DSL,灵活强大 |
| Amper |
JetBrains 新工具 |
声明式配置,简化 KMP 项目 ^30^^41^ |
| Maven |
传统构建 |
Kotlin 支持良好 |
Gradle Kotlin DSL
// build.gradle.kts (KMP 项目)
plugins {
kotlin("multiplatform") version "2.1.0"
id("com.android.library") version "8.2.0"
id("org.jetbrains.compose") version "1.8.0"
kotlin("plugin.serialization") version "2.1.0"
}
kotlin {
androidTarget()
iosX64()
iosArm64()
iosSimulatorArm64()
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
}
}
val androidMain by getting {
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
}
}
val iosMain by getting {
dependencies {
// iOS 特定依赖
}
}
}
}
CI/CD 与部署
| 场景 |
工具/库 |
| GitHub Actions |
官方 Action:gradle/gradle-build-action |
| GitLab CI |
Kotlin 原生镜像 |
| Docker |
多阶段构建,GraalVM 原生镜像 |
| Fastlane |
移动应用自动化部署 |
| Kotlin Script |
.main.kts 用于脚本化任务 |
十一. 企业级应用与集成
消息队列与事件驱动
| 技术 |
库 |
| Apache Kafka |
org.apache.kafka:kafka-clients |
| RabbitMQ |
com.rabbitmq:amqp-client |
| Redis Pub/Sub |
io.lettuce:lettuce-core |
| Pulsar |
org.apache.pulsar:pulsar-client |
工作流引擎
| 引擎 |
Kotlin 支持 |
| Camunda |
通过 Spring Boot 集成 |
| Flowable |
Java 库,Kotlin DSL 封装 |
| Temporal |
现代工作流,官方 SDK |
企业集成模式
// Apache Camel Kotlin DSL
camel {
route {
from("kafka:orders")
.filter { it.body != null }
.to("jdbc:orderDb")
.to("websocket://notifications")
}
}
十二. 安全与密码学
安全框架
| 技术 |
库 |
用途 |
| Ktor Auth |
ktor-server-auth |
认证与授权 |
| JWT |
io.jsonwebtoken:jjwt |
令牌处理 |
| OAuth2 |
ktor-server-auth-oauth |
第三方登录 |
| 密码哈希 |
org.mindrot:jbcrypt |
安全存储 |
密码学库
// BouncyCastle
implementation("org.bouncycastle:bcprov-jdk18on:1.77")
// Tink (Google)
implementation("com.google.crypto.tink:tink:1.12.0")
// Kotlin 原生
implementation("org.jetbrains.kotlinx:kotlinx-crypto:0.1.0")
Ktor 安全示例
fun Application.configureSecurity() {
authentication {
jwt("auth-jwt") {
realm = "Access to the app"
verifier(JWT.require(Algorithm.HMAC256("secret")).build())
validate { credential ->
if (credential.payload.getClaim("username").asString() != "") {
JWTPrincipal(credential.payload)
} else null
}
}
}
}
📋 快速选型指南
| 我想做... |
首选技术栈 |
| Android 原生应用 |
Jetpack Compose + Hilt + Room + Retrofit + Coroutines |
| iOS + Android 跨平台 |
KMP + Compose Multiplatform + Ktor + SQLDelight |
| 全平台应用 (含桌面/Web) |
KMP + Compose Multiplatform 1.8+ + KStore |
| 后端 API 服务 |
Ktor + Exposed + PostgreSQL + Prometheus |
| 企业级微服务 |
Spring Boot + Kotlin + JPA + Kafka |
| 云原生 Serverless |
Quarkus + GraalVM 原生编译 + AWS Lambda |
| 桌面工具软件 |
Compose for Desktop + Koin + SQLite |
| 数据科学分析 |
Kotlin DataFrame + KotlinDL + Jupyter |
| 物联网网关 |
Kotlin/Native + Pi4J + MQTT + InfluxDB |
| 实时聊天应用 |
Ktor + WebSocket + Redis + Coroutines |
| 电商/支付系统 |
Spring Boot + Kotlin + Exposed + Stripe |
| AI 推理服务 |
Ktor + KotlinDL + ONNX Runtime |
🎯 2024-2025 热门技术组合
现代跨平台应用 (KMP + Compose)
// build.gradle.kts (shared module)
plugins {
kotlin("multiplatform") version "2.1.0"
id("org.jetbrains.compose") version "1.8.0" // iOS 稳定
id("app.cash.sqldelight") version "2.0.2"
}
kotlin {
androidTarget()
iosX64()
iosArm64()
iosSimulatorArm64()
jvm("desktop")
sourceSets {
commonMain.dependencies {
// 协程与序列化
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
// 网络
implementation("io.ktor:ktor-client-core:2.3.12")
implementation("io.ktor:ktor-client-content-negotiation:2.3.12")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.12")
// 依赖注入
implementation("io.insert-koin:koin-core:3.5.3")
// 日志
implementation("io.github.aakira:napier:2.7.1")
// Compose
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material3)
implementation(compose.ui)
}
}
}
sqldelight {
databases {
create("AppDatabase") {
packageName.set("com.example.db")
}
}
}
云原生后端 (Ktor + 可观测性)
// build.gradle.kts
plugins {
kotlin("jvm") version "2.1.0"
id("io.ktor.plugin") version "2.3.12"
}
dependencies {
// Ktor 核心
implementation("io.ktor:ktor-server-core-jvm")
implementation("io.ktor:ktor-server-netty-jvm")
// 内容协商
implementation("io.ktor:ktor-server-content-negotiation-jvm")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm")
// 数据库 (R2DBC 非阻塞)
implementation("org.jetbrains.exposed:exposed-core:0.50.1")
implementation("org.jetbrains.exposed:exposed-dao:0.50.1")
implementation("org.jetbrains.exposed:exposed-jdbc:0.50.1")
implementation("org.jetbrains.exposed:exposed-r2dbc:0.50.1")
// 监控
implementation("io.ktor:ktor-server-metrics-micrometer-jvm")
implementation("io.micrometer:micrometer-registry-prometheus:1.12.0")
implementation("io.ktor:ktor-server-call-logging-jvm")
// 文档
implementation("io.ktor:ktor-server-openapi")
implementation("io.ktor:ktor-server-swagger-jvm")
}
AI 原生应用 (Kotlin + LLM)
// LangChain4j + Kotlin
dependencies {
implementation("dev.langchain4j:langchain4j:0.35.0")
implementation("dev.langchain4j:langchain4j-open-ai:0.35.0")
implementation("dev.langchain4j:langchain4j-embeddings:0.35.0")
// Ktor 集成
implementation("io.ktor:ktor-server-core")
implementation("io.ktor:ktor-client-core")
}
附录:Kotlin 2.1 新特性速查 ^32^
| 特性 |
说明 |
应用场景 |
| K2 编译器 |
统一前端架构,编译速度提升 2 倍 |
所有项目 |
| 守卫条件 |
when 表达式中嵌入布尔判断 |
复杂条件分支 |
| 上下文接收器 |
函数在特定上下文中调用 |
DSL 设计 |
| Wasm 支持 |
Kotlin/Wasm Alpha |
高性能 Web |
| Swift 导出 |
直接导出 Swift API (2025 年底) |
iOS 开发 |
| Amper 构建工具 |
声明式配置,简化 KMP |
新项目 |