Reputation: 1951
I got these errors when I try to run the emulator
Duplicate class androidx.lifecycle.ViewModelLazy found in modules jetified-lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1) and lifecycle-viewmodel-2.4.0-runtime (androidx.lifecycle:lifecycle-viewmodel:2.4.0)
Duplicate class androidx.lifecycle.ViewModelProviderKt found in modules jetified-lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1) and lifecycle-viewmodel-2.4.0-runtime (androidx.lifecycle:lifecycle-viewmodel:2.4.0)
Duplicate class androidx.lifecycle.ViewTreeViewModelKt found in modules jetified-lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1) and lifecycle-viewmodel-2.4.0-runtime (androidx.lifecycle:lifecycle-viewmodel:2.4.0)
Note: This is not specific to the android emulator but problem with gradle build.
Upvotes: 195
Views: 124210
Reputation: 11
I had the same errors and it turned out that I just forgot to close a curly brace } somewhere in my build.gradle file.
Upvotes: 0
Reputation: 1721
PROBLEM: I got this error when I updated the appcompat
dependency to 1.5.1
.
SOLUTION: I updated the appcompat
dependency to 1.6.1
.
*Later versions should also work.
// Pick one:
// Kotlin
implementation("androidx.appcompat:appcompat:1.6.1")
// Groovy
implementation "androidx.appcompat:appcompat:1.6.1"
REASON: This works because of a bug fix implemented in 1.6.0-beta01
.
The 1.6.0-beta01 release notes state the following in the "Bug Fixes" section:
AppCompat
now explicitly depends onLifecycle
2.5.1
andSavedState
1.2.0
. (I7e3e2)
Upvotes: 51
Reputation: 330
Experienced same version of the error;
> Duplicate class androidx.lifecycle.ViewModelLazy found in modules lifecycle-viewmodel-2.5.1-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.1) and lifecycle-viewmodel-ktx-2.2.0-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0)
My approach was to explicitly specify the version of LiveData to use, I do not use liveData anywhere in my project, but gradle needs to know what to use under the hood.
Add the below dependency to your problematic module - be sure to update the version to match what you use in your lifecycle dependencies.
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.4.0")
Upvotes: 0
Reputation: 159
VERSIONS OF "def lifecycle_version" IN ALL LINES MUST BE SAME
IN THIS CASE IS '2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
def lifecycle_version = '2.5.1'
implementation "androidx.lifecycle:lifecycle-process:2.5.1"
implementation "androidx.lifecycle:lifecycle-runtime:2.5.1"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.5.1"
Upvotes: 0
Reputation: 5791
I was having this issue and various of these solutions didn't work for me. Let me just post the set of dependencies that fixed the issue in my case:
ext {
compose_version = "1.3.1"
kotlin_version = "1.7.10"
coroutines_version = "1.5.0"
hilt_version= '2.44.2'
}
...
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation "androidx.compose.animation:animation:$compose_ui_version"
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
implementation 'androidx.compose.material:material:1.1.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.activity:activity-compose:1.6.1'
implementation 'androidx.navigation:navigation-compose:2.5.3'
Upvotes: 0
Reputation: 119
As directed by l33t, I added
def lifecycle_version = "2.5.1"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
but put the lines in the wrong file and got
Could not find method implementation() for arguments...
Solution: Make sure you're adding these dependencies in android/app/build.gradle, not android/build.gradle. See here.
Upvotes: 8
Reputation: 247
for anyone using compose, you just need to add
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"
Upvotes: 13
Reputation: 83
While many of these answers will work, there is a better way to set transitive dependency versions rather than including the package as a direct dependency.
constraints {
implementation('androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1') {
because 'insert explanation here'
}
}
Here's a breakdown https://docs.gradle.org/current/userguide/dependency_constraints.html
Upvotes: 8
Reputation: 3530
I get the same error, any answers solved my issue. The key point is here to solve the error (preferences 1.2.0 in my project -> add exclude group).
def preference_version = "1.2.0"
implementation ("androidx.preference:preference:$preference_version"){
exclude group: 'androidx.lifecycle', module:'lifecycle-viewmodel'
exclude group: 'androidx.lifecycle', module:'lifecycle-viewmodel-ktx'
}
Then use 2.5.1 version for lifecycle dependencies.
def lifecycle_version = "2.5.1"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
// https://developer.android.com/jetpack/androidx/releases/lifecycle
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
This is working fine, i hope that it will help some people !
Upvotes: 7
Reputation: 136
It helped me reverting appcompat from 1.5.0 to 1.4.2 as suggested in this thread: Duplicate class androidx.lifecycle.ViewModelLazy found in modules lifecycle-viewmodel-2.5.0-runtime
Upvotes: 9
Reputation: 60913
After creating a new empty Compose activity, I got this error.
Update all dependencies to the latest version solve error with Duplicate class androidx.lifecycle.ViewModelLazy ...
Upvotes: -2
Reputation: 924
I thought that eliminating duplicate classes is better than adding new ones, so I'll post my solution to this problem here:
configurations {
all {
exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel-ktx'
}
}
These lines of code need to be added to the project level build.gradle file - and the build error will go away along with the wasted memory (a bit, but still).
Strange and incomprehensible behavior. I got it after adding the Kotlin library to the project, which it was later decided to replace with a version for Java. If you go to the .gradle folder, you can find it there, but I'm not sure if removing it from there is a good idea, because it may be used in other libs. It is strange that gradle or AndroidStudio does not automatically solve this problem, because only dependencies for Java are specified in the build.gradle file.
Upvotes: 28
Reputation: 1897
I had the same problem and I solved it by adding only one line of code
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
Upvotes: 154
Reputation: 19937
Most likely, one of your dependencies uses the kotlin
version of the viewmodel
library whereas your code uses the java
version.
Specify both to enforce the latest version for all dependencies:
def lifecycle_version = "2.4.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
Upvotes: 228