无法合并dex - 如何修复?

时间:2017-12-25 10:28:45

标签: java android

我使用Android Studio 3.0.1版。 我的项目出现了无法合并dex。

  

错误:任务':app:transformDexArchiveWithExternalLibsDexMergerForDebug'的执行失败。   java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法合并   DEX

我曾尝试过

方法A:

1.clean project
2.rebuild project 

方法B:

android {
    defaultConfig {
       multiDexEnabled true
    }
}

但仍然没有工作:(

以下是我的gradle文件设置,任何人都可以帮我查看问题吗? THX!

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    useLibrary 'org.apache.http.legacy'

defaultConfig {
    multiDexEnabled true
    applicationId "edu.berkeley.calhacks.pictureperfect"
    minSdkVersion 21
    targetSdkVersion 16  
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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


dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:25.1.0'
    compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    compile "org.apache.httpcomponents:httpmime:4.2.3"
    compile 'com.android.support:multidex:1.0.1'
}

2 个答案:

答案 0 :(得分:1)

如果您使用的是Google服务,那么您的项目 gradle 应该如下所示 -

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

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.0.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
}

用于处理Multidexing并在App level gradle中使用Google服务,

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.example.hp_pc.cerv"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    dexOptions {
        preDexLibraries = false
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
//    compile files('libs/httpclient-4.2.3.jar')
//    compile files('libs/httpmime-4.3.jar')
//    compile files('libs/httpcore.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:23.4.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.mcxiaoke.volley:library:1.0.+'
    compile 'com.github.pinball83:masked-edittext:1.0.3'
   // compile 'com.google.android.gms:play-services-maps:9.8.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.wdullaer:materialdatetimepicker:2.5.0'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'joda-time:joda-time:2.9.4'
    testCompile 'junit:junit:4.12'
}

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

AndroidManifest.xml 文件

中添加这两行
<application
    android:hardwareAccelerated="true"
    android:largeHeap="true"

答案 1 :(得分:1)

我正在使用Android Studio 3.0并且遇到了同样的问题。我把它添加到我的gradle中:

multiDexEnabled true

然后在您的应用程序类中添加它:

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

然后清理和synk项目。 但如果你尝试了所有并且还没有工作的话  更改targetSdkVersion = 26并且compileSdkVersion也设置为26。 并尝试这个。