android - 构建我的项目时无法合并 dex 错误

标签 android gradle

当我尝试构建我的项目时,我得到了 Unable to merge dex 错误。我确实在其他线程上尝试了所有解决方案,但我找不到适合我的解决方案......所以请不要立即将其标记为重复。

所以这是确切的错误:

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

我的 build.gradle(project) 文件:
// 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.1'

        // 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
}

和我的模块 build.gradle:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    //buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.example.leodr.upgradeadmin"
        minSdkVersion 15
        targetSdkVersion 27
        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(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.android.support:appcompat-v7:27.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

作为外部库,我使用 GSON。

最佳答案

尝试启用multidex

修改你的 build.gradle

dependencies {
    compile 'com.android.support:multidex:1.0.1'
}

defaultConfig {
    multiDexEnabled true

}

在您的 list 文件中定义您的应用程序名称属性,如下所示
android:name="com.pkg.YouApplication"

将此包含在您的应用程序类中
public class YouApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

}

关于android - 构建我的项目时无法合并 dex 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47560595/

相关文章:

android - 为 Galaxy S8/S8+ 指定的屏幕密度

android - fragmentActivity setarguments 无法解析

spring - 在eclipse上使用gradle导入另一个项目上的项目

java - 错误 : Could not find com. android.tools.build :gradle:3. 5.2

android - Retrofit2.1 附带的默认 OkHttp 版本?

使用 SimpleAdapter 的 Android ListActivity - 突出显示所选项目

android - 当软键盘可见时,编辑文本隐藏在标题栏后面

android - 使用 gradle 每晚构建 android 项目

gradle - JUnit 5 Gradle:确实必须在测试类后缀 “Test(s)”吗?

android - Gradle错误版本1.2.3