构建失败:程序类型已存在

Build Failed: Program Type Already Present

我在网上搜索过这个错误,这个问题似乎总是以某种依赖冲突告终。我想我应该在某些依赖项之后添加 exclude,但我不确定是哪一个。根据错误,我实际上应该排除哪个依赖项也不清楚;我所知道的是 group 可能是 com.android.support...

这是我试过的方法:

  1. multiDexEnabled true 添加到我在 build.gradle 中的 defaultConfig 区块。
  2. 正在 Android Studio 中清理项目。
  3. 手动删除(从文件系统)整个 .gradle 目录。
  4. 我确定 compileSdkVersiontargetSdkVersion 是一样的。
  5. 我确保我所有的 Android 依赖项都使用相同的版本(即 26.1.0)。

尽管如此,我在构建时仍然遇到此错误:

Program type already present: android.support.compat.R$bool

并且来自 Java 编译器:

Caused by: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
F:\ExampleProject\app\build\intermediates\transforms\dexBuilder\debug5, 
F:\ExampleProject\app\build\intermediates\transforms\externalLibsDexMerger\debug[=11=]

这是我模块的 build.gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.myapp.exampleproject"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.2"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.android.support:support-compat:26.1.0'
    implementation 'com.android.volley:volley:1.1.0'
    implementation 'com.github.bumptech.glide:glide:4.1.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.1.0'
}

最后,我项目的 build.gradle 文件:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

如能帮助纠正此问题,我们将不胜感激。

这可能是因为 Glide 库。尝试从中排除支持库:

// add support-fragment removed from Glide.
implementation "com.android.support:support-fragment:26.1.0"

implementation ('com.github.bumptech.glide:glide:4.1.0') {
   exclude group: 'com.android.support'
   exclude module: 'support-fragment'
   exclude module: 'appcompat-v7'
}

您可以在 Glide build.gradle

查看 Glide 内部的支持库

这是 Glide 4.1.0 的问题。使用版本 4.1.1 代替,它通过从依赖项中删除 R*.class 文件来修复错误。 (source)

解决方案:

 implementation ('com.github.bumptech.glide:glide:4.1.0') {
        exclude group: 'com.android.support'
        exclude module: 'support-fragment'
        exclude module: 'appcompat-v7'
    }