65

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

enter image description here

A txt version:

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

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
}

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

How can I solve this issue?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Patryk Kubiak
  • 1,679
  • 2
  • 11
  • 17
  • 1
    With hilt you can initialize a ViewModel without a factory. Can you show your ViewModel's code! – ameencarpenter Apr 25 '21 at 18:05
  • 17
    I got the same issue. Removing this import seems to have fixed it for me `implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'` – Rafsanjani Apr 25 '21 at 20:39
  • 3
    @Rafsanjani thanks a lot. You are right. The `androidx.hilt:hilt-lifecycle-viewmodel` dependency was the problem. It was also redundant in the beta version. Feel free to write the answer ;) – Patryk Kubiak Apr 26 '21 at 20:03
  • 1
    @Rafsanjani Thanks a lot. I was getting a little different error, it was something like "Unable to process my.packages.SingletonC" but it also mentioned DefaultActivityViewModelFactory, and I don't even have any ViewModels or UI that use them yet (re-writing an app and just have the data layer, repo, and part of DI setup). Commenting out that lifecycle-viewmodel dependency fixed it. – clamum Aug 10 '22 at 05:42

4 Answers4

235

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.

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

Rafsanjani
  • 4,352
  • 1
  • 14
  • 21
  • 1
    I removed and error gone, but when i added viewmodel dependency with ViewModelScoped it throws errror like error: [Dagger/MissingBinding] com.example.smartbilling.ui.login.LoginViewModel cannot be provided without an Inject constructor or an Provides-annotated method. public abstract static class SingletonC implements BaseApplication_GeneratedInjector, ^ – Akash kumar Apr 29 '21 at 12:29
  • 2
    @Akashkumar The docs over here might be helpful to you. It essentially shows you the recommended way of using the `ViewModelScope` https://dagger.dev/hilt/view-model.html – Rafsanjani Apr 29 '21 at 12:45
  • @Rafsanjani, thanks! After removing this library I had to fix the error: `incompatible types: NonExistentClass cannot be converted to Annotation`. Your link to `ViewModel` helped me. – CoolMind May 24 '21 at 15:18
  • Didn't find this in the release notes google/dagger, or did we missing something? Btw, thanks a lot you saved our days! – mochadwi Jul 12 '21 at 07:18
  • Yes this solved my issue please flow android developer site for latest update i was using hilt version 2.44 at this time. – Kashinath Feb 01 '23 at 04:22
15

Remove below deprecated dependency:

implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"

(It was deprecated since dagger-2.34 version)

proof: https://github.com/google/dagger/releases/tag/dagger-2.34

Also Try to upgrade your lifecycle version as below:

def lifecycle_version = "2.5.1"

add below lines after dependency section in build.gradle(app):

kapt {
  correctErrorTypes true
}

follow official documentation:

https://developer.android.com/training/dependency-injection/hilt-android

https://dagger.dev/hilt/view-model.html

https://github.com/google/dagger/releases

Mr.Dark
  • 241
  • 3
  • 5
6

In your Build.gradle (:app), remove this line

implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"

Your ViewModel should use @HiltViewModel & @Inject instead of @ViewModelInject

@HiltViewModel
class MainViewModel @Inject constructor() : ViewModel() { }
Gmacv
  • 383
  • 3
  • 6
0

Some dependencies were deprecated. Hence, you can modify build.gradle (:app) and project level build.gradle as shown below.

Additionally, you may look up the official source at: Dependency injection with Hilt

build.gradle (:app)

plugins{
   ...
   id 'dagger.hilt.android.plugin'
   id 'kotlin-kapt'
}

dependencies {
   ...
   // Dagger Hilt
   implementation("com.google.dagger:hilt-android:2.44.2")
   kapt("com.google.dagger:hilt-android-compiler:2.44.2")
}

kapt {
   correctErrorTypes true
}

build.gradle (Project's root)

plugins {
  ...
  id("com.google.dagger.hilt.android") version "2.44" apply false
}