意外的顶级异常:com.android.dex.DexException:多个dex文件定义Landroid / support / annotation / AnimRes

时间:2015-04-22 12:42:29

标签: android android-studio android-gradle build.gradle android-build

我在尝试运行应用程序时遇到此问题。 我没有在我的代码中使用任何注释,但仍然拥有它。

my app build.gradle

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "mobi.greenon.greenon"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':facebook')
    compile files('libs/android-async-http-1.4.6.jar')
    compile 'com.google.android.gms:play-services:6.5.87'
}

我的facebook build.gradle

apply plugin: 'com.android.library'

repositories {
  mavenCentral()
}

project.group = 'com.facebook.android'

dependencies {
    compile 'com.parse.bolts:bolts-android:1.1.4'
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 8
    }

    lintOptions {
        abortOnError false
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

apply plugin: 'maven'
apply plugin: 'signing'

def isSnapshot = version.endsWith('-SNAPSHOT')
def ossrhUsername = hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
def ossrhPassword = hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""

task setVersion {
    // The version will be derived from source
    project.version = null
    def sdkVersionFile = file('src/com/facebook/FacebookSdkVersion.java')
    sdkVersionFile.eachLine{
        def matcher = (it =~ /(?:.*BUILD = \")(.*)(?:\".*)/)
        if (matcher.matches()) {
          project.version = matcher[0][1]
          return
        }
    }
    if (project.version.is('unspecified')) {
      throw new GradleScriptException('Version could not be found.', null)
    }
}

uploadArchives {
    repositories.mavenDeployer {
        beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

        repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
            authentication(userName: ossrhUsername, password: ossrhPassword)
        }

        snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
            authentication(userName: ossrhUsername, password: ossrhPassword)
        }

        pom.project {
            name 'Facebook-Android-SDK'
            artifactId = 'facebook-android-sdk'
            packaging 'aar'
            description 'Facebook Android SDK'
            url 'https://github.com/facebook/facebook-android-sdk'

            scm {
                connection 'scm:git@github.com:facebook/facebook-android-sdk.git'
                developerConnection 'scm:git@github.com:facebook/facebook-android-sdk.git'
                url 'https://github.com/facebook/facebook-android-sdk'
            }

            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'https://github.com/facebook/facebook-android-sdk/blob/master/LICENSE.txt'
                    distribution 'repo'
                }
            }

            developers {
                developer {
                    id 'facebook'
                    name 'Facebook'
                }
            }
        }
    }
}

uploadArchives.dependsOn(setVersion)

signing {
    required { !isSnapshot && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}

artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}

afterEvaluate {
    androidJavadocs.classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
}

我的项目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()
    }
}

我的setting.gradle

include ':app', ':facebook'

错误是 -

Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /home/eyal/android-sdk-linux/build-tools/21.1.2/dx --dex --no-optimize --output /home/eyal/Projects/GreenOn/Software/GreenOn/app/build/intermediates/dex/debug --input-list=/home/eyal/Projects/GreenOn/Software/GreenOn/app/build/intermediates/tmp/dex/debug/inputList.txt
  Error Code:
    2
  Output:
    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)

如何解决? 谢谢,Eyal。

4 个答案:

答案 0 :(得分:2)

在编译之前尝试清理项目。这对我有用。

答案 1 :(得分:2)

从Libs文件夹中删除android-support-v4.jar,我这样做了,它可以正常工作

答案 2 :(得分:0)

试试这个

configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'

}

dexOptions {
    javaMaxHeapSize "4g"
}

defaultConfig {
        multiDexEnabled true
    }

compile 'com.android.support:multidex:1.0.1'

  MultiDex.install(getBaseContext());//Application class

答案 3 :(得分:-1)

将此添加到build.gradle

android{
....
packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}