You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio运行Java程序时preDebugAndroidTestBuild任务执行失败求助

解决support-annotations依赖冲突问题

这种冲突在Android项目里很常见,核心原因是你的主应用使用的com.android.support:support-annotations版本(26.1.0)和测试模块自动引入的版本(27.1.1)不一致,Gradle无法自动调和这个版本差异。这里有几个靠谱的解决方案:

方案1:强制统一所有模块的依赖版本

在你的app模块的build.gradle文件中,添加一段配置来强制所有模块使用同一个版本的support-annotations:

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
}

如果你打算升级到更高版本(比如27.1.1),记得把主应用中所有其他support库(比如appcompat-v7、design等)的版本也同步改成27.1.1——support库的版本必须完全统一,否则还会触发其他依赖冲突。

方案2:明确指定测试模块的依赖版本

直接在build.gradle的测试依赖块中指定和主应用一致的版本,覆盖测试模块默认拉取的版本:

androidTestImplementation 'com.android.support:support-annotations:26.1.0'

这样测试模块就会严格使用你指定的版本,和主应用保持一致。

方案3:迁移到AndroidX(推荐长期方案)

如果你的项目还在使用旧的support库,建议直接迁移到AndroidX——这是Google现在主推的库体系,能从根源上减少这类版本冲突问题:

  1. 在项目根目录的gradle.properties中添加以下配置:
android.useAndroidX=true
android.enableJetifier=true
  1. 把所有com.android.support:support-annotations依赖替换成AndroidX的注解库:
// 主应用依赖
implementation 'androidx.annotation:annotation:1.6.0'
// 测试模块依赖
androidTestImplementation 'androidx.annotation:annotation:1.6.0'

Jetifier会自动帮你转换第三方库中的旧support依赖为AndroidX版本,无需手动修改第三方依赖代码。

内容的提问来源于stack exchange,提问作者Vishal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:31:16