How to resolve duplicated gradle Dependency issues
Asked Answered
L

4

24

I have tried to test my code with robolectric. Problem is that it has duplicated References. e.g.

java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class org.apache.maven.artifact.Artifact found in modules maven-ant-tasks-2.1.3.jar (org.apache.maven:maven-ant-tasks:2.1.3) and maven-artifact-2.2.1.jar (org.apache.maven:maven-artifact:2.2.1)

I have used the gradel artifact app:dependencies to get the following report. Here the important parts:

+--- org.robolectric:robolectric:4.3
|    +--- org.robolectric:annotations:4.3
|    +--- org.robolectric:junit:4.3
|    |    +--- org.robolectric:annotations:4.3
|    |    +--- org.robolectric:sandbox:4.3
|    |    |    +--- org.robolectric:annotations:4.3
|    |    |    +--- org.robolectric:utils:4.3
|    |    |    |    +--- org.robolectric:annotations:4.3
|    |    |    |    +--- org.robolectric:pluginapi:4.3
|    |    |    |    |    +--- org.robolectric:annotations:4.3
|    |    |    |    |    +--- org.apache.ant:ant:1.8.0
|    |    |    |    |    |    \--- org.apache.ant:ant-launcher:1.8.0
|    |    |    |    |    \--- org.apache.maven:maven-ant-tasks:2.1.3
|    |    |    |    |         +--- org.apache.ant:ant:1.8.0 (*)
|    |    |    |    |         +--- classworlds:classworlds:1.1-alpha-2
|    |    |    |    |         +--- org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1
|    |    |    |    |         |    +--- org.codehaus.plexus:plexus-utils:1.0.4 -> 1.5.15
|    |    |    |    |         |    \--- classworlds:classworlds:1.1-alpha-2
|    |    |    |    |         +--- org.codehaus.plexus:plexus-utils:1.5.15
|    |    |    |    |         +--- org.codehaus.plexus:plexus-interpolation:1.11
|    |    |    |    |         +--- org.apache.maven:maven-artifact:2.2.1
|    |    |    |    |         |    \--- org.codehaus.plexus:plexus-utils:1.5.15
|    |    |    |    |         +--- org.apache.maven:maven-artifact-manager:2.2.1
|    |    |    |    |         |    +--- org.apache.maven:maven-repository-metadata:2.2.1
|    |    |    |    |         |    |    \--- org.codehaus.plexus:plexus-utils:1.5.15
|    |    |    |    |         |    +--- org.codehaus.plexus:plexus-utils:1.5.15
|    |    |    |    |         |    +--- org.apache.maven:maven-artifact:2.2.1 (*)
|    |    |    |    |         |    +--- org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1 (*)
|    |    |    |    |         |    +--- org.apache.maven.wagon:wagon-provider-api:1.0-beta-6
|    |    |    |    |         |    |    \--- org.codehaus.plexus:plexus-utils:1.4.2 -> 1.5.15
|    |    |    |    |         |    \--- backport-util-concurrent:backport-util-concurrent:3.1
|    |    |    |    |         +--- org.apache.maven:maven-model:2.2.1
|    |    |    |    |         |    \--- org.codehaus.plexus:plexus-utils:1.5.15
|    |    |    |    |         +--- org.apache.maven:maven-project:2.2.1
|    |    |    |    |         |    +--- org.apache.maven:maven-settings:2.2.1
|    |    |    |    |         |    |    +--- org.apache.maven:maven-model:2.2.1 (*)
|    |    |    |    |         |    |    +--- org.codehaus.plexus:plexus-interpolation:1.11
|    |    |    |    |         |    |    +--- org.codehaus.plexus:plexus-utils:1.5.15
|    |    |    |    |         |    |    \--- org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1 (*)
|    |    |    |    |         |    +--- org.apache.maven:maven-profile:2.2.1
|    |    |    |    |         |    |    +--- org.apache.maven:maven-model:2.2.1 (*)
|    |    |    |    |         |    |    +--- org.codehaus.plexus:plexus-utils:1.5.15
|    |    |    |    |         |    |    +--- org.codehaus.plexus:plexus-interpolation:1.11
|    |    |    |    |         |    |    \--- org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1 (*)
|    |    |    |    |         |    +--- org.apache.maven:maven-model:2.2.1 (*)
|    |    |    |    |         |    +--- org.apache.maven:maven-artifact-manager:2.2.1 (*)
|    |    |    |    |         |    +--- org.apache.maven:maven-plugin-registry:2.2.1
|    |    |    |    |         |    |    +--- org.codehaus.plexus:plexus-utils:1.5.15
|    |    |    |    |         |    |    \--- org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1 (*)
|    |    |    |    |         |    +--- org.codehaus.plexus:plexus-interpolation:1.11
|    |    |    |    |         |    +--- org.codehaus.plexus:plexus-utils:1.5.15
|    |    |    |    |         |    +--- org.apache.maven:maven-artifact:2.2.1 (*)
|    |    |    |    |         |    \--- org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1 (*)
|    |    |    |    |         +--- org.apache.maven:maven-error-diagnostics:2.2.1
|    |    |    |    |         |    \--- org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1 (*)
|    |    |    |    |         +--- org.apache.maven:maven-settings:2.2.1 (*)
|    |    |    |    |         +--- org.apache.maven.wagon:wagon-file:1.0-beta-6
|    |    |    |    |         |    \--- org.apache.maven.wagon:wagon-provider-api:1.0-beta-6 (*)
|    |    |    |    |         +--- org.apache.maven.wagon:wagon-http-lightweight:1.0-beta-6
|    |    |    |    |         |    +--- org.apache.maven.wagon:wagon-http-shared:1.0-beta-6
|    |    |    |    |         |    |    +--- nekohtml:xercesMinimal:1.9.6.2
|    |    |    |    |         |    |    +--- nekohtml:nekohtml:1.9.6.2
|    |    |    |    |         |    |    \--- org.apache.maven.wagon:wagon-provider-api:1.0-beta-6 (*)
|    |    |    |    |         |    \--- org.apache.maven.wagon:wagon-provider-api:1.0-beta-6 (*)
|    |    |    |    |         \--- org.apache.maven.wagon:wagon-provider-api:1.0-beta-6 (*)
|    |    |    |    +--- org.robolectric:utils-reflector:4.3
|    |    |    |    |    +--- org.ow2.asm:asm:7.0
|    |    |    |    |    +--- org.ow2.asm:asm-commons:7.0
|    |    |    |    |    |    +--- org.ow2.asm:asm:7.0
|    |    |    |    |    |    +--- org.ow2.asm:asm-tree:7.0
|    |    |    |    |    |    |    \--- org.ow2.asm:asm:7.0
|    |    |    |    |    |    \--- org.ow2.asm:asm-analysis:7.0
|    |    |    |    |    |         \--- org.ow2.asm:asm-tree:7.0 (*)
|    |    |    |    |    \--- org.ow2.asm:asm-util:7.0
|    |    |    |    |         +--- org.ow2.asm:asm:7.0
|    |    |    |    |         +--- org.ow2.asm:asm-tree:7.0 (*)
|    |    |    |    |         \--- org.ow2.asm:asm-analysis:7.0 (*)
|    |    |    |    +--- com.google.auto.service:auto-service:1.0-rc4
|    |    |    |    |    +--- com.google.auto:auto-common:0.8
|    |    |    |    |    |    \--- com.google.guava:guava:19.0 -> 27.0.1-jre
|    |    |    |    |    |         +--- com.google.guava:failureaccess:1.0.1
|    |    |    |    |    |         +--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
|    |    |    |    |    |         +--- com.google.code.findbugs:jsr305:3.0.2
|    |    |    |    |    |         +--- org.checkerframework:checker-qual:2.5.2
|    |    |    |    |    |         +--- com.google.errorprone:error_prone_annotations:2.2.0
|    |    |    |    |    |         +--- com.google.j2objc:j2objc-annotations:1.1
|    |    |    |    |    |         \--- org.codehaus.mojo:animal-sniffer-annotations:1.17
|    |    |    |    |    \--- com.google.guava:guava:23.5-jre -> 27.0.1-jre (*)
|    |    |    |    +--- javax.inject:javax.inject:1
|    |    |    |    \--- javax.annotation:javax.annotation-api:1.3.2
|    |    |    +--- org.robolectric:shadowapi:4.3
|    |    |    |    \--- org.robolectric:annotations:4.3
|    |    |    +--- org.robolectric:utils-reflector:4.3 (*)
|    |    |    +--- javax.annotation:javax.annotation-api:1.3.2
|    |    |    +--- javax.inject:javax.inject:1
|    |    |    +--- org.ow2.asm:asm:7.0
|    |    |    +--- org.ow2.asm:asm-commons:7.0 (*)
|    |    |    \--- com.google.guava:guava:27.0.1-jre (*)
|    |    +--- org.robolectric:pluginapi:4.3 (*)
|    |    +--- org.robolectric:shadowapi:4.3 (*)
|    |    \--- org.robolectric:utils-reflector:4.3 (*)
|    +--- org.robolectric:pluginapi:4.3 (*)
|    +--- org.robolectric:resources:4.3
|    |    +--- org.robolectric:utils:4.3 (*)
|    |    +--- org.robolectric:annotations:4.3
|    |    +--- org.robolectric:pluginapi:4.3 (*)
|    |    \--- com.google.guava:guava:27.0.1-jre (*)
|    +--- org.robolectric:sandbox:4.3 (*)
|    +--- org.robolectric:utils:4.3 (*)
|    +--- org.robolectric:utils-reflector:4.3 (*)
|    +--- org.robolectric:plugins-maven-dependency-resolver:4.3
|    |    +--- org.robolectric:pluginapi:4.3 (*)
|    |    +--- org.robolectric:utils:4.3 (*)
|    |    +--- org.apache.ant:ant:1.8.0 (*)
|    |    \--- org.apache.maven:maven-ant-tasks:2.1.3 (*)
|    +--- javax.inject:javax.inject:1
|    +--- javax.annotation:javax.annotation-api:1.3.2
|    +--- org.robolectric:shadows-framework:4.3
|    |    +--- org.robolectric:annotations:4.3
|    |    +--- org.robolectric:resources:4.3 (*)
|    |    +--- org.robolectric:pluginapi:4.3 (*)
|    |    +--- org.robolectric:shadowapi:4.3 (*)
|    |    +--- org.robolectric:utils:4.3 (*)
|    |    +--- org.robolectric:utils-reflector:4.3 (*)
|    |    +--- androidx.test:monitor:1.2.0 -> 1.3.0-alpha02 (*)
|    |    +--- com.almworks.sqlite4java:sqlite4java:0.282
|    |    +--- com.ibm.icu:icu4j:53.1
|    |    +--- com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:2.1
|    |    |    +--- org.hamcrest:hamcrest-core:1.3
|    |    |    +--- org.hamcrest:hamcrest-library:1.3 (*)
|    |    |    \--- com.google.protobuf:protobuf-java:2.6.1
|    |    \--- androidx.annotation:annotation:1.0.0
|    +--- org.bouncycastle:bcprov-jdk15on:1.52
|    \--- androidx.test:monitor:1.2.0 -> 1.3.0-alpha02 (*)

