在模块中找到的问题重复类 androidx.lificycle.viewmodel

我在运行模拟器时出现了这些错误

重复的类 androidx.life cycle. ViewModelLazy 发现在模块 jetfied-life cycle-viewmodel-ktx-2.3.1-running (androidx.life cycle: life cycle-viewmodel-ktx: 2.3.1)和 life cycle-viewmodel-2.4.0-running (androidx.life cycle: life cycle-viewmodel: 2.4.0)中

重复的类 androidx.life cycle. ViewModelProviderKt 发现在模块 jetfied-life cycle-viewmodel-ktx-2.3.1-run (androidx.life cycle: life cycle-viewmodel-ktx: 2.3.1)和 life cycle-viewmodel-2.4.0-run (androidx.life cycle: life cycle-viewmodel: 2.4.0)中

重复的类 androidx.life cycle. ViewTreeViewModelKt 发现在模块 jetfied-life Cycle-viewmodel-ktx-2.3.1-running (androidx.life cycle: life cycle-viewmodel-ktx: 2.3.1)和 life cycle-viewmodel-2.4.0-running (androidx.life cycle: life cycle-viewmodel: 2.4.0)中

注意: 这不是特定于 android 模拟器,而是渐变构建的问题。

52364 次浏览

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"

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'

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.

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"

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

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

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 !

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

PROBLEM: I got this error when I updated the appcompat dependency to 1.5.1.

SOLUTION: I updated the appcompat dependency to 1.6.0-rc01.

// Pick one:
// Kotlin
implementation("androidx.appcompat:appcompat:1.6.0-rc01")
// Groovy
implementation "androidx.appcompat:appcompat:1.6.0-rc01"

REASON: This works because of a bug fix implemented in 1.6.0-beta01 - therefore later versions should also work.

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)

for anyone using compose, you just need to add

implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"