Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem duplicate class androidx.lifecycle.viewmodel found in modules

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.

like image 767
Martin GGR Avatar asked Nov 02 '21 23:11

Martin GGR


4 Answers

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"
like image 129
l33t Avatar answered Oct 17 '22 01:10

l33t


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'
like image 27
Kirill Kitten Avatar answered Oct 17 '22 02:10

Kirill Kitten


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.

like image 19
Alex Rmcf Avatar answered Oct 17 '22 02:10

Alex Rmcf


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"
like image 14
PRANAV SINGH Avatar answered Oct 17 '22 01:10

PRANAV SINGH