Mark Korzhov
Mark Korzhov

Reputation: 2159

Unable to exclude duplicate libraries in Gradle

I'm trying to connect library to my project in Gradle:

compile 'com.balancedpayments:balancedpayments:1.4'

When I click "Sync Now" project building without errors, but when I run my project IDE shows errors:

Executing tasks: [:app:assembleDebug]

Configuration on demand is an incubating feature. :app:preBuild :app:compileDebugNdk UP-TO-DATE :app:preDebugBuild :app:checkDebugManifest :app:preReleaseBuild :app:prepareComAndroidSupportAppcompatV72103Library UP-TO-DATE :app:prepareComAndroidSupportSupportV42103Library UP-TO-DATE :app:prepareDebugDependencies :app:compileDebugAidl UP-TO-DATE :app:compileDebugRenderscript UP-TO-DATE :app:generateDebugBuildConfig UP-TO-DATE :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets UP-TO-DATE :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources UP-TO-DATE :app:mergeDebugResources UP-TO-DATE :app:processDebugManifest UP-TO-DATE :app:processDebugResources UP-TO-DATE :app:generateDebugSources UP-TO-DATE :app:compileDebugJava UP-TO-DATE :app:preDexDebug UP-TO-DATE :app:dexDebug UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Lorg/apache/commons/codec/Decoder; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106)

FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:dexDebug'.

    com.android.ide.common.internal.LoggedErrorException: Failed to run command: C:\Users\zen_75\AppData\Local\Android\sdk\build-tools\21.1.2\dx.bat --dex --no-optimize --output C:\skip\TestPayment\app\build\intermediates\dex\debug --input-list=C:\skip\TestPayment\app\build\intermediates\tmp\dex\debug\inputList.txt Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Lorg/apache/commons/codec/Decoder; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106)

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

I found the description of the problem - it is a conflict of libraries. To solve this problem are advised to eliminate conflicting library. To do this, I do so:

configurations {
    all*.exclude module: 'httpclient'
    all*.exclude module: 'commons-logging'
}

Warnings disappear, but the error remains. Can you please tell how to eliminate these libraries.

Thanks.

Upvotes: 1

Views: 2419

Answers (2)

Egos Zhang
Egos Zhang

Reputation: 1364

I had a similar problem when I use HttpClient in api 23. I import org.apache.http.legacy.jar the problem appear. I use useLibrary 'org.apache.http.legacy' solve it.

Upvotes: 0

Kevin-Prichard
Kevin-Prichard

Reputation: 163

"com.android.dex.DexException: Multiple dex files define "

This vexing error is caused when two or more copies of a .class file or jarfile's contents have been inadvertently included into a project. (Newbies to Gradle and/or Android Studio get blocked by this one often enough).

  1. Try excluding it in your app's top-level build.gradle-

    android {
        ...
        packagingOptions {
            exclude('org/apache/commons/codec/*')
        }
    }
    
  2. Check if the library was included as dependency more than once: review all of your project's build.gradle files to ensure it's been included only once.

     compile 'org.apache.commons.code' ...
    
  3. A library or subproject may also includes a copy of the duplicated library or classfile: you can exclude it where the library is compiled into your app, look for its compile spec in your build.gradle file(s)-

    dependencies {
        ...
        compile('com.googlecode.json-simple:json-simple:1.1.1') {
            exclude group: 'org.hamcrest'
        }
    }
    
  4. If that doesn't work, try appending ", module: 'library'" to the exclude line, i.e.-

        compile('com.googlecode.json-simple:json-simple:1.1.1') {
            exclude group: 'org.hamcrest', module: 'library'
        }
    
  5. To determine exactly which library or subproject is including the duplicated classfiles, try running gradle with --debug and reviewing the console output in order to trace where that inclusion originated-

    % gradle --debug release 2>&1 | less
    

Upvotes: 1

Related Questions