Dagger Hilt Android gives Error : method getActivityFactory in class DefaultViewModelFactories cannot be applied to given types

Viewed 1072

I am working on a project where I have implemented Dagger Hilt + Retrofit + Coroutine.

But after a new update of dependencies of dagger hilt, the project is not able to run.

I am getting this error for the activities and fragments in which I have implemented Dagger and Viewmodels with Dependency Injection. Earlier before the dependency update, it was working fine.

Here is the screenshot for the same:

enter image description here

Here is the class that is created by Dagger and that has the error:

enter image description here

If anyone has faced this same issue. I don't know what I am doing wrong. I have followed this tutorial. This tutorial is also working fine for me, but I am getting the above errors after updating the dependencies.

2 Answers

Ok, I find out what was the problem.

The problem is in the official documentation of Dagger Hilt here

See the screenshot of that document below.

enter image description here

The problem is in the code of documentation. It's really frustrating why they don't update their official documentation!!! Let it be.......

We have to change

FROM

@HiltViewModel
class MyViewModel @Inject constructor(
    private val mainRepository: MainRepository
) : ViewModel() {

TO

@HiltViewModel
class MyViewModel @Inject constructor(
    private val mainRepository: MainRepository
) : ViewModel(), LifecycleObserver {

They have missed LifecycleObserver and because of that, I was facing errors.

Not only this,

You should also note that following dependency's version should also be the same.

in App Level Gradle:

implementation "com.google.dagger:hilt-android:2.35.1"
kapt "com.google.dagger:hilt-android-compiler:2.35.1"

in Project Level Gradle:

classpath "com.google.dagger:hilt-android-gradle-plugin:2.35.1"

dependency version for project level and module level must be same .

Related