2

In the project, there are 3 modules, A,B,C. A is having dependency of B and B is having dependency of C.

Error Output:

Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\Users\user\AppData\Local\Android\sdk\build-tools\21.1.2\dx.bat --dex --no-optimize --output C:\Testing\MyApplication\app\build\intermediates\dex\debug --input-list=C:\Testing\MyApplication\a\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
    2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/test/test2/BuildConfig;
        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)

MainProject[settings.gradle]:

include ':A', ':B', ':C'

MainProject[build.gradle] :

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

A.gradle :

apply plugin: 'com.android.application'

dependencies {

    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(':B')
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    /*defaultConfig {
        minSdkVersion 9
        targetSdkVersion 21
    }*/

    lintOptions {
        abortOnError false
    }
}

B.gradle :

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile('de.keyboardsurfer.android.widget:crouton:1.8.5') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'de.greenrobot:eventbus:2.2.1'
    compile 'com.mobsandgeeks:android-saripaar:1.0.3'
    compile 'com.loopj.android:android-async-http:1.4.6'
    compile project(':C')
    compile 'com.android.support:appcompat-v7:21.0.3'
}

C.gradle :

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile files('libs/classes.jar')
}

If I am removing the duplication of appcpmpat-v7 in A and module B then I am getting the errors.

Module C contains the compiled .class files.

I am unable to understand the duplication of build.config files. What can be the solution of this problem?

Custadian
  • 845
  • 1
  • 11
  • 37
  • I'm going to make a guess and say that dependency B and/or C probably has the same package name as A. If so, then that's the underlying cause of the error you're seeing. Both generate a `com.test.test2.BuildConfig` class file, hence the error. The solution is to not have duplicate package names, but unique ones instead. See also [this Q&A](http://stackoverflow.com/questions/23236790/multiple-dex-files-define-lcom-amazon-ags-buildconfig-when-using-gamecirclesdk-i). – MH. Mar 03 '15 at 06:56

1 Answers1

3

The support library is included several times (maybe because declared as dependency by several of the libraries you're using), look for that in your build script.

resolution remove appcompat library from Gradle A.


Dependency resolution

When a project references two Library projects that both require the same jar file, the build system has to detect and resolve the duplication.

A full dependency system would associate each jar file with a fully qualified name and a version number to figure out which version to use.

Unfortunately the Android build system does not have a full dependency resolution system (yet). In the meantime we have implemented a very basic system that follows these rules:

Jar file are identified strictly by their file names.

This means mylib.jar is different than mylib-v2.jar and both will be packaged, possibly resulting in “already added” dx error if they are actually the same library in a different revision.

For jars with the same file name, “same version” means same exact file.

Currently our detection is very basic, checking only that the files are identical in size and sha1. If two libraries each include in their libs folder a file called mylib.jar but these two files are different, then the build system will fail indicating a dependency error.

check Android Dex: UNEXPECTED TOP-LEVEL EXCEPTION for more info.

Community
  • 1
  • 1
Maveňツ
  • 1
  • 12
  • 50
  • 89