本文介绍了未找到 DefaultActivityViewModelFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After migrating the Hilt version from 2.33-beta to 2.35 my project has stopped building with the error given below:

一个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(项目)的片段:

A snippet of my build.gradle (project):

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(应用程序)的片段:

A snippet of my build.gradle (app):

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"
}

有没有人遇到过这个错误并且知道可能的解决方案是什么?

Does anyone meet that error and know what may be the solution?

推荐答案

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

Removing the dependency on hilt-lifecycle-viewmodel causes the error to go away as it is no longer required in newer versions of hilt.Simply delete this line from your app level build.gradle file if you have it.

实现'androidx.hilt:hilt-lifecycle-viewmodel:x.x.x'

这篇关于未找到 DefaultActivityViewModelFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 03:33