简体   繁体   English

Android Studio Gradle错误“多个dex文件定义...”

[英]Android Studio Gradle error “Multiple dex files define…”

My Project was working fine, when I have added facebook sdk into my project I have an error like this, I have tried so many ways to fix this, but I didn't. 我的项目工作正常,当我将facebook sdk添加到我的项目中时我有这样的错误,我已经尝试了很多方法来解决这个问题,但我没有。 What should I do? 我该怎么办?

  Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: bolts/AggregateException.class

My App gradle is below 我的App gradle在下面

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        applicationId "com.example.myproject"
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        multiDexEnabled = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

    compile project(':facebook')
}

And here is the facebook build.gradle 这是facebook build.gradle

apply plugin: 'com.android.library'

repositories {
  mavenCentral()
}

project.group = 'com.facebook.android'

dependencies {
    compile 'com.android.support:support-v4:[21,22)'
    compile 'com.parse.bolts:bolts-android:1.1.4'
}

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 21
    }

    lintOptions {
        abortOnError false
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

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
}

Now they split bolts-android into bolts-applinks and bolts-tasks .so you need exclude both from the gradle build 现在他们将bolt-android拆分为bolt-applinks和bolt-tasks。所以你需要从gradle构建中排除这两者

compile ('com.facebook.android:facebook-android-sdk:4.10.0'){
exclude group: 'com.parse.bolts',
        module: 'bolts-tasks'
exclude group: 'com.parse.bolts',
        module: 'bolts-applinks';}

This works perfectly for me !!!! 这对我来说非常有用!!!!

For me, I was adding Facebook SDK as a project, and set it as dependencies. 对我来说,我将Facebook SDK添加为项目,并将其设置为依赖项。

However, the exclude work after i switching to use the maven source. 但是,切换到使用maven源后,排除工作。

I think it is for maven only, not for project dependencies? 我认为它仅适用于maven,而不适用于项目依赖项? (please provide correct info if someone know about this) (如果有人知道,请提供正确的信息)

In other word, you can now delete the Facebook SDK project and files. 换句话说,您现在可以删除Facebook SDK项目和文件。

remember to add 记得加

repositories {
    mavenCentral()
}

if you weren't using maven. 如果你不使用maven。

So the build.gradle look like this, I commented out the project way. 所以build.gradle看起来像这样,我评论了项目的方式。

repositories {
    mavenCentral()
}
dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':google-play-services_lib')
    compile ('com.facebook.android:facebook-android-sdk:3.23.0'){
        exclude module: 'bolts-android'
        exclude module: 'support-v4'
    }
//    compile (project(':FacebookSDK')){
//        exclude module: 'bolts-android'
//        exclude module: 'support-v4'
//    }
    compile (project(':UserVoiceSDK')){ exclude module: 'support-v4' }
}

I've had a similar problem. 我有类似的问题。 This was really frustrating for me because everything worked fine and suddenly it broke for no reason. 这对我来说真的很令人沮丧,因为一切都很好,突然间它无缘无故地破了。

The issue is hinted in duplicate entry: bolts/AggregateException.class . 这个问题在duplicate entry: bolts/AggregateException.class暗示duplicate entry: bolts/AggregateException.class It's a clash of the Bolts library, used by Facebook and Parse. 这是Bolts图书馆的冲突,由Facebook和Parse使用。

For me, the problem lay in these two lines: 对我来说,问题在于以下两点:

compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.parse.bolts:bolts-android:1.1.4'

I had downloaded the Parse libraries and put them in the /libs/ folder. 我下载了Parse库并将它们放在/libs/文件夹中。 The problem was that there was another bolts-android file in that folder. 问题是该文件夹中还有另一个bolts-android文件。

The solution is to delete that library and keep the compile 'com.parse.bolts:bolts-android:1.1.4' part. 解决方案是删除该库并保持compile 'com.parse.bolts:bolts-android:1.1.4'部分。

Alternative problem 替代问题

In my case, I used compile 'com.parse.bolts:bolts-android:1.+' instead of a specific version. 就我而言,我使用了compile 'com.parse.bolts:bolts-android:1.+'而不是特定版本。 This always takes the latest version. 这始终采用最新版本。 So when bolts upgraded to version 1.2.0 , the thing just seemed to randomly break because all of a sudden the version in the /libs/ folder and the latest version no longer aligned. 所以当bolts升级到版本1.2.0 ,事情似乎随机破坏,因为/libs/文件夹中的版本和最新版本突然不再对齐。

Best practice is to avoid 1.+ style versioning and just keep checking and updating to the latest version every now and then. 最佳做法是避免1.+样式版本控制,并且不时地检查和更新到最新版本。

Hope this helps someone. 希望这有助于某人。

no need to remove any jar files. 无需删除任何jar文件。 In Gradle file we have written these two lines 在Gradle文件中,我们写了这两行

compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.parse.bolts:bolts-android:1.1.4'

just remove 只是删除

compile fileTree(dir: 'libs', include: ['*.jar'])

because we are compiling all the jar files and then again including the bolts to compile due to which the error is shown 因为我们正在编译所有jar文件,然后再次包含要编译的螺栓,因为显示错误

in my case the i had added a .jar in the library code . 在我的情况下,我在库代码中添加了一个.jar。 the library in turn gets used in the main app . 该库反过来在主应用程序中使用。 the dex was still in the cache file even if i had cleaned my project and installed it . 即使我已经清理了我的项目并安装了它,dex仍然在缓存文件中。 To be sure in the main app you can check the count of the library . 为了确保在主应用程序中,您可以检查库的计数。 The cache file is "Project -> build -> dex-cache -> cache.xml" . 缓存文件是“Project - > build - > dex-cache - > cache.xml”。 If you have multiple counts of the library then u need to do this in Android Studio -> File -> invalidate cache / restart 如果您有多个库计数,那么您需要在Android Studio中执行此操作 - >文件 - >使缓存/重启无效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM