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.
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"
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'
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.
Changing the viewmodel and viewmodel-ktx version to their latest version solved the problem for me :
implementation "androidx.lifecycle:lifecycle-viewmodel:2.5.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With