Android Studio编译报错:hamcrest-core与JUnit 4.10类重复
解决JUnit与Hamcrest依赖冲突问题
编译项目时持续出现重复类错误,错误显示org.hamcrest包下的类同时存在于hamcrest-core-1.1和junit-4.10两个模块中。
错误信息
Duplicate class org.hamcrest.BaseDescription found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.BaseMatcher found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.CoreMatchers found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.Description found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.Factory found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.Matcher found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.SelfDescribing found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.StringDescription found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.AllOf found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.AnyOf found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.DescribedAs found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.Is found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.IsAnything found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.IsEqual found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.IsInstanceOf found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.IsNot found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.IsNull found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.core.IsSame found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.internal.ArrayIterator found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.internal.SelfDescribingValue found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Duplicate class org.hamcrest.internal.SelfDescribingValueIterator found in modules hamcrest-core-1.1 (org.hamcrest:hamcrest-core:1.1) and junit-4.10 (junit:junit:4.10) Go to the documentation to learn how to Fix dependency resolution errors.
当前依赖配置
dependencies { implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.core:core-ktx:1.5.0' implementation 'androidx.appcompat:appcompat:1.5.0' implementation "org.projectlombok:lombok:1.18.30" implementation 'com.google.code.gson:gson:2.8.7' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' implementation 'com.github.bumptech.glide:glide:4.16.0' implementation 'com.googlecode.json-simple:json-simple:1.1.1' runtimeOnly 'com.google.android.material:material:1.11.0-alpha03' testImplementation 'junit:junit:4.10' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
解决方案
方案1:升级JUnit版本(推荐)
JUnit 4.10及更早版本内置了Hamcrest类,而Android测试库(如Espresso)会单独引入Hamcrest,导致冲突。升级到JUnit 4.11及以上版本即可解决,因为这些版本不再内置Hamcrest,而是通过依赖引入。
修改依赖中的JUnit配置:
testImplementation 'junit:junit:4.13.2'
方案2:排除JUnit中的Hamcrest依赖
如果不想升级JUnit版本,可在依赖声明中排除JUnit内置的Hamcrest模块:
testImplementation('junit:junit:4.10') { exclude group: 'org.hamcrest' }
额外优化:统一Material库版本
当前依赖中同时存在两个版本的Material库(1.2.1和1.11.0-alpha03),建议统一使用一个稳定版本,避免潜在的版本冲突,例如:
implementation 'com.google.android.material:material:1.11.0' // 移除runtimeOnly的旧版本
内容的提问来源于stack exchange,提问作者Giovanna Bregantin
相关产品推荐
相关产品推荐

