[Fix] Android Studio Dolphin 2021.3.1 Duplicate class androidx.lifecycle.ViewModelLazy found in modules

The problem “Android Studio Dolphin 2021.3.1: Error while building project” is seen when you are working on android studio and have recently updated from arctic fox to dolphin and the error is especially seen when you try to build a project after migrating to the latest version of Gradle version.

Given below is the snippet of the problem “Android Studio Dolphin 2021.3.1: Error while building project” :

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)

To fix the error “Android Studio Dolphin 2021.3.1: Error while building project”, you will have to add build.gradle dependency in your project file and error would be fixed.

Use below commands to fix error “Android Studio Dolphin 2021.3.1: Error while building project” into dependency in your grade androidx.preference.preference-ktx:1.2.0 

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

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

One of the dependencies has an explicit reliance on lifecycle 2.3.1, which is the cause of this problem.

You should only utilise the extensions library supplied by Android for viewmodels, activities, and fragments if you are using Kotlin.