android dex无法合并

时间:2017-12-09 14:40:58

标签: android gradle android-gradle dex

正在运行的项目面临此错误:

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

这是我的傻瓜:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.arnick.bazim"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "0.8"
        multiDexEnabled true //Add this
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        manifestPlaceholders = [onesignal_app_id               : "#onesingal_app_id",

                                onesignal_google_project_number: "REMOTE"]
    }
    applicationVariants.all { variant ->
        variant.resValue "string", "versionName", variant.versionName
    }
    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 fileTree(dir: 'libs', include: ['*.jar'])


    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:cardview-v7:26.0.2'
    compile 'com.android.support:design:26.0.2'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'com.android.support:customtabs:26.1.0'
    compile 'com.google.firebase:firebase-auth:11.6.2'
    compile 'com.google.android.gms:play-services-auth:11.6.2'

    compile('io.socket:socket.io-client:1.0.0') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
    }
    compile 'com.onesignal:OneSignal:3.6.5'
    compile 'com.android.volley:volley:1.0.0'


    testCompile 'junit:junit:4.12'
}


configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.0.2'
            }
        }
    }
}

android {
    defaultConfig {
        multiDexEnabled true
    }
}

我有什么特殊代码吗? 我应该提一下,这是我的一个旧项目,我已经4个月没有工作了,现在我又在研究它了。 4个月前一切都很好。 有谁知道这是什么问题?谢谢

1 个答案:

答案 0 :(得分:1)

实际上我可以在第一次尝试时编译你的配置,但是我看到了关于用于某个模块的com.google.android.gms版本的警告。

例如,运行gradle app:dependencies我可以注意到这一点:

+--- com.onesignal:OneSignal:3.6.5
|   +--- com.google.android.gms:play-services-gcm:[10.2.1,11.3.0) -> 11.2.2
|    |    +--- com.google.android.gms:play-services-base:11.2.2 -> 11.6.2 (*)
|    |    +--- com.google.android.gms:play-services-basement:11.2.2 -> 11.6.2 (*)
|    |    \--- com.google.android.gms:play-services-iid:11.2.2
|    |         +--- com.google.android.gms:play-services-base:11.2.2 -> 11.6.2 (*)
|    |         \--- com.google.android.gms:play-services-basement:11.2.2 -> 11.6.2 (*)
|    +--- com.google.android.gms:play-services-location:[10.2.1,11.3.0) -> 11.2.2
|    |    +--- com.google.android.gms:play-services-base:11.2.2 -> 11.6.2 (*)
|    |    +--- com.google.android.gms:play-services-basement:11.2.2 -> 11.6.2 (*)
|    |    \--- com.google.android.gms:play-services-tasks:11.2.2 -> 11.6.2 (*)
|    +--- com.android.support:support-v4:[26.0.0,26.2.0) -> 26.0.2 (*)
|    \--- com.android.support:customtabs:[26.0.0,26.2.0) -> 26.0.2 (*)

因此,例如com.google.android.gms:play-services-gcm:[10.2.1,11.3.0) -> 11.2.2引用版本11.2.2

虽然firebase引用版本11.6.2

|    +--- com.google.firebase:firebase-common:11.6.2
|    |    +--- com.google.android.gms:play-services-basement:11.6.2 (*)
|    |    +--- com.google.android.gms:play-services-tasks:11.6.2 (*)
|    |    \--- com.google.firebase:firebase-common-license:11.6.2
|    +--- com.google.android.gms:play-services-tasks:11.6.2 (*)
|    \--- com.google.firebase:firebase-auth-license:11.6.2

因此,既然您已经有了解决策略,我会改变它:

resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
        if (!requested.name.startsWith("multidex")) {
            details.useVersion '26.0.2'
        }
    }
    if (requested.group == 'com.google.android.gms') {
        details.useVersion '11.6.2'
    }

}

我的警告消失了,我希望它可以帮助你。

<强>更新

我确认运行命令

gradle app:transformDexArchiveWithExternalLibsDexMergerForDebug

正如您的例外中所述,我首先在您的配置中获得了相同的结果,然后使用我建议的更改成功构建。