无法在项目的gradle中合并dex

时间:2018-03-24 14:59:06

标签: android gradle

当我尝试重建项目时出现此错误:

return db.collection('users').get()
  .then((snapshot) => {
    const allPromises = snapshot.map((doc) => {
      const user = doc.data();
      const obj = {};
      obj.id = user.id;
      obj.name = user.name;
      obj.matches = [];
      return getMatches(user.param1, user.param2)
        .then(matchPromise => {
          obj.matches.push(res);
          list.push(obj);
        });
    });
    return Promise.all(allPromises);
  })
  .then(() => {
    ... sorts list ...
  })
  .then(() => {
    ... returns list to database ...
  });

尽管我添加了这一行

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

在我的app模块gradle文件中,错误如下所示。

multiDexEnabled true

我在重建项目后也进行了清理,但我遇到了同样的问题。

下面是我的app模块的gradle:

Error:Execution failed for task ':app:transformClassesWithMultidexlistForDebug'.
> java.io.IOException: Can't write [C:\Users\Amine\AndroidStudioProjects\Movies\app\build\intermediates\multi-dex\debug\componentClasses.jar] (Can't read [C:\Users\Amine\AndroidStudioProjects\Movies\app\build\intermediates\transforms\desugar\debug\19.jar(;;;;;;**.class)] (Duplicate zip entry [19.jar:android/support/design/widget/CoordinatorLayout$Behavior.class]))

在我的项目模块的gradle文件下面:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 27
    buildToolsVersion '27.0.0'
    defaultConfig {
        applicationId "com.aoutir.mohamed.movies"
        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'
        }
    }

    buildTypes.each {
        it.buildConfigField("String", "MOVIES_END_POINT", "\"https://api.themoviedb.org\"")
        it.buildConfigField("String", "APPIKEY", "\my API Key")
    }
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
}
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 'com.android.support:recyclerview-v7:27.0.0'
    compile 'com.android.support:cardview-v7:27.0.0'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.okhttp3:okhttp:3.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
    compile 'com.android.support:appcompat-v7:27.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:support-v4:27.+'
    compile 'com.android.support:design:27.0.1'
    testCompile 'junit:junit:4.12'
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是因为您有重复的支持库。问题的根源是版本的+用法。你不应该使用这个:

compile 'com.android.support:appcompat-v7:27.+'
compile 'com.android.support:support-v4:27.+'

因为gradle将使用27的最新版本27.1.0(请查看Recent Support Library Revisions)。然后您的依赖项块将是这样的:

dependencies {
    //...

    compile 'com.android.support:recyclerview-v7:27.0.0'
    compile 'com.android.support:cardview-v7:27.0.0'

    //...

    compile 'com.android.support:appcompat-v7:27.1.0'
    compile 'com.android.support:support-v4:27.27.1.0'
}

这是完全不同的版本。

但等等,您还可以使用以下内容:

compile 'com.android.support:design:27.0.1'

这是另一个版本。真正的问题是,支持设计库隐式使用appcompat-v7support-v4库。所以,上面这一行与此相同:

compile 'com.android.support:design:27.0.1'
compile 'com.android.support:appcompat-v7:27.0.1'
compile 'com.android.support:support-v4:27.0.1'

您的问题的解决方案是,您需要使用相同版本的支持库。像这样:

dependencies {

    compile 'com.android.support:recyclerview-v7:27.1.0'
    compile 'com.android.support:cardview-v7:27.1.0'
    compile 'com.android.support:design:27.1.0'
    // adding appcompat-v7 and support-v4 is not necessary 
    //when using support design.

   // ...
}