Android Studio:任务执行失败:无法合并dex

时间:2018-04-06 23:46:02

标签: android android-studio android-gradle

我只是在我的android项目中添加一个模块。 Android Studio正确构建项目,但是当我尝试启动应用程序时,我收到此错误。

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

我的项目gradle:

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

buildscript {
    ext.kotlin_version = '1.2.21'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.apollographql.apollo:apollo-gradle-plugin:0.4.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

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

我的模块gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.apollographql.android'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.yamblet.smartliving"
        minSdkVersion 21
        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 {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':openpayandroid')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.jakewharton:butterknife:8.8.1'
    implementation 'com.airbnb.android:lottie:2.0.0'
    implementation 'org.apache.commons:commons-lang3:3.4'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.apollographql.apollo:apollo-runtime:0.4.4'
    implementation "com.apollographql.apollo:apollo-android-support:0.4.4"
    implementation 'com.apollographql.apollo:apollo-http-cache:0.4.4'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'jp.wasabeef:glide-transformations:2.0.1'
    implementation 'com.master.android:permissionhelper:1.1'
    implementation 'com.iamhabib:easy-preference:1.0.1'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.github.jkwiecien:EasyImage:2.0.2'

    //RETROFIT
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    //JSON PARSING
    implementation 'com.google.code.gson:gson:2.6.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'

    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.master.android:permissionhelper:1.1'

    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'


    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"


}
repositories {
    maven { url "https://jitpack.io" }
    mavenCentral()
}

apply plugin: 'kotlin-android-extensions'

我添加的模块gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 19
    buildToolsVersion "26.0.2"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
    }

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

dependencies {
    compile 'com.android.support:support-v4:19.+'
    compile files('libs/devicecollector-sdk-2.5.jar')
    compile files('libs/google-http-client-1.17.0-rc.jar')
    compile files('libs/google-http-client-android-1.17.0-rc.jar')
    compile files('libs/google-http-client-jackson2-1.17.0-rc.jar')
    compile files('libs/jackson-core-2.1.3.jar')
    compile files('libs/jackson-core-asl-1.9.11.jar')
    compile files('libs/jsr305-1.3.9.jar')
    compile files('libs/junit-4.10.jar')
}

我清理并重建项目但是,错误在构建过程中仍然存在。我也添加了multiDexEnabled true,但没有用。谢谢你的帮助。

4 个答案:

答案 0 :(得分:1)

确保您拥有$query ->select(array('a.id', 'a.promo', 'a.harga', 'a.dp', 'a.image', 'a.teaser' , 'b.title','b.created')) ->from($db->quoteName('#__cck_store_form_paket_trip', 'a')) ->join('LEFT', $db->quoteName('#__content', 'b') . ' ON (' . $db->quoteName('a.id') . ' = ' . $db->quoteName('b.id') . ')') ->where($db->quoteName('b.catid') . ' IN '.$db->quote(.'(13,14,15)'.) ->order($db->quoteName($orderby) . ' '.$order) ->setLimit($limit,$start) ;即。在Android部分的应用程序gradle中启用了multidex

答案 1 :(得分:1)

在build.gradle中使用默认配置       multiDexEnabled true 并添加依赖项,如 compile' com.android.support:multidex:1.0.3' 并使用 MultidexApplication 扩展您的应用程序文件并重建您的项目。这对你有帮助。

答案 2 :(得分:1)

我执行了这个gradle,它只能解决我能看到的问题implementation project(':openpayandroid'),我没有把它作为你的私人库包含在内,但据我所知,openpayandroid还包括一些相同的依赖关系导致问题,因此解决方案可以是从中排除该组,例如implementation project(':openpayandroid') { exclude group 'the dex error causing lib group' } 如果以上工作正在尝试这个  在你的项目build.gradle中尝试使用所有组的相同版本,例如。

if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex')) {
                details.useVersion "26.1.0" // your version
            }

我认为这可能有所帮助,但如果这不起作用,请尝试检查哪个文件导致此dex错误并编辑您的问题

你还有两件事包括权限助手两次,删除(但可能不是问题)

答案 3 :(得分:1)

如果您最近更新了依赖项,请尝试此答案

只需清洁项目并重新构建项目

Build -> Clean project 
Build -> Rebuild project