本文介绍了com.android.dex.DexException: 多个 dex 文件定义了 Landroid/support/annotation/AnimRes;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发一个使用 Android Studio 的项目.我已经设置了一切,但是当我运行我的项目时,我会遇到以下错误.过去 2 天我无法解决它.我的项目中可能是什么问题导致了这个错误

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

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'
}

库 build.gradle

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

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

settings.gradle

include ':app'
include ':multiStateToggleButton'

推荐答案

我相信您的问题是,无论您将库链接到主项目,您的支持库和注释在两者之间都有相同的依赖关系.

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.

问题是你有两个 dex 文件,因为有两个同名的文件,因为文件与你的依赖项重叠.

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.

首先将您的模块复制到您的主项目的 libs/文件夹中,然后

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

在主项目的根目录中创建 settings.gradle 文件:

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

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

对于你图书馆的 build.gradle

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'
}

然后为你的主项目 build.gradle

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")
}

这篇关于com.android.dex.DexException: 多个 dex 文件定义了 Landroid/support/annotation/AnimRes;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 05:40
查看更多