问题描述
我正在尝试在插件android项目中导入implementation 'com.googlecode.json-simple:json-simple:1.1.1'
。
Gradle Scripts:build.gradle(Module: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.
解决方法
问题在于hamcrest类也捆绑在junit中。因此,“ org.hamcrest.BaseDescription”和其他类在类路径中存在两次。 JVM将选择最先出现的那个。由于这可能是不确定的,版本可能会有所不同,因此gradle会检测到并抱怨。
要解决此问题,必须以一种或另一种方式删除其中一个副本。在您的情况下,hamcrest似乎是另一个库的可传递依赖项,因此这可能并不简单。
这些答案可能会帮助您
Duplicate Hamcrest and JUnit classes after updating Gradle and Android Studio to 3.5