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


当前回答

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

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

其他回答

在我的例子中,我导入了一个'android_support' lib两次,所以我在'libs'目录下删除了一个并重新构建。

如果你在你的插件中使用Firebsae,这个问题会因为播放服务版本问题而出现,试试这个 “cordov

cordova.system.library.4=com.google.gms:google-services:3.3.0 cordova.system.library.5=com.google.android.gms:play-services-tagmanager:+ cordova.system.library.6=com.google.firebase:firebase-core:16.0.1 cordova.system.library.7=com.google.firebase:firebase-messaging:17.1.0 cordova.system.library.8=com.google.firebase:firebase-crash:16.0.1 cordova.system.library.9=com.google.firebase:firebase-config:16.0.0 cordova.system.library.10=com.google.firebase:firebase-perf:16.0.0

改变你的项目。属性像这样,那么这个问题将得到解决 参考

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

所以我通过以下方法解决了这个问题:

在gradle:3.0中,compile配置现在已弃用,应该由实现或api取代。要了解更多信息,你可以在Gradle Build Tool上阅读官方文档

编译配置仍然存在,但不应该被使用,因为它不会提供api和实现的保证 配置提供。

最好使用实现或API,而不是编译

只需将compile替换为implementation,将debugCompile替换为debugImplementation,将testCompile替换为testImplementation,将androidtestcompile替换为androidTestImplementation

例如:而不是这个

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'

像这样使用

implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.github.bumptech.glide:glide:4.0.0'

在那之后

删除项目中的。gradle文件夹(注意,为了看到。gradle,你需要切换到左上角导航器中的“项目”视图) 删除所有的构建文件夹和gradle缓存。 从“构建”菜单中,按下“清洁项目”按钮。 任务完成后,从Build菜单中按下Rebuild Project按钮。

希望对大家有所帮助!

在我的例子中,错误是导入依赖项:

实现“com.firebaseui: firebase-ui: tripwire”

我没有使用它,当我删除它清洁项目并重新编译时,它开始工作。当然,我也做了上面那些人做的事:

在整个app.grade中更改遵从到实现 在整个app.grade中将testcomplie更改为testimplementation 确保multidexenabled为true 更新compileSdk到26 删除。gradle文件(在项目中找到),清理项目并重新编译

最后……

删除实现'com.firebaseui:firebase-ui:2.3.0'(因为我没有在我的代码中使用它)

在我的情况下,一个库使这个问题,库已成功添加到项目,但当我运行我的应用程序,它显示我这个错误。所以如果你也遇到了这种情况,你可以去github检查问题或提出新的问题。如果你没有找到任何解决方案关于图书馆,我建议你更换它。