错误:程序类型已存在:android.support.design.widget.CoordinatorLayout$Behavior
错误:程序类型已存在: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" }
错误原因:出现该错误的原因是因为在项目中同时引入了不同版本的support design库,导致重复定义了同一个类。
解决方法:有几种解决方法可以尝试。首先,可以降级support appcompat依赖的版本为27.0.2。其次,可以在app级别的build.gradle文件中添加support design库的依赖,版本号为27.1.0或更高。此外,还可以尝试将依赖的版本号改为compile,如将compile 'com.android.support:design:27.1.0'修改为implementation 'com.android.support:design:27.1.0'。如果以上方法都不起作用,可以尝试添加support design依赖并进行项目的clean和rebuild操作来解决问题。
(Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior)这个问题的出现的原因是由于库开始使用了27.1.0版本,而应用仍然使用26.x.x版本导致的版本不兼容。解决方法是在app级别的build.gradle文件中添加以下依赖项:
implementation 'com.android.support:design:27.1.0'
如果问题无法立即解决,可以尝试清理/重建项目。这是我成功解决问题的方法。
通常这种错误是由于版本不兼容引起的。如果使用了appcompat和design库,最好使用最新版本。例如:implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.1.1'。
如果遇到类似的错误,可以使用./gradlew -q yourApp:dependencies --configuration debugRuntimeClasspath
命令检查重复依赖项,并确保不要依赖不同版本的同一库。
在这个链接中,有人遇到了类似的问题,可以帮助解决。