我有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 (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'
        }
    }
}

其他回答

不幸的是,Michel和Suragch的解决方案对我都不起作用。

我最终不得不做的就是回滚我的com.google。Firebase: Firebase -database到10.0.1版本,因为11.4.0在我的应用程序gradle文件中导致依赖不一致警告。

我遇到了同样的问题,并找到了我的真正原因。 之前我也把之前的所有答案都试了一遍,但是没有解决问题。 我有两个模块在我的穿着应用程序项目,和构建。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”。

当我从com.google.android.gms:play-services:11.2.2更新到com.google.android.gms:play-services:11.4.0时,我也遇到了同样的问题。这为我解决了问题:

清洁 重建

如果(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'

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

将此添加到app/build.gradle中。会有用的

实现“com.android.support:设计:27.1.0”