我有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,我有一个错误Java .lang. outofmemoryerror:构建项目时的Java堆空间。

在gradle里。我有这样的注释行:

# 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

其他回答

检查依赖版本,如果它们不相同,然后使它相同,这将解决你的问题

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'

简单的解决方法对我很有效:

不要编译/实现相同依赖关系的两个版本。 如果你直接从gradle编译,不要在项目的libs文件夹中使用.jar文件。上述观点在这里也适用。

然后清理和重建项目。

注意警告!

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


在我的模块级构建中有这两个依赖项。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'

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

如果你正在使用

compile ("org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.1")

你应该把它改成

compile ("org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.1"){
    exclude group:'org.apache.oltu.oauth2' , module: 'org.apache.oltu.oauth2.common'
}

由于客户端库已经包含了公共库,它会创建一个重复的类条目,并给出一个错误:

无法合并dex

如果上面的答案对您不起作用,您的问题可能是您有多个依赖于同一个库的依赖项。

下面是一些调试技巧。在这个示例代码中,com.google.code.findbugs:jsr305:3.0.0是有问题的库。

每次修改以检查解决方案时,都要清理并重新构建!

Build with the --stacktrace flag on for more detail. It will complain about a class, Google that class to find the library. Here's how you can set up Android studio to always run gradle with the --stacktrace flag. Take a glance at the Gradle Console in Android Studio View > Tool Windows > Gradle Console after a build Check for repeated dependences by running ./gradlew -q app:dependencies. You can re-run this each time you modify the your build.gradle. In build.gradle, android { ... configurations.all { resolutionStrategy { // Force a particular version of the library // across all dependencies that have that dependency force 'com.google.code.findbugs:jsr305:3.0.0' } } } In build.gradle, dependencies { ... implementation('com.google.auth:google-auth-library-oauth2-http:0.6.0') { // Exclude the library for this particular import exclude group: 'com.google.code.findbugs' } } In build.gradle, android { ... configurations.all { resolutionStrategy { // Completely exclude the library. Works for transitive // dependencies. exclude group: 'com.google.code.findbugs' } } } If some of your dependencies are in jar files, open up the jar files and see if there are any conflicting class names. If they are, you will probably have to re-build the jars with new class names or look into shading.

更多背景阅读:

Android -理解并支配gradle依赖 Gradle文档:Java项目的依赖项管理 Gradle文档:resoltionstrategy 错误:与依赖项“com.google.code.findbugs:jsr305”冲突 StackOverflow:当使用Gradle时,我如何排除所有可传递依赖的实例? 谷歌搜索StackOverflow所有相关问题