com.android.dex.DexException:多个dex文件定义Landroid / support / annotation / AnimRes;

前端之家收集整理的这篇文章主要介绍了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'
}

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'

解决方法

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

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

问题是您有两个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")
}

猜你在找的Android相关文章