Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior

Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior

构建项目时出现以下错误。没有在这个项目中使用 CoordinatorLayout 。刚刚添加为 build.gradle 中的依赖项:

我正在使用 Android Studio 3.2 Canary 4。

LogCat

AGPBI: {"kind":"error","text":"Program type already present: android.support.design.widget.CoordinatorLayout$Behavior","sources":[{}],"tool":"D8"} :app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: /windows/Other/app/build/intermediates/transforms/dexBuilder/debug/0.jar, /windows/Other/app/build/intermediates/transforms/dexBuilder/debug/1.jar, /windows/Other/app/build/intermediates/transforms/dexBuilder/debug/4.jar, . . ...................

/windows/Other/app/build/intermediates/transforms/dexBuilder/debug/294.jar

Program type already present: android.support.design.widget.CoordinatorLayout$Behavior

build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "com.dagkot"
        minSdkVersion 16
        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'
            buildConfigField "String", "BASE_URL", "\"http://api.openweathermap.org/data/2.5/\""
            buildConfigField "String", "API_KEY", "\"435e9075f348868c2714fe7c6895efa5\""
        }
        debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", "BASE_URL", "\"http://api.openweathermap.org/data/2.5/\""
        buildConfigField "String", "API_KEY", "\"xxxx\""
    }
}
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"


    // Dagger dependencies
    compileOnly 'org.glassfish:javax.annotation:10.0-b28'
    implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"

    //Butterknife dependencies
    implementation 'com.jakewharton:butterknife:8.8.1'
    kapt 'com.jakewharton:butterknife-compiler:8.8.1'

    // Architecture Components Dependencies
    kapt "android.arch.lifecycle:compiler:$rootProject.lifeCycle"
    implementation "android.arch.lifecycle:extensions:$rootProject.lifeCycle"
    implementation "android.arch.lifecycle:reactivestreams:$rootProject.lifeCycle"
    implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"

    // Retrofit/RxJava Dependencies
    implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
    implementation "io.reactivex.rxjava2:rxandroid:$rootProject.rxAndroidVersion"
    implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0'
    implementation 'com.jakewharton.rxbinding2:rxbinding-kotlin:2.1.1'

    // GSON
    implementation "com.google.code.gson:gson:$rootProject.gsonVersion"

    // Rx Location Manager
    implementation 'io.nlopez.smartlocation:library:3.3.3'
    implementation 'io.nlopez.smartlocation:rx:3.3.1'

    //Anko Dependencies
    implementation "org.jetbrains.anko:anko-commons:$rootProject.anko_version"
    implementation "org.jetbrains.anko:anko-design:$rootProject.anko_version"
    implementation 'com.android.support:design:27.0.2'

    implementation("com.github.hotchemi:permissionsdispatcher:3.1.0") {
        // if you don't use android.app.Fragment you can exclude support for them
        exclude module: "support-v13"
    }
    kapt "com.github.hotchemi:permissionsdispatcher-processor:3.1.0"
}

我遇到了同样的问题, 我添加了 android 支持设计依赖到应用级别 build.gradle

添加以下内容:

implementation 'com.android.support:design:27.1.0'

在 build.gradle 中。现在它为我工作。

当我降级支持 appcompat gradle 依赖时,它起作用了,如下所示:

implementation 'com.android.support:appcompat-v7:27.0.2'

之前是

implementation 'com.android.support:appcompat-v7:27.1.0'

这也可以通过 添加支持设计依赖性 来解决 27.1.0 或更高版本到您的应用级别 build.gradle,如下所示:

implementation 'com.android.support:design:27.1.0'

