首页 > 解决方案 > 重复类 org.hamcrest

问题描述

我正在尝试implementation 'com.googlecode.json-simple:json-simple:1.1.1'在插件android项目中导入。

Gradle 脚本:build.gradle(模块:android)

ext {
    junitVersion =  project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.12'
    androidxJunitVersion =  project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.1'
    androidxEspressoCoreVersion =  project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.2.0'
}

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29
    defaultConfig {
        minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
        targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

repositories {
    google()
    jcenter()
    mavenCentral()
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':capacitor-android')
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
    implementation 'net.zetetic:android-database-sqlcipher:4.4.0@aar'
    implementation "androidx.sqlite:sqlite:2.0.1"
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    // Because RxAndroid releases are few and far between, it is recommended you also
    // explicitly depend on RxJava's latest version for bug fixes and new features.
    // (see https://github.com/ReactiveX/RxJava/releases for latest 3.x.x version)
    implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
    implementation 'com.googlecode.json-simple:json-simple:1.1.1'
}

在构建添加到 MainActivity 项目后,在构建 MainActivity 项目时出现错误。

构建输出错误:

Duplicate class org.hamcrest.BaseDescription found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.BaseMatcher found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.CoreMatchers found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.Description found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.Factory found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.Matcher found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.SelfDescribing found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.StringDescription found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.AllOf found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.AnyOf found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.DescribedAs found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.Is found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.IsAnything found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.IsEqual found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.IsInstanceOf found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.IsNot found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.IsNull found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.core.IsSame found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.internal.ArrayIterator found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.internal.SelfDescribingValue found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)
Duplicate class org.hamcrest.internal.SelfDescribingValueIterator found in modules jetified-hamcrest-core-1.1.jar (org.hamcrest:hamcrest-core:1.1) and jetified-junit-4.10.jar (junit:junit:4.10)

Go to the documentation to learn how to Fix dependency resolution errors.

标签: javaandroidhamcrest

解决方案


问题是 hamcrest 类也捆绑在 junit 中。所以“org.hamcrest.BaseDescription”和其他类在类路径中存在两次。JVM 将选择最先出现的那个。由于这可能是不确定的并且版本可能不同,gradle 检测到这一点并抱怨。

要解决此问题,您必须以一种或另一种方式删除其中一个副本。在你的情况下,hamcrest 似乎是另一个库的传递依赖,所以这可能不是微不足道的。

这些答案可能会有所帮助:

将 Gradle 和 Android Studio 更新到 3.5 后复制 Hamcrest 和 JUnit 类

如何忽略或修复重复类警告?


推荐阅读