更新到Android Studio 3.0时出现“无法合并dex”错误?

3
我更新到Android Studio 3.0后出现了这个错误。我尝试了许多类似问题的解决方法,但是都没有效果。
这里是错误信息: enter image description here 我发现当我移除库'com.google.android.gms:play-services-maps:11.4.2'时,我的项目可以成功构建。但我需要在我的项目中使用这个库,请问有人知道如何解决这个问题吗?
我的完整build.gradle文件如下:


buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
dependencies { classpath 'io.fabric.tools:gradle:1.+' } } apply plugin: 'com.android.application' apply plugin: 'io.fabric'
repositories { maven { url 'https://maven.fabric.io/public' } google() }
android { compileSdkVersion 25 buildToolsVersion '26.0.2' defaultConfig { applicationId "vn.com.ttsoft.dhd" minSdkVersion 21 targetSdkVersion 25 versionCode 14 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" useLibrary 'org.apache.http.legacy' multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
debug { // Disable fabric build ID generation for debug builds ext.enableCrashlytics = false } } productFlavors { } compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } }
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile files('libs/android-async-http-1.4.4.jar') compile files('libs/google-play-services.jar') compile files('libs/gson-2.3.1.jar') compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') { transitive = true; } compile 'com.github.pavlospt:roundedletterview:1.2' compile 'com.github.PhilJay:MPAndroidChart:v3.0.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:appcompat-v7:25.4.0' compile 'com.android.support:design:25.4.0' compile 'com.android.support:support-v4:25.4.0' compile 'com.google.android.gms:play-services-maps:11.4.2' compile 'me.tatarka.support:jobscheduler:0.1.1' compile 'com.evernote:android-job:1.1.8' testCompile 'junit:junit:4.12' compile files('libs/activation.jar') compile files('libs/additionnal.jar') compile files('libs/mail.jar') }
这是一个Gradle构建脚本,用于在Android应用程序中使用Fabric和一些其他依赖项。其中包括Google Play服务、Espresso测试框架、Android异步HTTP库、Gson JSON解析器以及一些UI库等。

@war_Hero 我更新了问题并添加了完整的 build.gradle 代码。 - Jacky
@AmjadKhan 它出现了相同的错误。 - Jacky
我也遇到了同样的问题。 - Krishna Meena
我在IDE中清除了缓存,然后删除了项目文件中的.gradle目录,并重新运行了应用程序。这对我很有帮助。 - Mohammed Farhan
你正在编译Play Services三次。在此处...编译com.google.android.gms:play-services-maps:11.4.2,在此处... compile files('libs/google-play-services.jar'),最后在此处... compile fileTree(include: ['*.jar'], dir: 'libs')。Gradle 2.3.3宽容一些。Gradle 3.0.0不喜欢多个Dex并抛出构建错误。要么删除.jar文件,要么删除compile 'com.google.android.gms:play-services-maps:11.4.2' - Simon Hutton
显示剩余9条评论
5个回答

0
你从文件中加载了哪个版本的Play Services?我猜这是个问题,你试图同时加载整个Play Services和部分play-services-maps。这也解释了当你移除地图时一切正常的事实。
请查看此链接以选择性编译Play Services API,这也有助于减少方法计数。 https://developers.google.com/android/guides/setup

为什么扣了1分?:/ 问题是OP试图加载.jar库,而不是 - tatocaster
不是我,我没有点击向下箭头 :) 无论如何,感谢您的信息。 - Jacky

0
对于正在使用android-async-http并尝试过启用DEX使缓存无效删除.gradle文件检查重复导入/版本的人:
我在org.apacheandroid-async-http包中遇到了一些冲突,
当我将org.apache.http.Header更改为cz.msebera.android.httpclient.Header时,我的问题得到了解决。
{       
 useLibrary 'org.apache.http.legacy'  // avoid this
}

dependencies {
    compile files('libs/android-async-http-1.4.4.jar') // only keep this one
}

0

我正在放置Gradle文件,你可以使用它,这可能会对你有所帮助

apply plugin: 'com.android.application'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}
android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "vn.com.ttsoft.dhd"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            // Disable fabric build ID generation for debug builds
            ext.enableCrashlytics = false
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    compile files('libs/android-async-http-1.4.4.jar')
    compile files('libs/google-play-services.jar')
    compile files('libs/gson-2.3.1.jar')
    compile('com.crashlytics.sdk.android:crashlytics:2.7.1@aar') {
        transitive = true;
    }
    compile 'com.github.pavlospt:roundedletterview:1.2'
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support:design:25.3.0'
    compile 'com.android.support:support-v4:25.3.0'
    compile 'me.tatarka.support:jobscheduler:0.1.1'
    compile 'com.evernote:android-job:1.1.8'
    testCompile 'junit:junit:4.12'
    compile files('libs/activation.jar')
    compile files('libs/additionnal.jar')
    compile files('libs/mail.jar')
}
apply plugin: 'io.fabric'

还有另一个项目的Gradle

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'io.fabric.tools:gradle:1.+'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

0

我也遇到了同样的问题,我认为是Google Play服务依赖项出了问题,所以我通过添加以下所有Google Play服务依赖项来解决了这个问题:

compile 'com.google.android.gms:play-services-base:11.4.2'
compile 'com.google.android.gms:play-services-auth:11.4.2'
compile 'com.google.firebase:firebase-core:11.4.2'
compile 'com.google.firebase:firebase-messaging:11.4.2'
compile 'com.google.android.gms:play-services-gcm:11.4.2'
compile 'com.google.android.gms:play-services-location:11.4.2'
compile 'com.google.android.gms:play-services-maps:11.4.2'
compile 'com.google.android.gms:play-services-places:11.4.2'

0

我在升级到AS 3.0 / Gradle 3.0.0后遇到了'Multiple Dex'构建错误的类似问题。

我发现,无效缓存和重启、清理项目以及minifyEnabled“解决方案”都是误导。

我为此苦苦挣扎了几个小时,然后找到了导致问题的原因。

我使用语句编译了Picasso、GSON和SQLiteAssetHelper。

        compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
        compile 'com.google.code.gson:gson:2.8.1'
        compile 'com.squareup.picasso:picasso:2.5.2'

...但我也将 .jar 文件编译自我的 \libs 文件夹,其中包括 sqliteassethelper、GSON 和 Picasso。

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

因此出现了重复项。
是的-很明显,但有时你看不见大局!
我希望这可以帮助某人。
(最后注:Gradle 3.0.0中已弃用`compile`命令,因此您可能希望将上述命令从`compile`更改为`implementation`)

此答案涉及Android 3.0稳定版本,而非Beta 4版本。因此,它已从Beta 4问题中删除,并重新用于此问题。当然,可能会有类似但不同的问题。除非搜索者来到这里,否则他们可能找不到相关的答案。我最初发布了这个答案的链接,但被其他人编辑掉了。 - Simon Hutton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接