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

Android项目androidTest依赖JUnit4.13出现版本冲突无法解析如何解决?

问题根因

你遇到的是Gradle依赖版本冲突,错误日志已明确冲突来源:

Constraint path 'Myapp:app:unspecified' --> 'junit:junit:{strictly 4.12}' because of the following reason: debugRuntimeClasspath uses version 4.12

主应用的debug运行时类路径误引入了JUnit 4.12,触发了Gradle的严格版本约束,和androidTest中需要的4.13.2版本不兼容。你从其他项目复制的依赖本身没问题,冲突来自主工程的依赖配置错误。


解决步骤

步骤1:强制指定JUnit版本

在app模块的build.gradle文件中添加以下配置,强制所有配置使用JUnit 4.13.2:

configurations.all {
    resolutionStrategy.force 'junit:junit:4.13.2'
}

如果你不想影响主工程配置(主工程正常情况下不会依赖JUnit),可以仅针对测试类路径指定:

configurations {
    debugAndroidTestRuntimeClasspath {
        resolutionStrategy.force 'junit:junit:4.13.2'
    }
    testImplementation {
        resolutionStrategy.force 'junit:junit:4.13.2'
    }
}

步骤2:清理主工程错误依赖

检查app模块build.gradle的dependencies块,确认JUnit的依赖仅出现在testImplementation和androidTestImplementation配置下,不要加到implementation/api等主代码依赖配置中。

步骤3:验证配置生效

修改完成后执行以下命令,确认输出中所有junit:junit的版本均为4.13.2即可:

./gradlew :app:dependencyInsight --dependency junit:junit --configuration debugAndroidTestRuntimeClasspath

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 09:06:03