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 02:55
  1. Delete the .gradle directory.

  2. Run your app again.

Notes

  • The .gradle directory is in your project's root folder. (You may have to show hidden files first.)
  • I have to do this every time I update a dependency module using Android 3.0. (More recent releases of Android Studio 3 seem to have resolved the problem.)
查看更多
零度萤火
3楼-- · 2018-12-31 02:55

Hi I have same issue tried almost everything. So, finally i resolved after 6 hour long struggle by debugging everything line by line.

classpath 'com.google.gms:google-services:3.0.0'

Google-services 3.0 Doesn't support firebase with Studio 3.0 with playServiceVersion: 11.6.0 or less.

implementation "com.google.firebase:firebase-messaging:$rootProject.ext.playServiceVersion"
implementation "com.google.firebase:firebase-core:$rootProject.ext.playServiceVersion"
implementation "com.firebase:firebase-jobdispatcher-with-gcm-dep:$rootProject.ext.jobdispatcherVersion"

Solution :

I have change google services to

classpath 'com.google.gms:google-services:3.1.1'

And it support firebase services.

Hopefully somebody save his/her time.

查看更多
十年一品温如言
4楼-- · 2018-12-31 02:55

if(1. Try to clean and rebuild work ) then good

else if (2. Try to remove gradle work ) then good

else-> 3. Try to add in grade.properties

android.enableD8 = false

else-> 4. Add multiDexEnabled true to your build.gradle

android {
    compileSdkVersion 26
    defaultConfig {
      ...
        minSdkVersion 15
        targetSdkVersion 26
        multiDexEnabled true
     ...
    }
}

and add the dependency

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

It may the first one works for u and so on but it really depends on the nature of your problem for me for example

I got the error once I have added this library

implementation 'com.jjoe64:graphview:4.2.2'

and later I discovered that I have to check that and I have to add the same version of the support libraries. So I have to try another version

compile 'com.jjoe64:graphview:4.2.1'

and it fixes the problem. So pay attention for that.

查看更多
琉璃瓶的回忆
5楼-- · 2018-12-31 02:58

Installing Google play services (latest version) + including

android {
    defaultConfig {
        multiDexEnabled true
        }
}

in build.gradle solved the issue for me, make sure to clean and rebuild project!

查看更多
姐姐魅力值爆表
6楼-- · 2018-12-31 02:59

I tried all above and none of them helps, finally, i find this work for me:

app/build.gradle:

android {
    defaultConfig {
       multiDexEnabled true
    }
}
查看更多
泪湿衣
7楼-- · 2018-12-31 02:59

add commands below:

android {
...

    dexOptions {

        jumboMode true
        javaMaxHeapSize "4g"

    }
}
查看更多
登录 后发表回答