@HiltViewModel annotated class should contain exactly one @Inject annotated constructor

Viewed 3567

I keep getting this error:

public final class MainViewModel extends androidx.lifecycle.ViewModel { ^ @HiltViewModel annotated class should contain exactly one @Inject annotated constructor. [Hilt] Processing did not complete. See error above for details.

Here is my MainViewModel:

@HiltViewModel
class MainViewModel @Inject constructor(
    repository: DefaultRepository
) : ViewModel() {
    val items = repository.getItems().asLiveData()
}
6 Answers

In my case the problem was that one of the packages was called "case", which is a name used by some conventions (like of SQL, or even Java).

@Atick Faisal (see comments above) had also another problem with package name, so I guess it might worth a check if you don't find the problem.

Well, I think I figured it out after wasting 3 goddam hours on this. Apparently, you cannot name your package by keywords

I was using enum, and a few people in the comments were using case, default etc.

If only the error message was a bit clearer (-‸ლ).

NB: This is my assumption and it makes sense too.

In my case, I was specifying default values for some of my primary constructor parameters. Getting rid of the default values fixed the issue.

Make sure you are using this inject annotation

import javax.inject.Inject

instead of this one

com.google.inject.Inject

Make sure in your module you have @InstallIn(SingletonComponent::class) instead of @InstallIn(ApplicationComponentManager::class)

When it happened to me, it was because someone removed the injected constructor from class I was injecting.

It was:

@HiltViewModel
class MyViewModel : ViewModel()

But needed to be:

@HiltViewModel
class MyViewModel @Inject constructor() : ViewModel()
Related