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

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

dexarchiivemergenerexception:无法合并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'
}

____________ finding_________

当我没有导入第二个模块(解析器)时,应用程序没有在dex上崩溃,但当模块没有导入时,应用程序无法工作。: D: D


当前回答

[无法合并dex解决] 经过数小时的堆栈溢出 我解决了“无法合并DEX错误”

更新gradle中的所有com.android.support行到v27.1.0

原因- Android已经将它的支持库更新到v27.1.0,所以你必须将gradle文件中的所有Android支持行从26.1.0更改为27.1.0

确保存储库部分包含一个具有“https://maven.google.com”端点的maven部分。例如: allprojects { 存储库{ jcenter () maven { url“https://maven.google.com” } } }

原因:Android无法在SDK管理器中更新支持库,现在它使用maven.google.com来更新,所以你必须包括这个来使用27.1.0的支持库

修改后版本: 1. 清洁项目 2. 重建项目

其他回答

确保所有相同的源库具有相同的版本号。在我的情况下,我使用不同的谷歌api版本。我对所有谷歌库使用11.6.0,但谷歌地方api使用10.4.0。将谷歌place api更新到11.6.0后,问题修复。

请注意,我的multidex也为该项目启用。

检查build.gradle中是否有debugCompile和releaseCompile。将其更改为debugImplementation和releaseImplementation。这就是我如何解决它,gradle应该通过这个成功标记的指示弃用,但突然它卡住了这两个。

在我的例子中,错误是导入依赖项:

实现“com.firebaseui: firebase-ui: tripwire”

我没有使用它,当我删除它清洁项目并重新编译时,它开始工作。当然,我也做了上面那些人做的事:

在整个app.grade中更改遵从到实现 在整个app.grade中将testcomplie更改为testimplementation 确保multidexenabled为true 更新compileSdk到26 删除。gradle文件(在项目中找到),清理项目并重新编译

最后……

删除实现'com.firebaseui:firebase-ui:2.3.0'(因为我没有在我的代码中使用它)

一种可能性是:存在相同的库,但依赖项中有不同的版本。

我有这个问题与以下行在gradle文件:

编译文件树(包括:['*.jar'], dir: 'libs') 编译的com.google.code.gson: gson: 2.8.2

gson库在我的libs目录中,但使用的是以前的版本。 我从libs目录中删除了gson-2.3.1.jar,一切都恢复正常了。

删除.gradle目录。 再次运行应用程序。

笔记

gradle目录位于项目的根文件夹中。(你可能需要先显示隐藏文件。) 每次使用Android 3.0更新依赖模块时,我都必须这样做。(最近发布的Android Studio 3似乎已经解决了这个问题。)