com.android.dex.DexException: Multiple dex files d

2019-02-10 04:25发布

问题:

Hello I am working on a project for which I am using Android Studio. I have setup everything but when I run my project then I get below errors. I could not resolve it for last 2 days. What could be the problem in my project that causing this error

Please help if anyone know about this.

app build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.+'
    //compile files('libs/android-support-v4.jar')
    compile 'com.android.support:support-v4:22.0.+'
    compile 'com.android.support:support-annotations:20.0.0'
}

library build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 22
    }

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

dependencies {
    //compile files('libs/android-support-v4.jar')
    compile 'com.android.support:support-v4:22.0.+'
    compile 'com.android.support:support-annotations:20.0.0'
}

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.1.0'

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

allprojects {
    repositories {
        jcenter()
    }
}

...

UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
        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)
    Error:Execution failed for task ': app:dexDebug'.

settings.gradle

include ':app'
include ':multiStateToggleButton'

回答1:

Your problem i believe is that wherever you are linking the Library to your Main Project you have the same dependencies between the two for your support library and annotations.

If you have the library project as a dependency in your application you will only need the dependency to be placed in the library dependencies closure.

The issue is that you have two dex files because there are two Files with the same name because the overlap in files with your dependencies.

First copy your module to your libs/ folder of your main project then,

create your settings.gradle file in the root of the main project:

include 'app_name', 'library_name'
project(':LibraryNameGoesHere').projectDir = new File('libs/LibraryNameGoesHere')

For your library's build.gradle

dependencies {
    compile files('libs/android-support-v4.jar')
    compile 'com.android.support:support-v4:22.0.+'
    compile 'com.android.support:support-annotations:20.0.0'
}

Then for your main project build.gradle

dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:22.0.+'
     compile project(":libs:LibraryNameGoesHere")
}


回答2:

Since facebook sdk configed for using Android 2.3.3, it requires annotaion lib. My app configed for using Anndoid > 4.x.x, which is contains Annotation, the conflict was emarged. I have changed, in the facebbok mainfest, to work with Android > 4.x.x and it solved the problem.



回答3:

if you migrate the project from eclipse to studio , and then your project need a new module, you add the build.gradle which in the module, add the dependencies like this,

compile 'com.android.support:support-annotations:24.1.1'
compile 'com.android.support:support-v4:24.1.1'
//recyclerview
compile 'com.android.support:cardview-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.3'

you might be see this stupid problem because the old project has include the jar file like android-support-v4.jar this shit is overlay the compile(thing) so you must remove the *.jar file, this shit takes my hole afternoon, so good luck ,my english is pool, fogiven me please



回答4:

For what it's worth I was getting this error after using Android Studio to import a project from Eclipse. In the /app/build.gradle file I had two entries in the dependencies section, it looked like this

dependencies {
    compile files('libs/android-support-v13.jar')
    compile files('libs/android-support-v4.jar')
}

I removed the reference to v4 like below

dependencies {
    compile files('libs/android-support-v13.jar')
}

I cleaned the project and was able to build my APK. I don't know if this was the correct way to fix it but it worked for me.