3. 项目依赖配置:Gradle 声明与核心依赖解析
本章聚焦于为《Kotlin Flow缓存更新案例》搭建正确的Gradle依赖环境。依赖配置直接影响协程作用域、Flow背压策略、Room编译与Retrofit动态代理是否能在运行时正确协作。错误的版本组合或遗漏的编译器插件会导致难以追踪的构建失败或运行时崩溃。
3.1 依赖声明全景
本案例基于 Android Gradle Plugin (AGP) 8.x 与 Kotlin 1.9.x / 2.0.x 构建。所有依赖通过 libs.versions.toml 版本目录管理。
| 依赖组 | 坐标 (Artifact Coordinate) | 最小版本建议 | 用途 |
|---|---|---|---|
| Kotlin 协程 | org.jetbrains.kotlinx:kotlinx-coroutines-core |
1.8.0 | Flow 数据流、异步任务 |
| Kotlin 协程 (Android) | org.jetbrains.kotlinx:kotlinx-coroutines-android |
1.8.0 | Android 主线程调度 |
| Retrofit | com.squareup.retrofit2:retrofit |
2.9.0 | HTTP 网络客户端 |
| Retrofit (Gson 转换器) | com.squareup.retrofit2:converter-gson |
2.9.0 | JSON 序列化/反序列化 |
| Room Runtime | androidx.room:room-runtime |
2.6.1 | 本地数据库运行时 |
| Room KTX | androidx.room:room-ktx |
2.6.1 | Flow + 协程扩展 |
| Room Compiler (KSP) | androidx.room:room-compiler |
2.6.1 | 编译期 DAO 生成 |
| OkHttp 日志拦截器 | com.squareup.okhttp3:logging-interceptor |
4.12.0 | 调试网络请求 |
| Kotlin 标准库 | org.jetbrains.kotlin:kotlin-stdlib |
1.9.22 | 基础语言运行时 |
📌 版本对齐提醒:
Room 2.6.x 要求 Kotlin 最低版本为 1.8.0,推荐使用 1.9.22 以上。协程 1.8.0 支持 Kotlin 1.7.x~2.0.x,但 Flow + Room 组合在 1.9.x 上测试最充分。
Room 2.6.x 要求 Kotlin 最低版本为 1.8.0,推荐使用 1.9.22 以上。协程 1.8.0 支持 Kotlin 1.7.x~2.0.x,但 Flow + Room 组合在 1.9.x 上测试最充分。
3.2 核心依赖分解
3.2.1 Kotlin 协程与 Flow
Flow 是 kotlinx-coroutines-core 的一部分,不需要额外 artifact。但 Android 项目必须引入 kotlinx-coroutines-android 以获得 Dispatchers.Main 的正确实现。
// libs.versions.toml 片段
[versions]
kotlin = "1.9.22"
coroutines = "1.8.0"
[libraries]
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
3.2.2 Retrofit 与 OkHttp
Retrofit 默认使用 OkHttp 作为网络层。本案例需要 logging-interceptor 用于调试缓存决策。
[libraries]
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
retrofit-converter-gson = { module = "com.squareup.retrofit2:converter-gson", version.ref = "retrofit" }
okhttp-logging = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "okhttp" }
3.2.3 Room 与 KSP 编译器
Room 使用 KSP (Kotlin Symbol Processing) 替代 kapt 进行编译期 DAO 实现生成。KSP 版本必须严格匹配 Kotlin 版本。
[libraries]
room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
[plugins]
kotlin-ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
⚠️ KSP 版本陷阱:
KSP 1.9.22-1.0.17 仅匹配 Kotlin 1.9.22。如果使用 Kotlin 2.0.0,必须升级至 KSP 2.0.0-1.0.21 及以上。版本不匹配会导致 room-compiler 生成代码时抛出
KSP 1.9.22-1.0.17 仅匹配 Kotlin 1.9.22。如果使用 Kotlin 2.0.0,必须升级至 KSP 2.0.0-1.0.21 及以上。版本不匹配会导致 room-compiler 生成代码时抛出
SymbolProcessingException。
3.3 Gradle 构建脚本示例
以下为 app/build.gradle.kts 的完整依赖声明,包含本案例所需全部组件。
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.ksp)
}
android {
namespace = "com.example.flowcacheupdate"
compileSdk = 34
defaultConfig {
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.10"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
}
dependencies {
// Kotlin 标准库
implementation(libs.kotlin.stdlib)
// 协程 & Flow
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.coroutines.android)
// Retrofit + OkHttp
implementation(libs.retrofit)
implementation(libs.retrofit.converter.gson)
implementation(libs.okhttp.logging)
// Room
implementation(libs.room.runtime)
implementation(libs.room.ktx)
ksp(libs.room.compiler)
// ViewModel KTX (用于 viewModelScope)
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
// Jetpack Compose (可选,用于 UI 层收集 Flow)
implementation(platform("androidx.compose:compose-bom:2024.01.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.runtime:runtime")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.7.0")
}
3.4 常见错误与修复
| 错误类型 | 典型日志/现象 | 根因 | 解决方案 |
|---|---|---|---|
| Room 编译错误 | cannot find symbol class TaskDao_Impl |
KSP 未应用或 room-compiler 未通过 ksp 引入 | 确保 ksp(libs.room.compiler) 写入 dependencies 块 |
| 协程版本冲突 | Module was compiled with an incompatible version of kotlinx.coroutines |
compose 或 lifecycle 传递依赖了旧版协程 | 使用 dependency constraint 锁定所有传递依赖版本 |
| Retrofit 无法序列化 | java.lang.IllegalArgumentException: Unable to create converter for ... |
缺少 converter-gson 或未在 Retrofit.Builder 中注册 | 确保 addConverterFactory(GsonConverterFactory.create()) |
| KSP 执行失败 | e: [ksp] com.google.devtools.ksp.KotlinKspExtension cannot be cast to class |
KSP 插件版本与 Kotlin 插件不匹配 | 对照 Kotlin 官网的 KSP 版本映射表升级 |
🔧 传递依赖锁定示例:
在
在
app/build.gradle.kts 中添加 constraints 块,强制使用统一协程版本:
dependencies {
constraints {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0") {
because("统一协程版本避免运行时冲突")
}
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0")
}
// 其他依赖...
}
3.5 验证依赖配置
依赖声明完成后,通过以下方式验证配置正确性:
- Gradle 同步:确保 IDE 的 Gradle 面板显示
BUILD SUCCESSFUL in xs。 - KSP 任务可见性:在 Terminal 执行
./gradlew :app:kspDebugKotlin,应无错误退出。 - 依赖树检查:运行
./gradlew :app:dependencies --configuration debugRuntimeClasspath,确认协程、Retrofit、Room 版本符合预期。 - 最小化测试:在 Application 子类中创建一个空的
CoroutineScope(Dispatchers.IO).launch { },编译运行不崩溃。
✅ 快速对照检查清单:
- Kotlin 插件版本 == KSP 版本前缀
- room-compiler 只使用 ksp 配置,而非 implementation
- retrofit 与 converter-gson 版本对齐
- compileSdk >= 34,jvmTarget = "17"
- Kotlin 插件版本 == KSP 版本前缀
- room-compiler 只使用 ksp 配置,而非 implementation
- retrofit 与 converter-gson 版本对齐
- compileSdk >= 34,jvmTarget = "17"