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

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

当我在 Android Studio 中按下 运行 按钮时,我的应用程序编译但显示此错误(编辑):

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

(Full log)

我试过删除 .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'

今天提升支持库版本时遇到了同样的问题

尝试将 所有 '27.1.0' 替换为 '27.0.2'


后来我也通过升级其他库设法消除了错误。 这是我目前的工作状态: 根 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'
    }
}

///////////////

我还将 graddle-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

@Edric:由于我无法在线程中重播图像,所以我在这里回答你的问题。

对我有用的更改:

PS: 我也在 gradle-wrapper.properties 中升级了 distributionUrl 到 http://services.gradle.org/distributions/gradle-4.6-all.zip

显然,这是有意为之的行为:

com.firebaseui:firebase-ui-firestore:3.1.0 depends on android.arch.lifecycle:extensions:1.0.0-beta1. Switching to version 3.2.2 fixes the issue by using the Lifecycle 1.1 libraries that Support Library 27.1.0 are built upon. - Issue Tracker

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

Firebase-UI 3.1.0 与 Firebase 不兼容/Google 服务 11.8.0

您需要根据https://github.com/firebase/FirebaseUI-Android#compatibility-with-firebase--google-play-services-libraries

升级或降级
  • Firebase-UI 3.1.0 with Firebase / Google 服务 11.4.2
  • Firebase / Google Services 11.8.0 with Firebase-UI 3.1.3

希望对您有所帮助 ;)

我也有在原问题中发布的错误,即:

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

不清楚是哪个库导致了问题。根据上面@lienmt 的提示,我意识到它可能与 Firebase 有关。

就我而言,我使用的是 Firebase 并且还使用了 firebase-ui library 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'

此 post 是非常相似错误的热门搜索结果:"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

请在您的应用 build.gradel 文件中添加以下依赖项

implementation "android.arch.core:runtime:1.1.1"
implementation "android.arch.core:common:1.1.1"

就像 Edric 提到的那样,发生这种情况是因为某些库仍在使用旧版本的 android.arch.lifecycle:extensions 库,即 android.arch.lifecycle:extensions:1.0.0.

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

有两种方法可以做到这一点:

  1. 在依赖项部分下明确定义了我们要在 Gradle 中使用的库版本。

    implementation 'android.arch.lifecycle:extensions:1.1.1

  1. 库的强制解析,也在dependencies部分下。

    android {
        configurations.all {
            resolutionStrategy.force 'android.arch.lifecycle:extensions:1.1.1'
        }
    }
    

在我的例子中,将 targetSdkVersioncompileSdkVersion 提高到 28,以及使用所有支持库的版本 28 解决了这个问题。


我在升级Glide的时候遇到了这个问题。我收到关于使用 com.android.support:design:27.1.0duplicate CoordinatorLayout. I 的错误,但随后我收到关于 LiveData$LifecycleBoundObserver.

的另一个错误

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

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。运气不好,成功了。

只是要升级。对我来说,我必须在 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!

如果您想了解我如何解决它的任何其他详细信息,请关注此 link,但这应该可以解决问题!