代码之家  ›  专栏  ›  技术社区  ›  Edric Prince Bansal

错误:程序类型已存在:android。拱生命周期。LiveData

  •  26
  • Edric Prince Bansal  · 技术社区  · 8 年前

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

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

    (Full log)

    我已尝试删除 .gradle 文件夹,然后转到 Build > Clean Project Build > 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'
    
    10 回复  |  直到 6 年前
        1
  •  27
  •   Edric Prince Bansal    8 年前

    显然,这是故意的行为:

    通用域名格式。firebaseui:firebase ui firestore:3.1.0依赖于android。拱生命周期:扩展:1.0.0-beta1。切换到版本3.2.2可以通过使用支持库27.1.0的生命周期1.1库修复此问题- Issue Tracker

    对于我来说,删除 firebase-ui 依赖关系解决了这个问题,因为我根本没有使用这个库。

        2
  •  26
  •   Hoornet    8 年前

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

    尝试替换 全部的 “27.1.0”和“27.0.2”


    后来,我也升级了其他库,成功地消除了这个错误。 这是我当前的工作状态: 根刻度:

    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" }
        }
    }
    

    ////////////////// app 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包装器。属性到:

    #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
    
        3
  •  21
  •   Elletlar FireTr3e    7 年前

    这篇帖子是针对类似错误的最热门搜索结果:“程序类型已经存在:android.arch.lifecycle。”。 ViewModelProvider$工厂 "

    我的项目使用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'
    

    ---更新的答案---

    我之前的回答旨在解决这个错误。然而,我认为值得使用最佳实践再次介绍:

    应用程序级生成。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"
    

    项目级构建。gradle文件:

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

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

        4
  •  3
  •   lienmt    8 年前

    Firebase UI 3.1.0与Firebase/Google Services 11.8.0不兼容

    您需要根据需要进行升级或降级 https://github.com/firebase/FirebaseUI-Android#compatibility-with-firebase--google-play-services-libraries

    • Firebase UI 3.1.0和Firebase/Google Services 11.4.2
    • Firebase/Google Services 11.8.0,Firebase UI 3.1.3

    希望这有帮助;)

        5
  •  2
  •   Lucy    8 年前

    我还把错误贴在了原来的问题上,即:

    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'
    
        6
  •  1
  •   Abhishek Chaniyara    7 年前

    请在应用程序生成中添加以下依赖项。gradel文件

    implementation "android.arch.core:runtime:1.1.1"
    implementation "android.arch.core:common:1.1.1"
    
        7
  •  0
  •   Hoornet    8 年前

    @艾德里克:因为我无法在线程中回放图像,所以我在这里回答你的问题。

    对我有用的变化:

    Module Project level

    PS:我还升级了gradle包装中的distributionUrl。属性到 http://services.gradle.org/distributions/gradle-4.6-all.zip

        8
  •  0
  •   Kharda    7 年前

    就像Edric提到的那样,发生这种情况是因为一些库仍然使用旧版本的android。拱生命周期:扩展库,即 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'
          }
      }
      
        9
  •  0
  •   Aloha    7 年前

    就我而言,颠簸 targetSdkVersion compileSdkVersion 到28,以及使用所有支持库的版本28修复了该问题。


    我在升级Glide时遇到了这个问题。关于 duplicate CoordinatorLayout . 我 solved 通过使用 com.android.support:design:27.1.0 ,但我在 LiveData$LifecycleBoundObserver .

    几个小时后,我放弃了,升级了 targetSdkVersion 编译SDK版本 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'
    

    因为约洛。运气不好,成功了。

        10
  •  0
  •   Fletchy1995    7 年前

    只是需要升级。对我来说,我必须升级以下应用程序内部版本。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 如果你想知道我是如何解决它的任何其他细节,但这应该可以做到!