我有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


当前回答

我遇到了同样的问题,并找到了我的真正原因。 之前我也把之前的所有答案都试了一遍,但是没有解决问题。 我有两个模块在我的穿着应用程序项目,和构建。Gradle如下:

磨损模块的build.gradle:

implementation project(':common')
implementation files('libs/farmer-motion-1.0.jar')

通用模块的build.gradle:

implementation files('libs/farmer-motion-1.0.jar')

在升级到gradle 3之前。X, 'implementation'都是'compile'。

我用——stacktrace选项运行gradlew来获得堆栈跟踪,当出现这个问题时,你可以在gradle控制台窗口上单击这个。并发现依赖于jar包重复:

Caused by: com.android.dex.DexException: Multiple dex files define Lcom/farmer/motion/common/data/pojo/SportSummary$2;

类SportSummary在farmer-motion-1.0.jar包,在阅读官方迁移指南后,我改变了我的构建。Gradle如下:

磨损模块的build.gradle:

implementation project(':common')
// delete dependency implementation files('libs/farmer-motion-1.0.jar')

build.gradle for common module:

api files('libs/farmer-motion-1.0.jar') // change implementation to api

现在磨损模块将有依赖农民运动1.0.jar导出由公共模块。 如果运行时对jar包没有依赖,也可以将jar包的“implementation”依赖更改为“compileOnly”。

其他回答

如果(1)。尝试清洁和重建工作)然后很好

Else if (2;试着去除gradle工作)然后很好

- > 3。尝试添加grade.properties

android.enableD8 = false

编辑2021:第三个选项现在已弃用,使用其他选项

- > 4。在build.gradle中添加multiDexEnabled true

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

并添加依赖项

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

也许第一个对你有用,但这真的取决于你的问题的性质,比如对我来说

一旦我添加了这个库,我就得到了错误

implementation 'com.jjoe64:graphview:4.2.2'

后来我发现我必须检查一下,我必须添加相同版本的支持库。所以我不得不尝试另一个版本

compile 'com.jjoe64:graphview:4.2.1'

这就解决了问题。所以要注意这一点。

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

注意警告!

有时你只需要消除警告,错误就会自动消失。特殊情况见下文:


在我的模块级构建中有这两个依赖项。gradle文件:

implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'

和工作室已经警告(除了dex合并问题):

所有com.android.support库必须使用完全相同的版本规范(混合版本会导致运行时崩溃)。找到27.0.2、21.0.3版本。例如com.android.support:animated-vector-drawable:27.0.2和com.android.support:support-v4:21.0.3

所以我明确地确定了com.android的版本。支持:Support -v4(详情请参阅这里),两个问题(警告和与dex合并相关的问题)都解决了:

implementation 'com.android.support:support-v4:27.0.2'  // Added this line (according to above warning message)
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'

其他类似情况请参见下面的评论。

我也有这个问题。

我能够通过将compileSdkVersion和targetSdkVersion更改为最新版本来解决。

如果在包含kotlin支持后出现此错误,并且其他解决方案都不起作用,请尝试更改应用模块构建的kotlin依赖项。gradle:

implementation ("org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version") {
    exclude group: 'org.jetbrains', module: 'annotations'
}

这适用于我的Android Studio 3.0 Beta 6。请参阅下面的答案以了解进一步的解释。