使用最新supportLibrary、版本27.1.1解决问题。为我工作。 (包括许多错误修复 - see changelog

将所有依赖项更改为编译而不是实现,然后我重建项目没有错误。然后我切换回实现而不是将其保留为编译。

使用

implementation 'com.android.support:appcompat-v7:27.1.1'

不要使用 like

implementation 'com.android.support:appcompat-v7:27.+'

它可能会给你一个错误,不要使用比这更旧的版本。

或者事件不这样做

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1' 

等...图书馆数量然后

implementation 'com.android.support:appcompat-v7:27.+'

相同的库,但版本不同,可能会出错。

因为 android 最新更新不支持 'compile' 关键字使用 'implementation' 在模块 build.gradle 文件中.

并在 build.gradle 中彻底检查是否依赖于 + 符号。

implementation 'com.android.support:support-v4:28.+'

如果有任何这样的依赖项,只需将它们更新为特定版本即可。 之后:

  1. 同步gradle.
  2. 清理你的项目。
  3. 重建项目。

可能是库的原因,我是因为Glide才遇到的。

implementation 'com.github.bumptech.glide:glide:4.7.1'

所以我加了exclude group: "com.android.support"就变成了

implementation ('com.github.bumptech.glide:glide:4.7.1') {
        exclude group: "com.android.support"
    }

如果此处提到的任何答案均无效,请转至“文件”>“无效”Catches/Restart

我正在使用 android studio 3.0 我将设计模式依赖项从 26.0.1 升级到 27.1.1,现在错误消失了。

在gradle中添加关注 implementation 'com.android.support:design:27.1.1'

确保这两个在您的应用级别build.gradle文件中是相同的版本

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'

我认为应该可以解决问题

我也遇到过这个问题;我以这种方式解决了这个问题:

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

注意我的最小 SDK 是 26,你必须换成你的!

这个问题的解决方案是删除以下依赖项:

implementation 'com.android.support:design:26.1.0'

将一般依赖项设置为:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:support-compat:26.1.0'
    implementation 'com.android.support:multidex:1.0.3'    
    implementation 'com.android.support:support-v4:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.facebook.android:audience-network-sdk:4.99.1'
}

我知道这是一个迟到的答案,但我遇到了同样的问题,我的解决方案只是添加 implementation 'com.android.support:design:28.0.0 或任何以上支持设计库!!

我降级支持

previously it was
implementation 'com.android.support:appcompat-v7:27.0.2'

使用它

implementation 'com.android.support:appcompat-v7:27.1.0'

implementation 'com.android.support:design:27.1.0'

工作愉快

重要更新

Android 28.0.0 后将不再更新支持库。根据 Support Library Release Notes -

This will be the last feature release under the android.support packaging, and developers are encouraged to .

所以请改用 AndroidX 支持库。在您的案例中,设计库现在可以在 material 包中使用。

dependencies {
    implementation 'com.google.android.material:material:1.0.0' // instead of design
    implementation 'androidx.appcompat:appcompat:1.0.2' // instead of support-v7
}

我已将最新版本放在依赖项中,您可以在阅读时check latest version此处。

有用的帖子:

  1. Some Do's and Dont's

就个人而言,我将以下行添加到我的 app/build.gradle:

implementation "com.android.support:design:${rootProject.ext.supportLibVersion}"

使用此语法,版本是动态的。

将此添加到 项目的 gradle.properties 为我们修复了它:

android.enableJetifier=true
android.useAndroidX=true

您的构建脚本应与应用程序 build.gradle 依赖项相匹配。

ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 16
        compileSdkVersion = 27
        targetSdkVersion = 26
        supportLibVersion = "27.1.1"
    }


dependencies {
    .................
    ...................

    implementation 'com.android.support:support-v4:27.1.0'
    implementation 'com.android.support:design:27.1.0'
    ................
    ...........
}

如果您想降级依赖项,那么还要降级 supportLibVersion 和 buildToolsVersion。

"Program type already exists" 删除你的 /build 目录内容,生成的二进制文件有一些 Dex(?) 问题。和你一样得到了这些答案,他们帮助解决了这个问题,但也创造了许多其他问题。构建内容删除适用于所有人。

我也遇到了同样的问题。 但后来我意识到我使用的支持库版本不一样。

一旦我做到了,错误就消失了。

你的情况

implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:design:27.0.2'

不一样,所以您刚刚将 appcompat 降级为

implementation 'com.android.support:appcompat-v7:27.0.2'

至此,你的问题解决了。

但是如果你可以将支持设计版本升级到

,你也可以解决
implementation 'com.android.support:design:27.1.0'

转到放置其他库的目录并删除重复的库。

当一个库多次加载到 gradle 时,可能会发生这种情况。最常见的是通过其他连接的图书馆。

删除一个实现 这个库在 build.gradle

然后构建 -> 清除项目

你可以 运行 程序集)