在模块 jetified-hamcrest-core-1.3.jar 中发现重复的类 org.hamcrest.BaseDescription。

7

Android Studio 3.6

应用程序/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)

1
这个 https://dev59.com/oOk5XIcBkEYKwwoY49on#65036282 对我有效。 - Chris Tang
2个回答

5
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')
    }
}

2
当我将它添加到应用程序模块而不是项目构建文件中时,它对我起作用。我吃了这个教训。 - JPM

3
我认为这个问题发生在你添加一个依赖时(就像你的情况下,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" }

4
请尝试以下代码:configurations { compile.exclude group: "junit", module: "junit"}。该代码用于排除JUnit库的依赖项。 - Sepehr
1
编译时排除操作会导致无法执行单元测试!请避免任何排除操作,正确的方法是用JUnit替换Hamcrest!configurations.all { resolutionStrategy.dependencySubstitution { substitute module('org.hamcrest:hamcrest-core:1.1') with module('junit:junit:4.10') } } - M22

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接