13

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()
}
Atick Faisal
  • 1,086
  • 1
  • 6
  • 13

6 Answers6

16

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()
HTMLlama
  • 1,464
  • 2
  • 11
  • 16
2

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

heyheyhey
  • 1,216
  • 8
  • 12
1

Make sure you are using this inject annotation

import javax.inject.Inject

instead of this one

com.google.inject.Inject
AncientMethods
  • 256
  • 1
  • 9
1

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.

Amir Golan
  • 378
  • 3
  • 8
1

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.

hushed_voice
  • 3,161
  • 3
  • 34
  • 66
0

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