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


当前回答

在我的案例中,问题在于房间图书馆:

compile 'android.arch.persistence.room:runtime:1.0.0-alpha1'

改为:

compile 'android.arch.persistence.room:runtime:1.0.0'

工作。

其他回答

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

笔记

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

对我来说,它是在应用程序\build.gradle中更新firebase消息:

compile 'com.google.firebase:firebase-messaging:10.0.1'

to

compile 'com.google.firebase:firebase-messaging:11.4.2'

对我来说,问题是我在项目中包含的两个jar中有相同的类。

对于其中一个罐子,我需要排除类:

jar {
    exclude('com/files/to/exclude/**')
}

添加如下命令:

android {
...

    dexOptions {

        jumboMode true
        javaMaxHeapSize "4g"

    }
}

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

下面是一些调试技巧。在这个示例代码中,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所有相关问题