【问题标题】:Duplicate class org.hamcrest.BaseDescription found in modules jetified-hamcrest-core-1.3.jar在模块 jetified-hamcrest-core-1.3.jar 中发现重复的类 org.hamcrest.BaseDescription
【发布时间】:2023-04-11 02:40:01
【问题描述】:

Android 工作室 3.6

app/build.gradle:

 androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'com.azimolabs.conditionwatcher:conditionwatcher:0.2'
    // Espresso framework
    androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_version"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$espresso_version"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espresso_version"
    androidTestImplementation 'org.hamcrest:hamcrest-junit:2.0.0.0'

    // UI Automator framework
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.8.0'

    // for test fragments
    debugImplementation 'androidx.fragment:fragment-testing:1.2.0-rc02'

    testImplementation 'junit:junit:4.12'
    testImplementation 'com.nhaarman:mockito-kotlin-kt1.1:1.5.0'

在 gradle.properties 中:

android.useAndroidX=true
android.enableJetifier=true

这是我的 Espresso 仪器测试:

import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.hamcrest.text.MatchesPattern
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class FeedbackActivityTransportTest {
 @Test
    fun buttonSend_click_checkRequest() {
        val request = mockServer.takeRequest();
        assertEquals("POST", request.method)
        assertThat(
            request.body.toString(),
            MatchesPattern.matchesPattern("(\"feedback.*\\\"type\\\":2\"))")
        )
    }

但我得到错误:

Duplicate class org.hamcrest.BaseDescription found in modules jetified-hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3) and jetified-java-hamcrest-2.0.0.0.jar (org.hamcrest:java-hamcrest:2.0.0.0)
Duplicate class org.hamcrest.BaseMatcher found in modules jetified-hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3) and jetified-java-hamcrest-2.0.0.0.jar (org.hamcrest:java-hamcrest:2.0.0.0)
Duplicate class org.hamcrest.Condition found in modules jetified-hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3) and jetified-java-hamcrest-2.0.0.0.jar (org.hamcrest:java-hamcrest:2.0.0.0)
Duplicate class org.hamcrest.Condition$1 found in modules jetified-hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3) and jetified-java-hamcrest-2.0.0.0.jar (org.hamcrest:java-hamcrest:2.0.0.0)

【问题讨论】:

标签: android android-espresso hamcrest


【解决方案1】:

我认为当您添加依赖项时会发生此问题(作为您的 情况 Hamcrest 和另一个依赖项、库、Jar 文件等...... 也使用 Hamcrest!但有另一个版本。

如果您像下面这样在 app Gradle 中强制您的 Hamcrest 依赖可能会解决您的问题:

configurations.all {
    resolutionStrategy {
        force 'org.hamcrest:hamcrest-junit:2.0.0.0'
    }
}

应用后,如果您遇到同样的错误,请尝试像这样排除

configurations { compile.exclude group: "junit", module: "junit" }

【讨论】:

  • 试试这个:配置 { compile.exclude 组:“junit”,模块:“junit”}
  • compile.exclude 会让你无法执行单元测试!请避免任何排除,正确的方法是用junit替换hamcrest! configuration.all { resolutionStrategy.dependencySubstitution { 替换模块('org.hamcrest:hamcrest-core:1.1') 与模块('junit:junit:4.10') } }
【解决方案2】:

排除 junit 并给予 Hamcrest 优先级将禁用使用 JUnit 执行单元测试的机会!这就是为什么在进行单元测试时会在 Android Studio 中出现错误:Cannot resolve '@Before' or '@Test'。正确的做法是将 Hamcrest 替换为 JUnit

将此代码放在 app 级别的 build.gradle 中:

configurations.all {
    resolutionStrategy.dependencySubstitution {
        substitute module('org.hamcrest:hamcrest-core:1.1') with module('junit:junit:4.10')
    }
}

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 2022-07-22
    • 2021-03-18
    • 2022-08-20
    • 2021-10-13
    • 2020-06-13
    • 2022-11-11
    • 2021-06-08
    • 2020-09-12
    相关资源
    最近更新 更多