How to resolve the reference error? It seems to me that robolectric has the same class in diffrent dependencies?

I use the following build script: apply plugin: 'com.android.application'

apply plugin: 'idea'

idea {
    module {
        downloadJavadoc = true
        downloadSources = true
    }
}
android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    useLibrary 'org.apache.http.legacy'
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'

    defaultConfig {
        applicationId "com.example.sampleapp"
        minSdkVersion 16
        targetSdkVersion 28
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

dependencies {
    androidTestImplementation 'androidx.test:rules:1.2.0'
    androidTestImplementation 'androidx.test:core:1.2.1-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'org.robolectric:robolectric:4.3'

    // Mail-Versand
    implementation 'com.sun.mail:android-mail:1.6.2'
    implementation 'com.sun.mail:android-activation:1.6.2'

    implementation 'androidx.appcompat:appcompat:1.0.2'//
    implementation 'com.github.woxthebox:draglistview:1.6.6'
}

How to fix that?

Issue on github is open: https://github.com/robolectric/robolectric/issues/5235

Lurk answered 21/8, 2019 at 14:45 Comment(0)
L
28

Finally get the test running. I've excluded all libs with duplicated classes:

testImplementation ("org.robolectric:robolectric:4.3"){
    exclude group: 'org.apache.maven', module: 'maven-artifact'
    exclude group: 'org.apache.maven', module: 'maven-artifact-manager'
    exclude group: 'org.apache.maven', module: 'maven-model'
    exclude group: 'org.apache.maven', module: 'maven-plugin-registry'
    exclude group: 'org.apache.maven', module: 'maven-profile'
    exclude group: 'org.apache.maven', module: 'maven-project'
    exclude group: 'org.apache.maven', module: 'maven-settings'
    exclude group: 'org.apache.maven', module: 'maven-error-diagnostics'
    exclude group: "org.apache.maven.wagon"
}

Not all possibilities tested but a simple test worked already.

Lurk answered 6/9, 2019 at 9:27 Comment(2)
you are a lifesaver @LurkMousetrap
Just in case: I have to add this exclude for protobuf also exclude group: "com.google.protobuf", module: "protobuf-java"Nissensohn
R
7

This probably could be written in more general way:

testImplementation ("org.robolectric:robolectric:4.3") {
    exclude group "org.apache.maven.wagon"
    exclude group "org.apache.maven"
}

or even:

testImplementation ("org.robolectric:robolectric:4.3") {
    exclude group: "org.apache.maven", name: "maven-ant-tasks"
}

because it is maven-ant-tasks of pluginapi, which pulls in org.apache.maven dependencies.

Romelda answered 8/9, 2019 at 11:28 Comment(4)
This doesn't work. It seems great, but you also exclude with org.apache.maven the group maven-ant-tasks. This causes a ClassNotFoundException: org.apache.maven.artifact.ant.DependenciesTask. The idea to exclude the whole group is great. I've used it for excluding org.apache.maven.wagonLurk
unfortunately, in my case, org.codehaus.classworld that roboelectric 4.3.1 used duplicate as well, even though I've excluded using the group, it will show ClassNotFound as well @LurkMousetrap
@Mousetrap if you'd post a new question, providing dependencies and notify me with it's URL, I'd likely could solve the issue. ClassNotFound usually only happens, when excluding too much.Romelda
Think that one has to use debugImplementation for one of the packages (would need to see which one it was). These are dependencies for two whole different applications; at least on Android.Romelda
K
3

I've encountered the same issue and the thing that helped me was that I had defined the Roboelectric dependency twice in Gradle file one with androidTestImplementation and the other with testImplementation so when I removed the first part problem solved!!!

Kantianism answered 8/1, 2020 at 7:29 Comment(1)
you're also a lifesaver man @alireza-rahmaty. I don't know why I need to add testImplementation for roboelectric. That means for instrumentation, which is doesn't needed for unitTest directory. thanks again manMousetrap
H
0

The solution for me was to ensure that I was not requiring Robolectric in my androidTestImplementation dependencies.

Once I fixed that, I did not have any conflicts to fix.

Haematoid answered 18/11, 2020 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.