Unable to merge dex

2018-12-31 02:28发布

I have Android Studio Beta. I created a new project with compile my old modules but when I tried launching the app it did not launch with the message:

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

But I don't know how to solve this error. I googled this for hours but with no success.

My project gradle:

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

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta6'
        classpath "io.realm:realm-gradle-plugin:3.7.1"
        classpath 'com.google.gms:google-services:3.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

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

My app gradle:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "parad0x.sk.onlyforyou"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
        }
    }
    compileOptions {
        targetCompatibility 1.7
        sourceCompatibility 1.7
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
    lintOptions {
        checkReleaseBuilds false
    }
    productFlavors {
    }
}

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'
    })
    //noinspection GradleCompatible
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile project(path: ':loginregisterview')


}

And my module gradle:

    apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        minSdkVersion 19
        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:appcompat-v7:26.0.2'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'com.github.bumptech.glide:glide:4.0.0'
    testCompile 'junit:junit:4.12'
    compile project(path: ':parser')

}

My second module:

     apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    realm {
        syncEnabled = true
    }
    useLibrary 'org.apache.http.legacy'

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile 'junit:junit:4.12'
    //  compile 'com.android.support:appcompat-v7:23.1.0'

    //   compile 'com.fasterxml.jackson.core:jackson-core:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
    compile 'com.google.code.gson:gson:2.6.2'
}

____________finding_________

When I did not import the second module (parser) the app did not crash on dex but when the module was not imported app did not work. :D :D

30条回答
心情的温度
2楼-- · 2018-12-31 03:17

[ UNABLE TO MERGE DEX SOLVED ] After hours of stack overflowing I resolved the " UNABLE TO MERGE DEX ERROR "

  1. Update all the com.android.support lines in your gradle to v27.1.0

Cause - Android has updated it support libraries to v27.1.0, so you have to change all the android support lines in your gradle file to 27.1.0 from 26.1.0

  1. Make sure that the repositories section includes a maven section with the "https://maven.google.com" endpoint. For example:

    allprojects { repositories { jcenter() maven { url "https://maven.google.com" } } }

Cause :- Android cannot update the support libraries in the SDK manager and now it uses maven.google.com to update , so you have to include this to use 27.1.0 support libraries

After Change Version: 1. Clean Project 2. Rebuild Project

查看更多
余生请多指教
3楼-- · 2018-12-31 03:18

One of the possibilities is: the presence of the same library but with different versions in the dependencies.

I had this problem with the following lines in gradle file:

  • compile fileTree(include: ['*.jar'], dir: 'libs')
  • compile 'com.google.code.gson:gson:2.8.2'

The gson library was in my libs directory but with a previous version. I deleted the gson-2.3.1.jar from the libs directory and everything is back to normal.

查看更多
公子世无双
4楼-- · 2018-12-31 03:18

I encountered the same problem and found the real reason for my case. Previously, I also tried all the previous answers again, but it did not solve the problem. I have two module in my wear app project, and the build.gradle as follows:

wear module's build.gradle:

implementation project(':common')
implementation files('libs/farmer-motion-1.0.jar')

common module's build.gradle:

implementation files('libs/farmer-motion-1.0.jar')

Before upgrade to gradle 3.x, 'implementation' are all 'compile'.

I run gradlew with --stacktrace option to get the stack trace, you can just click this on gradle console window when this problem arises. And found that dependency to the jar package repeated:

Caused by: com.android.dex.DexException: Multiple dex files define Lcom/farmer/motion/common/data/pojo/SportSummary$2;

Class SportSummary in the farmer-motion-1.0.jar package, after read the official migration guide, i changed my build.gradle to follows:

wear module's build.gradle:

implementation project(':common')
// delete dependency implementation files('libs/farmer-motion-1.0.jar')

common module的build.gradle:

api files('libs/farmer-motion-1.0.jar') // change implementation to api

Now wear module will has the dependency of farmer-motion-1.0.jar export by common module. If there has no dependency on jar package during runtime, 'implementation' dependency of jar package can also be change to 'compileOnly'.

查看更多
十年一品温如言
5楼-- · 2018-12-31 03:19
android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
查看更多
临风纵饮
6楼-- · 2018-12-31 03:21

I tried every other solution, but no one worked for me. At the end, i solved it by using same dependency version by editing build.gradle. I think this problem occurres when adding a library into gradle which uses different dependency version of support or google libraries.

Add following code to your build gradle file. Then clean and rebuild project.

ps: that was old solution for me so you should use updated version of following libraries.

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
        if (!requested.name.startsWith("multidex")) {
            details.useVersion '26.1.0'
        }
    } else if (requested.group == "com.google.android.gms") {
        details.useVersion '11.8.0'
        } else if (requested.group == "com.google.firebase") {
            details.useVersion '11.8.0'
          }
      }
}
查看更多
人气声优
7楼-- · 2018-12-31 03:22

This may not be your problem, but I got this error when I accidentally included two identical (but differently named) libraries in the dependencies{} section of the project.

查看更多
登录 后发表回答