无法合并dex

324

我有Android Studio Beta。我创建了一个新项目,编译了我的旧模块,但是当我尝试启动应用程序时,它没有启动,并显示以下消息:

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

com.android.builder.dexing.DexArchiveMergerException: 无法合并dex文件

但我不知道该如何解决这个错误。我已经谷歌搜索了几个小时,但是没有成功。

我的项目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
}

我的应用 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')


}

我的模块 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')

}

我的第二个模块:

     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'
}

在没有导入第二个模块(parser)时,应用程序不会在dex上崩溃,但当未导入该模块时,应用程序无法工作。:D :D

____________找到答案____________


6
如果有人在使用Android Studio 3.0稳定版本时遇到同样的问题,我强烈建议您查看 https://dev59.com/f1YN5IYBdhLWcg3wwKdZ#46971548 上的答案,并看看它是否能够帮助您。 - Bhavesh Patadiya
1
我曾经遇到同样的问题,我按照本页上提到的每种方法都尝试了,但都没有帮助到我,所以我去了Gradle并在每个库上按住光标检查它们是否有新版本可用。那些有新版本的我就更新了,同步 Gradle 并运行。这样就解决了问题。 - Shahzad Afridi
63个回答

1
我遇到了同样的问题。 我只需启用Instant Run(它之前是禁用状态),然后我的项目就可以正常工作了。 你可以在以下位置找到它 -
File->Settings-> Build,Execution,Deployment->Instant Run

在Android Studio 3.5中,即时运行已被移除。 请参考此处

在“构建、执行、部署”路径中没有“即时运行”,你的回答没有意义。 - Bay
@Bay 在 Android Studio 3.5 中,即时运行功能不可用。我的回答已更新。 - Rasel

0
    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.xyz.name"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 7
        versionName "1.6"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    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.volley:volley:1.0.0'
    implementation 'com.wang.avi:library:2.1.3'
    implementation 'com.android.support:design:27.1.0'
    implementation 'com.android.support:support-v4:27.1.0'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.0'
    implementation 'com.loopj.android:android-async-http:1.4.9'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    implementation 'com.felipecsl.asymmetricgridview:library:2.0.1'
    implementation 'com.android.support:recyclerview-v7:27.1.0'
    implementation 'com.github.darsh2:MultipleImageSelect:3474549'
    implementation 'it.sephiroth.android.library.horizontallistview:hlistview:1.2.2'
    implementation 'com.android.support:multidex:1.0.1'
}

apply plugin: 'com.google.gms.google-services'

注意:请将您的所有支持库更新到27.1.0,如上所示,并删除重复项。

0

检查依赖版本,如果它们不同,则使它们相同,这将解决您的问题。

compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support:recyclerview-v7:27.0.2'
compile 'com.android.support:cardview-v7:27.0.2'

0

在升级一些依赖项后,我找到了解决方案。我们应该使用最新的Play服务版本,在build.gradle[app]的依赖中。

compile 'com.android.support:multidex:1.0.2'
compile 'com.google.android.gms:play-services:11.8.0'
compile 'com.google.firebase:firebase-core:11.8.0'

build.gradle[project] 中,我们应该使用最新的 Google 插件。
classpath 'com.google.gms:google-services:3.1.1'

我也分享以下代码以便更好地理解。

    android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.***.user"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 26
        versionName "1.0.20"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true

        aaptOptions {
            cruncherEnabled = false
        }

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

0
对我来说,问题在于包含在项目中的两个JAR文件具有相同的类。
对于其中一个JAR文件,我需要排除这些类:
jar {
    exclude('com/files/to/exclude/**')
}

0

试试这样做,这对我有效。

在 app/build.gradle 文件中:

dependencies {
    ....

    compile project(':grow-notification')

    /** Firebase Start **/
    compile 'com.google.firebase:firebase-messaging:11.4.0'
    compile 'com.firebase:firebase-jobdispatcher:0.6.0'
    /** Firebase End **/
}

apply plugin: 'com.google.gms.google-services'

在模块 grow-notification/build.gradle 中:

dependencies {
    ....

    /** Firebase Start **/
    provided 'com.google.firebase:firebase-messaging:11.4.0'
    provided 'com.firebase:firebase-jobdispatcher:0.6.0'
    /** Firebase End **/
}

0

在构建项目时,我遇到了无法合并dexjava.lang.OutOfMemoryError: Java heap space的错误。

gradle.properties文件中,我发现以下行被注释掉了:

# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

取消注释此行并将堆内存增加到3g可解决问题:
org.gradle.jvmargs=-Xmx3g -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

0
    android {
        defaultConfig {
            multiDexEnabled true
            }
    }

   compile 'com.google.android.gms:play-services:11.0.4'

   compile 'com.google.android.gms:play-services-location:11.0.4'

清理构建并重新构建


0

只需在 build.gradle 文件中添加以下内容:

defaultConfig { multiDexEnabled true }


感谢您提供这段代码片段,它也许可以提供一些有限的、即时的帮助。通过展示为什么这是一个好的解决方案,一个适当的说明 将极大地提高其长期价值,并使它对未来其他类似问题的读者更有用。请编辑您的回答以添加一些解释,包括您所做出的假设。 - Dwhitz

0
将以下代码添加到你的 app/build.gradle 文件中,它会起作用:

implementation 'com.android.support:design:27.1.0'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接