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

前端 未结 10 960
迷失自我
迷失自我 2020-12-06 04:32

When I press the run button in Android Studio, my app compiles but shows this error (redacted):

Error:Program type a         


        
10条回答
  •  一生所求
    2020-12-06 05:05

    This post is the top search result for the very similar error: "Program type already present: android.arch.lifecycle.ViewModelProvider$Factory"

    My project uses Room and LiveData, but not firebase. The following changes removed the error:

    FROM:

    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'
    

    TO:

    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'
    

    --- UPDATED ANSWER ---

    My previous answer was aimed at solving this error. However, I thought it would be worth presenting it again using best practises:

    App level build.gradle file:

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

    Project level build.gradle file:

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

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

提交回复
热议问题