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


当前回答

将buildToolsVersion更改为其他版本并同步,将其更改回来并再次同步。它会再次工作的。

其他回答

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

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

升级了一些依赖后,我找到了解决方案。我们应该使用最新的游戏服务版本。在构建。gradle (app)的依赖。

compile 'com.android.support:multidex:1.0.2'
compile 'com.google.android.gms:play-services:11.8.0'
compile 'com.google.firebase:firebase-core:11.8.0'

在构建。gradle[项目],我们应该使用最新的谷歌插件。

classpath 'com.google.gms:google-services:3.1.1'

为了更好地理解,我还分享了下面的代码。

    android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.***.user"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 26
        versionName "1.0.20"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true

        aaptOptions {
            cruncherEnabled = false
        }

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

我尝试了所有其他的解决方案,但没有一个适合我。最后,我通过编辑build.gradle使用相同的依赖版本解决了这个问题。我认为这个问题发生在添加一个库到gradle使用不同的依赖版本的支持或谷歌库。

将以下代码添加到构建gradle文件中。然后清理和重建项目。

Ps:对我来说,这是旧的解决方案,所以你应该使用更新版本 在库。

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
        if (!requested.name.startsWith("multidex")) {
            details.useVersion '26.1.0'
        }
    } else if (requested.group == "com.google.android.gms") {
        details.useVersion '11.8.0'
        } else if (requested.group == "com.google.firebase") {
            details.useVersion '11.8.0'
          }
      }
}

我也有同样的问题。我通过执行一个干净的,然后从Android Studio 3.0菜单构建项目来解决这个问题:

Build ->清洁项目 Build ->重建项目

只需在app/build.gradle中添加这一行

defaultConfig {
       multiDexEnabled true 
    }