Atick Faisal
Atick Faisal

Reputation: 1858

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

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()
}

Upvotes: 20

Views: 10584

Answers (6)

HTMLlama
HTMLlama

Reputation: 1573

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()

Upvotes: 23

hushed_voice
hushed_voice

Reputation: 3608

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.

Upvotes: 1

Amir Golan
Amir Golan

Reputation: 534

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.

Upvotes: 1

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

Upvotes: 0

AncientMethods
AncientMethods

Reputation: 356

Make sure you are using this inject annotation

import javax.inject.Inject

instead of this one

com.google.inject.Inject

Upvotes: 11

heyheyhey
heyheyhey

Reputation: 1656

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

Upvotes: 2

Related Questions