错误:程序类型已经存在:android.arch.lifecycle.LiveData

26
当我在Android Studio中按下运行按钮时,我的应用程序会编译,但会显示此错误(已编辑)。
Error:Program type already present: android.arch.lifecycle.LiveData

(完整日志)

我尝试删除了 .gradle 文件夹,然后进行了 Build > Clean ProjectBuild > Rebuild Project。但是,这并没有起作用。我还尝试过删除源代码,然后再次从 git 克隆并将文件夹导入到 Android Studio 中。然而,它仍然会产生同样的错误。

以下是我的应用程序的 app/build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "com.edricchan.studybuddy"
        minSdkVersion 24
        targetSdkVersion 27
        versionCode 8
        versionName "1.0.0-rc.503"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        resConfigs "en"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation 'com.android.support:support-v4:27.1.0'
    implementation 'com.android.support:support-annotations:27.1.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:27.1.0'
    implementation 'com.android.support:cardview-v7:27.1.0'
    testImplementation 'junit:junit:4.12'
    // Firebase stuff
    implementation 'com.google.firebase:firebase-storage:11.8.0'
    implementation 'com.google.firebase:firebase-firestore:11.8.0'
    implementation 'com.google.firebase:firebase-auth:11.8.0'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    implementation 'com.google.android.gms:play-services-auth:11.8.0'
    implementation 'com.firebaseui:firebase-ui-auth:3.1.0'
    implementation 'com.firebaseui:firebase-ui-firestore:3.1.0'
    implementation 'com.firebaseui:firebase-ui-storage:3.1.0'
    // Provide a way to update the app
    implementation 'com.github.javiersantos:AppUpdater:2.6.4'
    // Chrome Custom Tabs
    implementation 'com.android.support:customtabs:27.1.0'
    // The app's intro screen
    implementation 'com.heinrichreimersoftware:material-intro:1.6.2'
    // Use for new Material Text field boxes recently introduced
    implementation 'com.github.HITGIF:TextFieldBoxes:1.3.7'
    // Report an issue to Github without having to open a new tab and so on...
    implementation 'com.heinrichreimersoftware:android-issue-reporter:1.3.1'
}
apply plugin: 'com.google.gms.google-services'

我刚刚向Google的问题跟踪器报告了此问题:https://issuetracker.google.com/issues/74130804 - Edric
10个回答

27

显然,这是预期的行为:

com.firebaseui:firebase-ui-firestore:3.1.0依赖于android.arch.lifecycle:extensions:1.0.0-beta1。切换到版本3.2.2通过使用基于Support Library 27.1.0构建的Lifecycle 1.1库解决了该问题。- 问题跟踪器

对于我来说,删除 firebase-ui 依赖项解决了此问题,因为我一开始根本没有使用该库。


1
终于,升级Android Studio的漫长道路结束了...谢谢! - Christopher Mills
3
我甚至没有使用firebase-ui,但仍然遇到了这个错误。 - Ege Kuzubasioglu
@EgeKuzubasioglu,你能发一个新问题吗? - Edric
1
@Edric 发现一个外部库合并 dex 的问题导致了这个错误,我从我的项目中移除了那个库。 - Ege Kuzubasioglu
在我的情况下,删除与燃料相关的依赖项(com.github.kittinunf.fuel)起了作用。 - user3561494

26

今天我遇到了完全相同的问题,当我提高支持库版本时。

尝试使用'27.0.2'替换所有'27.1.0'


后来我通过升级其他库成功地解决了这个错误。 这是我的当前工作状态: 根gradle:

buildscript {
    ext.kotlin_version = '1.2.21'
    ext.support_version = '27.1.0'
    ext.anko_version = '0.10.4'
    ext.android_plugin_version = '3.0.1'
    ext.google_services_version = '11.8.0'

    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"

        classpath 'com.android.tools.build:gradle:3.1.0-beta4'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.1.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

应用程序 Gradle 库:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
        transitive = true
    }

    // kotlin:
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation "org.jetbrains.anko:anko-common:$anko_version"
    implementation "org.jetbrains.anko:anko-commons:$anko_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.3'

    // support libraries:
    implementation "com.android.support:recyclerview-v7:$support_version"
    implementation "com.android.support:support-v4:$support_version"
    implementation "com.android.support:design:$support_version"
    implementation "com.android.support:appcompat-v7:$support_version"
    implementation "com.android.support:cardview-v7:$support_version"
    implementation "com.android.support:support-vector-drawable:$support_version"

    // misc:
    implementation 'com.github.d-max:spots-dialog:0.7@aar'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'
    implementation 'com.backendless:backendless:4.4.0'
    implementation 'io.nlopez.smartlocation:library:3.3.3'

    // Google services:
    implementation "com.google.firebase:firebase-core:$google_services_version"
    implementation "com.google.firebase:firebase-auth:$google_services_version"
    implementation 'com.firebaseui:firebase-ui-auth:3.2.2'

    implementation "com.google.android.gms:play-services-location:$google_services_version"
    implementation "com.google.android.gms:play-services-auth:$google_services_version"

    implementation('com.google.api-client:google-api-client:1.23.0') {
        exclude group: 'com.google.code.findbugs', module: 'jsr305'
    }
}

我还将gradle-wrapper.properties升级为:

#Wed Dec 20 15:08:34 CET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

