Halil Ozel
Halil Ozel

Reputation: 3332

Duplicate class androidx.lifecycle.ViewModelLazy found in modules lifecycle-viewmodel-2.5.0-runtime

I'm using Android Studio Chipmunk | 2021.2.1 Patch 2.

I get the following error when I use the implementation 'androidx.appcompat:appcompat:1.5.0' version.

Duplicate class androidx.lifecycle.ViewModelLazy found in modules lifecycle-viewmodel-2.5.0-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.0) and lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1) Duplicate class androidx.lifecycle.ViewTreeViewModelKt found in modules lifecycle-viewmodel-2.5.0-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.0) and lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1)

It works fine after rolling back to the previous implementation 'androidx.appcompat:appcompat:1.4.2' version.

build.gradle:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.halil.ozel.darkmode"
        minSdk 28
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        dataBinding true
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

    // Preference
    implementation "androidx.preference:preference-ktx:1.2.0"
}

Can anyone help with similar error?

Thanks.

Upvotes: 26

Views: 17821

Answers (9)

Pankaj choudhary
Pankaj choudhary

Reputation: 1

This work perfectly for me.

File: android/app/build.gradle

dependencies {
{/* Add in last */}
     implementation 'androidx.appcompat:appcompat:1.6.0-beta01'
     implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
}

Upvotes: 0

Steven.Nguyen
Steven.Nguyen

Reputation: 1054

This works well for me

configurations {
    all {
      exclude group: 'androidx.lifecycle', module: 'lifecycle-runtime-ktx'
      exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel-ktx'
    }
  }

Upvotes: 8

Jasol Singh Sodha
Jasol Singh Sodha

Reputation: 51

I have also this implementation 'androidx.appcompat:appcompat:1.5.0' version

In my case, solved by doing this

First I updated it to version implementation 'androidx.appcompat:appcompat:1.6.0'

Also updated targetSdk and minSdk version to 33

Upvotes: 1

Pascal
Pascal

Reputation: 16661

This bug is fixed in AppCompat 1.6.0.
(and if you need androidx.lifecycle:lifecycle-viewmodel-ktx, use 2.5.1 or above)

implementation "androidx.appcompat:appcompat:1.6.0"

The issue description is here: https://issuetracker.google.com/issues/242384116.

Issue description excerpt:

This was caused by AppCompat 1.5.0 having an explicit dependency on Lifecycle 2.3.1 and a transitive dependency on Lifecycle 2.5.0 via Activity 1.5.0.

Since AppCompat 1.6.0-beta01, it now depends explicitly on Lifecycle 2.5.1 so this is no longer an issue. We can backport these bumps into an AppCompat 1.5.1 as well.

Note:
Using kotlin version '1.7.20'

Upvotes: 5

Mehdi Samavat
Mehdi Samavat

Reputation: 11

This works for me ->

kotlin_version = '1.7.22
appcompat_version = "1.6.0-beta01"
lifecycle_version = "2.5.1"

Upvotes: 0

Imran
Imran

Reputation: 271

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: 27

Halil Ozel
Halil Ozel

Reputation: 3332

Finally, This problem was solved.

build.gradle(project):

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(app):

...
android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.halil.ozel.catchthefruits"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        dataBinding true
    }
}

...

implementation 'androidx.appcompat:appcompat:1.5.1'

...

For more details:

https://developer.android.com/jetpack/androidx/releases/appcompat

Upvotes: 2

Zainal Fahrudin
Zainal Fahrudin

Reputation: 608

In my case, solved by add this :

api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1" 

source : https://issuetracker.google.com/issues/238425626

Upvotes: 10

jiasheng
jiasheng

Reputation: 181

I had this problem too. Apparently it's a bug specifically for version 1.5.0 having an explicit dependency on Lifecycle 2.3.1 and a transitive dependency on Lifecycle 2.5.0 via Activity 1.5.0. It will be fixed with 1.5.1

Here is the issue tracker reference: https://issuetracker.google.com/issues/242384116

Just roll back to 1.4.2 until it will be fixed.

Upvotes: 14

Related Questions