Manually constructing a ViewModel’s dependencies (Repository, which needs an ApiService and a Dao, which need a Retrofit instance and a Database…) gets unwieldy fast, and makes testing hard since you can’t easily substitute fakes. Hilt is Google’s recommended DI framework, built on top of Dagger, purpose-built for Android.
@HiltAndroidApp
class MyApplication : Application()
@AndroidEntryPoint
class MainActivity : AppCompatActivity() { /* ... */ }
@HiltViewModel
class MovieViewModel @Inject constructor(
private val repository: MovieRepository
) : ViewModel() { /* ... */ }
With these annotations in place, Hilt generates the wiring code at compile time — you never call a constructor manually. In the Activity:
class MovieListFragment : Fragment() {
private val viewModel: MovieViewModel by viewModels() // Hilt supplies it automatically
}
- Image
- SKU
- Rating
- Price
- Stock
- Availability
- Add to cart
- Description
- Content
- Weight
- Dimensions
- Additional information