找不到 DefaultActivityViewModelFactory

DefaultActivityViewModelFactory not found

将 Hilt 版本从 2.33-beta 迁移到 2.35 后,我的项目已停止构建并出现以下错误:

txt版本:

error: cannot access DefaultActivityViewModelFactory    
  class file for dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory not found   
  Consult the following stack trace for details.    
  com.sun.tools.javac.code.Symbol$CompletionFailure: class file for dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory not found

我的 build.gradle(项目)的片段:

buildscript {
    ext.hilt_version = '2.33-beta'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
        ...
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的 build.gradle(应用程序)的片段:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

android {
    compileSdkVersion 29
    ...

    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments += [
                        "room.schemaLocation": "$projectDir/schemas".toString(),
                        "room.incremental"   : "true"
                ]
            }
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
    }

    buildFeatures {
        viewBinding true
        dataBinding true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    ...

    //DI
    implementation "com.google.dagger:hilt-android:$hilt_version"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    implementation 'androidx.hilt:hilt-work:1.0.0-beta01'
    kapt "com.google.dagger:hilt-compiler:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
    kapt 'androidx.hilt:hilt-compiler:1.0.0-beta01'

    // INSTRUMENTED TESTS
    ...
    androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
    androidTestImplementation "androidx.work:work-testing:2.5.0"

    //KOTLIN
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.32"

    //LIFECYCLE
    implementation 'androidx.lifecycle:lifecycle-common-java8:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'

    // WORK MANAGER
    implementation "androidx.work:work-runtime-ktx:2.5.0"
}

是否有人遇到该错误并知道可能的解决方案?

删除对 hilt-lifecycle-viewmodel 的依赖会导致错误消失,因为在较新版本的 hilt 中不再需要它。 只需从您的应用级别 build.gradle 文件中删除此行(如果有)。

implementation 'androidx.hilt:hilt-lifecycle-viewmodel:x.x.x'