Home > front end >  Android Studio Dolphin 2021.3.1: Error while building project
Android Studio Dolphin 2021.3.1: Error while building project

Time:09-19

I just recently upgraded from Arctic Fox to Dolphin and migrated my project from Gradle 7.0.3 to 7.3.0.

When I want to build project I got error:

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)

My entire project is non-usable and broken right now.

CodePudding user response:

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 = "latest_version"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

CodePudding user response:

This happened with me when I added the androidx.preference.preference-ktx:1.2.0 dependency in gradle.

Just add this dependency in build.gradle file to fix this.

Java -> implementation 'androidx.lifecycle:lifecycle-viewmodel:2.5.1'

Kotlin -> implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'

This error occurs due to one of the dependencies having an explicit dependency of lifecycle 2.3.1.

Google Tracker Issue Link -> https://issuetracker.google.com/issues/242384116

Hope this helps :)

  • Related