运行Compose UI测试时遇找不到androidx.compose.ui:ui-test-junit4依赖问题
问题:Compose UI测试依赖解析失败
运行Compose UI测试时出现以下错误:
Could not find androidx.compose.ui:ui-test-junit4:. Required by: project :app Search in build.gradle files我已经在依赖中添加了
androidTestImplementation "androidx.compose.ui:ui-test-junit4",附上完整的app模块build.gradle代码:/* * Copyright 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { compileSdk 33 defaultConfig { applicationId "com.example.reply" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.3.0' } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation platform('androidx.compose:compose-bom:2023.01.00') implementation 'androidx.activity:activity-compose:1.6.1' implementation 'androidx.core:core-ktx:1.9.0' implementation "androidx.compose.ui:ui" implementation "androidx.compose.material3:material3" implementation "androidx.compose.material3:material3-window-size-class:$material3_version" implementation "androidx.compose.material:material-icons-extended" implementation "androidx.compose.ui:ui-tooling-preview" implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" androidTestImplementation "androidx.compose.ui:ui-test-junit4" androidTestImplementation 'androidx.test.ext:junit:1.1.3' debugImplementation("androidx.compose.ui:ui-test-manifest") debugImplementation "androidx.compose.ui:ui-tooling" }
解决步骤
这个问题主要由两个原因导致:Compose编译器扩展版本与BOM版本不匹配,以及依赖中使用了未声明的变量。按以下步骤修复:
对齐Compose编译器扩展版本
你使用的compose-bom:2023.01.00对应的Kotlin Compose编译器扩展版本是1.4.0,但当前配置的是1.3.0,版本不兼容会导致依赖解析失败。修改composeOptions块:composeOptions { kotlinCompilerExtensionVersion '1.4.0' }移除未声明的变量依赖
你的依赖中$material3_version、$lifecycle_version、$kotlin_version这些变量没有定义,直接替换为兼容版本:- 由于已经引入Compose BOM,Material3相关依赖可以去掉版本号,由BOM自动管理:
implementation "androidx.compose.material3:material3-window-size-class" - Lifecycle和Kotlin标准库使用明确的兼容版本:
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.1" implementation "org.jetbrains.kotlin:kotlin-stdlib:1.8.10"
- 由于已经引入Compose BOM,Material3相关依赖可以去掉版本号,由BOM自动管理:
验证测试依赖配置
androidTestImplementation "androidx.compose.ui:ui-test-junit4"和debugImplementation "androidx.compose.ui:ui-test-manifest"不需要加版本号,BOM会自动匹配正确版本,保持现有配置即可。
修改后的完整dependencies块示例:
dependencies { implementation platform('androidx.compose:compose-bom:2023.01.00') implementation 'androidx.activity:activity-compose:1.6.1' implementation 'androidx.core:core-ktx:1.9.0' implementation "androidx.compose.ui:ui" implementation "androidx.compose.material3:material3" implementation "androidx.compose.material3:material3-window-size-class" implementation "androidx.compose.material:material-icons-extended" implementation "androidx.compose.ui:ui-tooling-preview" implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.1" implementation "org.jetbrains.kotlin:kotlin-stdlib:1.8.10" androidTestImplementation "androidx.compose.ui:ui-test-junit4" androidTestImplementation 'androidx.test.ext:junit:1.1.3' debugImplementation "androidx.compose.ui:ui-test-manifest" debugImplementation "androidx.compose.ui:ui-tooling" }
完成以上修改后,执行Gradle同步操作,问题即可解决。
内容的提问来源于stack exchange,提问作者AbsoluteHeero
相关产品推荐
相关产品推荐

