Android 构建 apk 时出错:dex-dexexception-multiple-dex-files

Android error while building apk : dex-dexexception-multiple-dex-files

在为我的 android 应用构建 APK 时显示以下错误:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/iid/zzb;

我找不到错误是什么。我的 module:app gradle 文件是:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "27.0.0"
    defaultConfig {
        applicationId "//my application"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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:design:26.+'
    compile 'com.android.support:support-v4:26.+'

    compile 'com.google.android.gms:play-services-fitness:11.0.0'
    compile 'com.google.firebase:firebase-messaging:9.0.1'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.google.code.gson:gson:2.6.1'

    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:recyclerview-v7:26.+'
    compile 'com.android.support:cardview-v7:26.+'
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

我在某处读到,除非你绝对需要在你的 build.gradle 中启用 multiDex,否则不要这样做!

但是当我从 gradle 文件中删除该行时,

 compile 'com.google.firebase:firebase-messaging:9.0.1'

然后构建apk成功。 因为我的应用程序需要 firebase,所以这不是我的解决方案。

谁能帮帮我?

您需要使用与 dependencies

相同的 version

使用这个

compile 'com.google.android.gms:play-services-fitness:11.0.0'
compile 'com.google.firebase:firebase-messaging:11.0.0'

而不是这个

compile 'com.google.android.gms:play-services-fitness:11.0.0'
compile 'com.google.firebase:firebase-messaging:9.0.1'

构建gradle文件

apply plugin: 'com.android.application'

android {
signingConfigs {
}

compileSdkVersion 26
buildToolsVersion "26.0.1"

defaultConfig {
    applicationId "com.imageditor.Image"
    minSdkVersion 19
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/notice.txt'
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt')
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7

}