英文:
Duplicate class org.hamcrest
问题
我正在尝试在插件Android项目中导入 implementation 'com.googlecode.json-simple:json-simple:1.1.1'
。
Gradle脚本: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'
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 ... (其他类似的错误)
Go to the documentation to learn how to Fix dependency resolution errors.
英文:
I am trying to import implementation 'com.googlecode.json-simple:json-simple:1.1.1'
in the plugin android project.
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'
}
And after build adding into the MainActivity project and while building getting an error in the MainActivity project.
Build output error:
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.
答案1
得分: 2
问题是 hamcrest 类也包含在 junit 中。所以 "org.hamcrest.BaseDescription" 和其他类在类路径上出现了两次。JVM 将选择首次出现的版本。由于这可能是非确定性的,而且版本可能不同,所以 Gradle 检测到这一点并发出警告。
要解决这个问题,你需要以某种方式移除其中一个副本。在你的情况下,hamcrest 似乎是另一个库的传递性依赖,所以这可能不是一件简单的事情。
以下答案可能会有帮助:
链接2: https://stackoverflow.com/questions/30702059/how-to-ignore-or-fix-the-duplicate-classes-warning
英文:
The problem is that the hamcrest classes are bundled in junit also. So "org.hamcrest.BaseDescription" and the other classes exist on the classpath twice. The JVM will pick the one which appears first. Since this might be non deterministic and the versions might be different, gradle detects this and complains.
To fix this, you have to remove one of the copies one way or the other. In your case, hamcrest seems to be a transitive dependency of another lib, so this is probably not trivial.
These anwers might help:
https://stackoverflow.com/questions/30702059/how-to-ignore-or-fix-the-duplicate-classes-warning
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论