Gradle build faild:无法合并dex

时间:2018-02-07 17:54:16

标签: android firebase gradle

我使用android studio 3.0.1创建项目并将firebase添加到应用程序。 当我尝试构建项目时,构建faild并得到以下异常。

构建错误 build error

Gradle构建脚本依赖项

apply plugin: 'com.android.application'
...
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.firebaseui:firebase-ui-database:3.1.0'
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation 'com.github.bumptech.glide:glide:4.3.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
}
apply plugin: 'com.google.gms.google-services'

Gradle构建项目级别。

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.1.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

添加multiDexEnabled trueimplementation 'com.android.support:multidex:1.0.1'后,我收到了以下错误

enter image description here 运行gradlew build后,我收到以下消息。

> Configure project :app
google-services plugin could not detect any version for com.google.android.gms or com.google.firebase, default version: 9.0.0 will be used.
please apply google-services plugin at the bottom of the build file.
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)

> Task :app:processDebugGoogleServices
Parsing json file: C:\Users\jem001.TGLCT\AndroidStudioProjects\OkpabackBackend\app\google-services.json


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 3m 32s
20 actionable tasks: 20 executed

请问如何解决?

2 个答案:

答案 0 :(得分:0)

将Multidex库添加到项目gradle

   android {
            compileSdkVersion 26
            buildToolsVersion "26.0.0"

        defaultConfig {
               minSdkVersion 21 
               targetSdkVersion 26
               multiDexEnabled true
              }
           }

dependencies {
    compile 'com.android.support:multidex:1.0.1'
    }

如果您正在使用单元测试,那么在基类的应用程序类中添加以下行

 public class ApplicationName extends MultiDexApplication {

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}}

之后将这些行添加到清单文件

 <application
    android:name="com.packageName.ApplicationName"
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/app_icon_new"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/MyAppTheme1"
    tools:replace="android:icon,theme">

答案 1 :(得分:0)

正如我在评论中提到的,我建议您先尝试清理项目并重建。

我能够使用这些脚本成功构建:

<强>项目

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        // These are later versions than you have.
        // I'm using Android Studio 3.2 Canary 1.  It requires build:gradle:3.2.0-alpha01
        classpath 'com.android.tools.build:gradle:3.2.0-alpha01'
        classpath 'com.google.gms:google-services:3.1.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用模块

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.your.app"
        minSdkVersion 16
        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 {
    // Copied from your post
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.firebaseui:firebase-ui-database:3.1.0'
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation 'com.github.bumptech.glide:glide:4.3.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
}
apply plugin: 'com.google.gms.google-services'