0PricingLogin
Jetpack Compose Academy · Lesson

hiltViewModel & @HiltViewModel

Inject ViewModels into Composables.

ViewModels Need Dependencies Too

Your ViewModel usually depends on a repository or use case. Hilt can inject those for you, so you never build them by hand again.

Annotate the ViewModel

Mark the class with @HiltViewModel and add @Inject to its constructor. Hilt now knows how to create it with all its dependencies.

@HiltViewModel
class FeedViewModel @Inject constructor(private val repo: Repo) : ViewModel()

All lessons in this course

  1. Why DI: Testable, Decoupled Code
  2. Modules, @Provides & @Binds
  3. hiltViewModel & @HiltViewModel
  4. Scopes & Component Lifetimes
← Back to Jetpack Compose Academy