You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

继承BottomSheetDialogFragment类出现依赖访问错误,求排查方案

Fixing "Cannot access 'androidx.lifecycle.HasDefaultViewModelProviderFactory'" Error

Let's break down why this error pops up and how to resolve it for good:

Root Cause

The error happens due to two key issues in your dependency setup:

  1. Outdated Lifecycle Component Version: The HasDefaultViewModelProviderFactory class was introduced in Lifecycle version 2.2.0, but you're using the older 2.0.0 version across all modules—this is why the IDE can't find the class.
  2. Inconsistent AndroidX Dependency Versions: Your modules use mismatched versions of core AndroidX libraries (e.g., appcompat is 1.1.0 in the app module but 1.0.2 in presentation-core), creating classpath conflicts that confuse the compiler even though the app runs.

Step-by-Step Fix

1. Define Unified Dependency Versions (Recommended)

First, add version variables to your project's root build.gradle file. This makes it easy to keep versions consistent across all modules and simplifies future updates:

ext {
    // AndroidX Versions
    androidx_appcompat_version = "1.1.0"
    androidx_core_ktx_version = "1.2.0"
    lifecycle_version = "2.2.0" // Minimum version needed for the missing class
    
    // Other Shared Versions
    kotlin_version = "1.3.61"
    material_version = "1.1.0"
    room_version = "2.0.0"
}

2. Update Dependencies in Each Module

App Module (app/build.gradle)

Update Lifecycle dependencies to the required version, and use the unified variables for consistency:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project (':core')
    implementation project (':presentation-core')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "androidx.appcompat:appcompat:$androidx_appcompat_version"
    implementation "androidx.core:core-ktx:$androidx_core_ktx_version"
    testImplementation 'junit:junit:4.12'
    implementation "com.google.android.material:material:$material_version"
    
    // RxJava
    implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
    implementation "io.reactivex.rxjava2:rxjava:2.2.9"
    
    // Architecture Components
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    kapt "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    implementation "androidx.room:room-rxjava2:$room_version"
    implementation "androidx.room:room-guava:$room_version"
    
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
}
Core Module (core/build.gradle)

Change Lifecycle dependencies from implementation to api so they're visible to dependent modules (like the app module), and use the unified version variables:

dependencies {
    api fileTree(dir: 'libs', include: ['*.jar'])
    api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
    api 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
    
    // Parceler
    implementation 'org.parceler:parceler-api:1.1.11'
    kapt 'org.parceler:parceler:1.1.11'
    
    // Testing
    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 Components (use api to expose to app module)
    api "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    api "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
    
    // RxJava
    implementation "io.reactivex.rxjava2:rxjava:2.2.9"
    implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
}
Presentation-Core Module (presentation-core/build.gradle)

Align AndroidX dependencies with the app module using the unified variables:

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

3. Clean and Rebuild the Project

After updating dependencies:

  1. Run ./gradlew clean in your terminal to clear cached build files.
  2. Sync your project with Gradle files (click the "Sync Now" button in Android Studio).
  3. Rebuild your project (Build > Rebuild Project).

Why This Works

  • Upgrading Lifecycle to 2.2.0 adds the missing HasDefaultViewModelProviderFactory class to your classpath.
  • Using unified versions eliminates conflicts between different AndroidX library versions across modules.
  • Changing Lifecycle dependencies to api in the core module ensures the app module has access to all required Lifecycle classes during compilation.

内容的提问来源于stack exchange,提问作者Oussèma Hassine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 21:27:59