Hilt Multi-module Cannot create an instance of class ViewModel

514 Views Asked by At

I'm trying out a new architecture with multi-module and DI though Hilt, I have the following modules:

  • app: Contains MainActivity (which does nothing except holding fragment)
  • featureHome: Contains HomeFragment and HomeViewModel

When I start the app I'm getting java.lang.RuntimeException: Cannot create an instance of class HomeViewModel

App module

@AndroidEntryPoint
class MainActivity: AppCompatActivity()

FeatureHome

class HomeFragment: Fragment() {
    private val viewModel: HomeViewModel by viewModels()
    ...
}
@HiltViewModel
class HomeViewModel @Inject constructor(): ViewModel {
    ....
}

I'm not sure what to do to resolve this. Should my HomeFragment also have @AndroidEntryPoint ?

1

There are 1 best solutions below

0
On BEST ANSWER

Your Fragment has to be marked with the annotation @AndroidEntryPoint in order to tell Hilt to inject instances in your Fragment. (documentation)