无法访问“androidx.lifecycle.HasDefaultViewModelProviderFactory”,它是“FavoriteBottomDialogFragment”的超类型。请检查您的模块类。

38
我在整个扩展了 BottomSheetDialogFragment 的类中遇到了错误。
Cannot access 'androidx.lifecycle.HasDefaultViewModelProviderFactory' which is a supertype of 'FavoriteBottomDialogFragment'. Check your module classpath for missing or conflicting dependencies

在这里输入图片描述

在这里输入图片描述

该类位于 app 模块中,该模块实现了另外两个模块: core 和 presentation-core。

build.gradle

dependencies {

     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation project (':core')
     implementation project (':presentation-core')

     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41"
     implementation 'androidx.appcompat:appcompat:1.1.0'
     implementation 'androidx.core:core-ktx:1.2.0'
     testImplementation 'junit:junit:4.12'
     implementation "com.google.android.material:material:1.1.0"

     //Rx
     implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
     implementation "io.reactivex.rxjava2:rxjava:2.2.9"
     //Architecture component
     implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
     implementation 'androidx.room:room-runtime:2.0.0'
     kapt 'androidx.room:room-compiler:2.0.0'
     kapt 'androidx.lifecycle:lifecycle-common-java8:2.0.0'
     implementation 'androidx.room:room-rxjava2:2.0.0'
     implementation 'androidx.room:room-guava:2.0.0'

     implementation 'androidx.recyclerview:recyclerview:1.1.0'
     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
 }

核心依赖项

dependencies {
     api fileTree(dir: 'libs', include: ['*.jar'])
     api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.61"
     api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
     api 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
     //library to serialize Java Objects between Contexts
     implementation 'org.parceler:parceler-api:1.1.11'
     kapt 'org.parceler:parceler:1.1.11'

     //testing dependencies
     testImplementation 'junit:junit:4.12'
     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     testImplementation "org.mockito:mockito-core:2.24.5"
     androidTestImplementation "org.mockito:mockito-android:2.24.5"

     //architecture component
     implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
     implementation "androidx.lifecycle:lifecycle-livedata:2.0.0"

     //RxJava2
     implementation "io.reactivex.rxjava2:rxjava:2.2.9"
     implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
}

演示核心

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
}

BottomSheetDialogFragment 图片描述

尽管我遇到了一些错误,但我可以在设备上运行该项目。


2
我也遇到了这个问题。可能是某个检查或 Lint 规则中存在一个 bug。我已经提交了一个错误报告,但被 Google 拒绝了。对于一些书籍示例,我最终升级到了最新的预发布版 1.2.0 的 appcompat 来解决这个问题。 - CommonsWare
2
是的,看起来是这样的。 将appcompatcom.google.android.material:material升级到1.3.0-alpha01可以解决这个问题。 - Oussèma Hassine
我也通过将appcompat升级到最新版本1.3.0-rc01来消除了这个错误消息。但是,我不必升级material库。 - Raj Narayanan
7个回答

49

今天我也遇到了完全相同的问题,并成功地解决了它。原来问题是在一个模块中,使用了期望版本的androidx.lifecycle:lifecycle-viewmodel,但某些其他依赖代码中却使用了一个更高版本

所以在我的情况下,我的模块使用了版本2.1.0,但其中一个依赖项使用了版本2.2.0。代码可以编译通过,因为Gradle将依赖项解析为最新版本;然而,Android Studio在这种情况下有些混乱(并不总是,因为这种情况并不总是发生,但有时候会出现——我不是第一次看到这种情况)。

因此解决方案是:找出您的应用程序中此库的最新版本,并更新此模块的build.gradle文件,使其指向Gradle正在解析的相同版本。或者:

  1. 运行gradlew app:dependencies
  2. 在结果中搜索lifecycle-viewmodel
  3. 在应用程序的build.gradle中更新依赖于lifecycle-viewmodel的版本,使其与Gradle正在解析的版本匹配
  4. 与Gradle文件同步项目

值得注意的是,我可以将它切换回2.1.0,问题没有再出现。 - David Pisoni
在Windows中运行"./gradlew app:dependencies"! - tohidmahmoudvand

21

就我的情况(一个Android模块),在Android Studio 4.0.1中,我在IDE中收到了与androidx.lifecycle.HasDefaultViewModelProviderFactory有关的警告, 我通过将此行添加到build.gradle文件来解决了这个问题:


dependencies { implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" }
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

因此,build.gradle 的开头变成:

implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
...

16
在我的情况下,问题是通过在有问题的类所在的模块中添加lifecycle viewmodel依赖项来解决的。

9

将依赖项添加到您的构建gradle中,实际版本为androidx.fragment:fragment-ktx:x.x.x。对于活动,您必须添加androidx.activity:activity-ktx:x.x.x。希望这能帮助您,对我来说解决了问题。


3

我在build.gradle中添加了以下代码来解决模块中相同的问题:

def archLifecycleVersion = '2.2.0'
implementation "androidx.lifecycle:lifecycle-extensions:$archLifecycleVersion"
kapt "androidx.lifecycle:lifecycle-compiler:$archLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$archLifecycleVersion"

0

你的 androidx.lifecycle:lifecycle-XXX 依赖项版本必须相同

在我的情况下:

implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"

0

我也遇到了同样的问题 ->

implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0'

我已将lifecycle-runtime-ktx的版本从以下内容更改:

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0'

更新为

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'

现在可以正常运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接