后来,我通过升级其他库成功解决了这个错误。 - Hoornet
谢谢!降级解决了问题。你在降级后是如何解决的? - Edric
你升级了哪些库?我看不出来。 - Edric
1
我刚刚向Google的问题跟踪器报告了此问题:https://issuetracker.google.com/issues/74130804 - Edric

21

这篇文章是非常相似的错误的顶部搜索结果: “Program type already present: android.arch.lifecycle.ViewModelProvider$Factory

我的项目使用了Room和LiveData,但没有使用firebase。以下更改解决了错误:

从:

implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
implementation 'android.arch.lifecycle:extensions:1.0.0'
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0'

致:

implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
implementation 'android.arch.lifecycle:extensions:1.1.1'
annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'

---更新的答案---

我的早期回答旨在解决此错误。但是,我认为再次使用最佳实践呈现它值得一提:

应用程序级别的build.gradle文件:

// Room components
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"

// Lifecycle components
implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"

项目级别的 build.gradle 文件:

ext {
   roomVersion = '1.1.1'
   archLifecycleVersion = '1.1.1'
}

参考资料:
https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#2


1
谢谢!这是唯一一个有效的答案。虽然我没有使用 Room,但升级生命周期依赖项有帮助,现在我的项目可以编译了! - zemaitis

3

1
就我在答案中所说的,你可以将 firebase-ui 升级到 3.2.2 或者完全删除它。 - Edric

2

我也遇到了原问题中发布的错误,即:

Error:Program type already present: android.arch.lifecycle.LiveData

不清楚是哪些库导致了问题。在@lienmt的提示下,我意识到可能与Firebase有关。

在我的情况下,我正在使用Firebase,并且还使用了firebase-ui库3.2.2:

implementation 'com.firebaseui:firebase-ui-database:3.2.2'

我已经将所有其他的Firebase库升级到15.0.0,但发现我的firebase-ui库不兼容,并在此确认:https://github.com/firebase/FirebaseUI-Android#compatibility-with-firebase--google-play-services-libraries。请确保将您的firebase-ui版本与其固定配对的Firebase版本完全匹配。将我的firebase-ui版本提升到3.3.1是解决错误的方法。
implementation 'com.firebaseui:firebase-ui-database:3.3.1'

参考一下,这是我现在正在使用的版本,我的应用程序没有出现任何错误:

implementation 'com.google.android.gms:play-services-wearable:15.0.0'
implementation 'com.google.android.gms:play-services-auth:15.0.0'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.google.firebase:firebase-database:15.0.0'
implementation 'com.google.firebase:firebase-core:15.0.0'
implementation 'com.google.firebase:firebase-auth:15.0.0'
implementation 'com.google.firebase:firebase-messaging:15.0.0'
implementation 'com.firebaseui:firebase-ui-database:3.3.1'

正如我在答案中所说,您可以将 firebase-ui 升级到 3.2.2 或完全删除它。 - Edric

1
请在您的应用程序 build.gradle 文件中添加以下依赖项。
implementation "android.arch.core:runtime:1.1.1"
implementation "android.arch.core:common:1.1.1"

4
描述这如何解决他的问题,以便更容易理解。 - rishad2m8
@rishad2m8,我已经解决了我的问题,尽管其他人可能会发现这里的其他解决方案有用。 - Edric

0

1
我已经看到你对gradle-wrapper.properties所做的更改,只是build.gradle。哦,顺便说一下,我报告的问题已经被分配给了一个Google员工。 - Edric
1
无论如何,现在你已经拥有了我所做的所有确切更改……都在一个地方。 - Hoornet

0

必须要升级。对我而言,我必须在App的build.gradle文件中升级以下内容:

implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-auth:16.2.0'
implementation 'com.firebaseui:firebase-ui-database:3.2.2'

然后您将同步您的文件。当您运行时,您会收到新的安装失败弹出框问题。只需取消它们并执行以下操作:

1) Build -> Clean Project
2) Build -> Build Bundle(s) / APK(s) -> Build APK(s)
3) Run your project! Should work!

如果你想了解我是如何解决这个问题的,可以跟随这个链接获取更多细节,但这应该已经解决了问题!


0
在我的情况下,将targetSdkVersioncompileSdkVersion升级到28,并使用所有支持库的28版本解决了这个问题。

当我升级 Glide 时遇到了这个问题。我收到有关 重复 CoordinatorLayout 的错误。我通过使用 com.android.support:design:27.1.0解决它,但随后我又遇到了有关 LiveData$LifecycleBoundObserver 的另一个错误。

几个小时后,我放弃了,并将targetSdkVersioncompileSdkVersion 升级到 API 28,同时使用

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'

因为Yolo。出乎意料的是,它起作用了。


0

正如Edric所提到的,这是因为某些库仍在使用旧版本的android.arch.lifecycle:extensions库,即android.arch.lifecycle:extensions:1.0.0

解决此问题的一种方法是强制应用程序使用该库的相同版本(如果可以,使用最新版本)。

有两种方法可以实现:

  1. 在Gradle的dependencies部分中明确定义我们想要使用的库版本。

    implementation 'android.arch.lifecycle:extensions:1.1.1'

或者

强制解析库,也在dependencies部分下。
android {
    configurations.all {
        resolutionStrategy.force 'android.arch.lifecycle:extensions:1.1.1'
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接