Android Gradle compiling commons-io creates duplicate in library tree
Asked Answered
B

4

8

I'm trying to build and maintain an old application for work but I can't get past the build phase. In my app/build.gradle file I have

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.apache.commons:commons-io:1.3.2'
    //some more libraries compiled as well
}

but get the following error when trying to execute:

Error:Execution failed for task ':myApp'. com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/apache/commons/io/CopyUtils.class

This is almost certainly because when I compile that jar, at the top of my External Libraries tree, this is generated:

enter image description here

Why is this happening, and how can I get it to stop so I can complete the build?

Bihar answered 3/2, 2016 at 19:4 Comment(0)
D
5

There is a simple way to exclude the double classes. At first you need to find out which dependency is causing that if you know that use this code:

compile('com.example:some-dependency:4.2') {
    exclude module: 'commons-io'
}
Dustheap answered 4/2, 2016 at 19:21 Comment(2)
It appears to be commons-io itself. When I leave that out, both versions disappear from the library. I'll add the exclude to its own compilation but that seems to be kind of counterintuitive...Bihar
I suspect it is the fault of this line: compile fileTree(dir: 'libs', include: ['*.jar']) do you have a libs directory with jars?Dustheap
H
13

There is an option to fix it on gradle dependency resolution level

configurations.all {
    resolutionStrategy.dependencySubstitution {
        substitute module('org.apache.commons:commons-io:1.3.2') with module('commons-io:commons-io:1.3.2')
    }
}

Reason of the conflict is that org.apache.commons:commons-io:1.3.2 was pushed by mistake https://mcmap.net/q/580128/-what-is-the-difference-between-maven-dependencies-org-apache-commons-commons-io-and-commons-io-commons-io

You can see where dependency is coming from with

gradle :main:dependencyInsight --configuration compile --dependency commons-io

Hawthorn answered 9/5, 2017 at 6:34 Comment(0)
D
5

There is a simple way to exclude the double classes. At first you need to find out which dependency is causing that if you know that use this code:

compile('com.example:some-dependency:4.2') {
    exclude module: 'commons-io'
}
Dustheap answered 4/2, 2016 at 19:21 Comment(2)
It appears to be commons-io itself. When I leave that out, both versions disappear from the library. I'll add the exclude to its own compilation but that seems to be kind of counterintuitive...Bihar
I suspect it is the fault of this line: compile fileTree(dir: 'libs', include: ['*.jar']) do you have a libs directory with jars?Dustheap
L
5

I know that this thread is old enough, but if someone faces this issue, the reason may be in the artifact itself.

com.apache.commons:commons-io:XXX has been moved to commons-io:commons-io:XXX and fetching of the old artifact may produce unexpected behavior.

Lakia answered 29/3, 2017 at 14:10 Comment(0)
M
1

It could be possible that other libraries in the project have the commons-io dependency causing duplicate entries.

See if this helps - Gradle Duplicate Entry: java.util.zip.ZipException

Momently answered 3/2, 2016 at 23:43 Comment(1)
It doesn't appear that this is the case. I've looked at that question and ruled it out. Looking through all my other libraries there don't seem to be any dependencies on commons_io, so I can't imagine why this is happening.Bihar

© 2022 - 2024 — McMap. All rights reserved.