170

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Martin GGR
  • 1,701
  • 2
  • 3
  • 3

14 Answers14

203

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"
l33t
  • 18,692
  • 16
  • 103
  • 180
  • 5
    Thanks, I was not using viewmodel-ktx but looks like viewmodel-savedstate required it. So after adding viewmodel-ktx:$lifecycle_version solved the issue. – BlankSpace Nov 04 '21 at 10:43
  • 4
    Good one, this shall be marked as right answer. I have also a java module using lifecycle-viewmodel 2.4.0 and lifecycle-livedata 2.4.0, and the jetifier resistantly generate 2.3.1 and leads to conflict. Even I am not using ktx in java module after adding the lifecycle-viewmodel-ktx:2.4.0 dependency, my error went a way. – Yingding Wang Nov 07 '21 at 12:34
  • 2
    Where do I put this in my project? Build.gradle? – Jackie Aug 15 '22 at 15:47
  • 3
    @Jackie Yes, the app's `build.gradle` in the `dependencies` section. – l33t Aug 16 '22 at 10:56
  • 4
    Brilliant. Note for people on the latest kotlin (1.7.21, November 2022) that your `lifecycle_version` will be `2.5.1`. Other than that this solution still works. – TDuff Nov 22 '22 at 12:09
  • Nice fix, I believe the source of error for me was when I was copying code into my project and I used the alt+enter to import the dependency and I guess I clicked the wrong option. So note to everyone be careful to import the correct dependency. – Seth Harlaar Dec 01 '22 at 21:26
141

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'
Kirill Kitten
  • 1,759
  • 1
  • 4
  • 5
  • 1
    Thank you. This worked today. I have no idea which library uses classes. I added the following to solve the problem: implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0' – Hong Jul 01 '22 at 22:21
  • 5
    I don't understand why people upvote this copycat answer. To begin with, this solution is incorrect and will break when the third-party library gets updated. You should specify **both** the kotlin and java versions. That way, you are in control of which `lifecycle-viewmodel` library version to use. – l33t Aug 15 '22 at 12:11
  • 1
    This works for me so I don't care whether it's a copycat answer or not, I've upvoted it. Saved me hours after trying so many dependency resolution strategies – Rafsanjani Aug 20 '22 at 11:40
  • It worked for me. But still, I can't decide whether this answer is the proper solution. – Rumit Patel Nov 14 '22 at 07:21
  • adding implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' works for me – Giancarlo Molina Dec 15 '22 at 23:21
40

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 on Lifecycle 2.5.1 and SavedState 1.2.0. (I7e3e2)

Just The Highlights
  • 1,555
  • 19
  • 31
  • 1
    This is the best solution IMHO, because it elegantly fixes the underlying issue. The only downside is that it'using a release candidate instead of a stable version, but as soon as a stable version >=1.6.0 is released this won't be a problem anymore. – Mauro Vanetti Dec 23 '22 at 15:05
  • 1
    This solution worked for me. Very simple. I also used the 1.5.1 version and faced the error. – MD. Shafiul Alam Biplob Jan 02 '23 at 20:50
  • Thank you, this fixed the issue. Nowadays there is a newer alpha version androidx.appcompat:appcompat:1.7.0-alpha01 This also fixes the issue. – Darksymphony Jan 08 '23 at 12:28
  • 1
    Thanks, this works better for me, I just need to update to the latest stable version now which is 1.6.1 and the error went away – Bruce Feb 15 '23 at 04:48
  • What a fix! Recently run into this after updating material dependency without update androidx.appcompat dependency. Thanks! – Siele Kim Jun 21 '23 at 06:25
27

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.

Alex Rmcf
  • 804
  • 7
  • 14
  • What "memory" are you referring to? The PC or the Android device? From what I could see, there is no added bloat in the resulting APK (could be a result of `R8`). – l33t Dec 29 '21 at 13:44
  • I am talking about resulting apk – Alex Rmcf Dec 29 '21 at 18:42
  • @l33t exactly - 12 870 436 bytes vs 13 102 070 bytes. As I already said - a bit, but still – Alex Rmcf Dec 29 '21 at 18:47
  • I ran some tests. With all optimizations (e.g. Release + minify using R8), removing `lifecycle-viewmodel-ktx` reduces the size with **less than 1 kB**. Most likely `R8` is able to get rid of 99% of the bloat, leaving one or two extra classes (`classes.dex` differs). Your solution seems to work, but why doesn't the minifier get rid of those last classes? Could be a minor glitch in `R8` or perhaps there is a real third-party dependency that, at some point, needs these kotlin classes. Your numbers indicate some other problem. – l33t Jan 06 '22 at 12:27
  • @l33t I didn’t quite explain it correctly. To get the numbers that were mentioned, I excluded several libraries that I used explicitly, but were also present as dependencies in other libraries (but for example with lower versions). exclude group: 'org.json', module: 'json' exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel-ktx' exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel:2.3.1' exclude group: 'org.apache.commons', module: 'commons-lang3' and others – Alex Rmcf Jan 06 '22 at 16:10
  • Not helpful!!! ... – C.F.G Dec 16 '22 at 14:10
12

for anyone using compose, you just need to add

implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"
Kou Peto
  • 237
  • 2
  • 7
9

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

KirilP
  • 111
  • 1
  • 4
  • I am not explicitly using any of those libraries and this is the only solution that helped me. The `configurations.all.exclude` did not work for me. Thanks! – KaHa6uc Sep 19 '22 at 07:21
  • 2
    Or upgrade appcompat to 1.6.0-rc1 (or higher), in 1.5.1 it was not fixed for me, even though the release notes state so. – hb0 Sep 21 '22 at 07:58
7

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 !

iCantC
  • 2,852
  • 1
  • 19
  • 34
Zhar
  • 3,330
  • 2
  • 24
  • 25
  • To me, this is the right answer, but it still concerns me. Not as much as how kotlin dependencies got pulled into a Java library. :P – Dustin Oct 25 '22 at 00:41
7

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

Carson
  • 73
  • 1
  • 4
5

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.

DotCFile
  • 59
  • 1
  • 4
0

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'
voghDev
  • 5,641
  • 2
  • 37
  • 41
0

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

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") 
KE Keronei
  • 323
  • 2
  • 8
0

I had the same errors and it turned out that I just forgot to close a curly brace } somewhere in my build.gradle file.

-2

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 ...

Linh
  • 57,942
  • 23
  • 262
  • 279