com.android.dex.DexException:多个dex文件定义了Landroid/support/annotation/AnimRes; [英] com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;

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

问题描述

您好,我正在研究一个正在使用Android Studio的项目.我已经设置了所有内容,但是当我运行项目时,出现了以下错误.最近2天无法解决.我的项目中可能会导致该错误的问题

如果有人知道请帮助.

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

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'

解决方案

我的问题是,无论您将库链接到主项目的哪个位置,对于支持库和注释,两者之间都具有相同的依赖性.

如果您将库项目作为应用程序中的依赖项,则只需要将该依赖项放入库依赖项闭包中即可.

问题在于您有两个dex文件,因为有两个文件具有相同的名称,这是因为文件之间存在重叠,并且它们之间存在依赖关系.

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

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

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

针对您图书馆的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

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

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'

解决方案

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

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