1. 环境概览与依赖清单
Compose 夹具(Compose Fixture)为开发者提供了一种轻量级的 UI 测试基础设施,允许在不受完整 Activity 生命周期干扰的情况下,对单个 Composable 组件进行状态驱动和交互验证。本章将梳理搭建夹具环境所需的系统组件、各组件的最低及推荐版本、官方获取渠道,帮助你在开始编写测试用例前一次性完成依赖对齐。
1.1 核心组件一览
Compose 夹具的运行依赖以下四个关键层次:
- 构建系统与运行环境:Gradle 8.x + JDK 17(最低 11)
- Kotlin & Compose 编译器:Kotlin 1.9.x + Compose Compiler 1.5.x(与 Kotlin 版本严格对应)
- Compose 测试基础库:
androidx.compose.ui:ui-test-junit4/ui-test-manifest(夹具核心) - 测试运行器与断言:AndroidX Test Runner + JUnit 4 / 5
⚠ 版本锁定
Compose Compiler 与 Kotlin 的版本映射表可在 Compose-Kotlin 兼容性表 查询。务必确保 compiler 版本与 Kotlin plugin 版本匹配,否则构建会直接失败。
Compose Compiler 与 Kotlin 的版本映射表可在 Compose-Kotlin 兼容性表 查询。务必确保 compiler 版本与 Kotlin plugin 版本匹配,否则构建会直接失败。
1.2 依赖清单与版本要求
下表列出了在 build.gradle.kts(Module 级)中必须声明的依赖项及其推荐版本:
| 组件 | 推荐版本 | 最低版本 | 获取方式(Maven 坐标) |
|---|---|---|---|
| Gradle | 8.5 | 8.2 | 项目 gradle-wrapper.properties 中指定 |
| JDK | 17 (LTS) | 11 | Adoptium / Oracle / OpenJDK 官网下载 |
| Kotlin Plugin | 1.9.22 | 1.9.0 | org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.22 |
| Compose Compiler | 1.5.10 | 1.5.0 | androidx.compose.compiler:compiler:1.5.10 |
| Compose UI Test | 1.6.0 | 1.5.0 | androidx.compose.ui:ui-test-junit4:1.6.0 |
| Compose Test Manifest | 1.6.0 | 1.5.0 | androidx.compose.ui:ui-test-manifest:1.6.0 |
| AndroidX Test Runner | 1.5.0 | 1.4.0 | androidx.test:runner:1.5.0 |
| JUnit | 4.13.2 | 4.12 | junit:junit:4.13.2 |
1.3 依赖声明模板
以下是一个典型的 Module 级 build.gradle.kts 片段,展示了如何整合所有必要依赖:
// 项目根 build.gradle.kts
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.22")
classpath("com.android.tools.build:gradle:8.2.0")
}
}
// Module 级 build.gradle.kts
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
compileSdk = 34
defaultConfig {
minSdk = 21
targetSdk = 34
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.10"
}
}
dependencies {
implementation(platform("androidx.compose:compose-bom:2024.01.00"))
// 夹具核心
debugImplementation("androidx.compose.ui:ui-test-manifest:1.6.0")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.6.0")
// 运行器 & JUnit
androidTestImplementation("androidx.test:runner:1.5.0")
androidTestImplementation("junit:junit:4.13.2")
}
1.4 组件依赖关系
下图直观地展示了 Compose 夹具环境中各组件之间的依赖层次:
1.5 常见环境配置错误
- JDK 版本过低:Gradle 8.x 要求 JDK 17,若使用 JDK 11 会出现
Unsupported class file major version。 - Compose Compiler 与 Kotlin 不匹配:典型报错
This version of the Compose Compiler requires Kotlin version 1.9.x。 - 缺少
ui-test-manifest:夹具需要在debugImplementation范围内引入该依赖,否则运行时抛出No tests found for given includes。 - BOM 版本陈旧:2024 年 1 月之前的 BOM 可能不包含最新的夹具 API,建议使用
2024.01.00或更高。
🔴 注意
如果项目中同时使用 Robolectric 与 Compose 夹具,需确保
如果项目中同时使用 Robolectric 与 Compose 夹具,需确保
testInstrumentationRunner 不冲突,推荐在 androidTest 源集中单独配置夹具用例,通过 @RunWith(AndroidJUnit4::class) 运行。
1.6 验证环境安装
在终端执行以下命令确认关键工具的版本:
# 检查 JDK 版本
java -version
# 输出示例:openjdk version "17.0.9" 2023-10-17 LTS
# 检查 Gradle 版本(使用项目包装器)
./gradlew --version
# 输出应包含:Gradle 8.5+
# 检查 Kotlin 插件版本(可在 build.gradle.kts 中查看,或执行)
./gradlew dependencies --configuration compileClasspath | grep kotlin-plugin