Questions tagged [dagger-hilt]

Hilt is a Dagger2-based dependency injection library for Android.

The goals of Hilt are:

  • To simplify Dagger-related infrastructure for Android apps.
  • To create a standard set of components and scopes to ease setup, readability/understanding, and code sharing between apps.
  • To provide an easy way to provision different bindings to various build types (e.g. testing, debug, or release).
1333 questions
12
votes
1 answer

Hilt injection in Android Services

I want to inject a class in Service. Lets have a look at the code below: class DeviceUtil @Inject constructor() { ... } @AndroidEntryPoint class LocationUpdateService : Service() { @Inject lateinit var deviceUtil: DeviceUtil …
Amir Raza
  • 2,320
  • 1
  • 23
  • 32
12
votes
2 answers

How to Bind/Provide Activity or Fragment with Hilt?

I'm trying to implement Hilt on an Android App, while It's pretty easy to implement and removes a lot of the boilerplate code when comparing with Dagger, there are some things I miss, Like building my own components and scoping them myself so i'll…
Danny
  • 345
  • 1
  • 4
  • 11
12
votes
4 answers

Hilt creating different instances of view model inside same activity

After recently migrating from Dagger to Hilt I started observing very strange behavior with respect to ViewModels. Below is the code snippet: @HiltAndroidApp class AndroidApplication : Application() {} @Singleton class HomeViewModel…
ritesh4302
  • 336
  • 1
  • 5
  • 11
11
votes
3 answers

What is the proper way to navigate from ViewModel in Jetpack Compose + Hilt + ViewModel?

I have stumbled upon this quite trivial, but tricky problem. I have spent a decent amount of time searching official docs, but unfortunately found no answer. Official docs say that you should pass an instance of NavController down to @Composable-s,…
11
votes
1 answer

How to do Assisted Injection with Navigation Compose?

I've a composable called ParentScreen and a ViewModel named ParentViewModel. Inside the ParentViewModel, I am collecting a value from my repo. class MyRepo @Inject constructor() { fun getParentData() = System.currentTimeMillis().toString() //…
11
votes
3 answers

HiltViewModel: Cannot create an instance of class

I am using Hilt. After updating to 1.0.0-alpha03 I got warnings that @ViewModelInject is deprecated and I should use @HiltViewModel. But when I change it I got an error: java.lang.RuntimeException: Cannot create an instance of class…
iknow
  • 8,358
  • 12
  • 41
  • 68
11
votes
3 answers

Dagger Hilt: cannot be provided without an @Provides-annotated method

When I use interface like below in viewmodel class MainViewModel @ViewModelInject constructor( private val trafficImagesRepository: TrafficImageRepository, <----------------- Not working @Assisted private val savedStateHandle:…
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
11
votes
1 answer

how to mock viewmodel that inject using dagger hilt for android testing

I use dagger hilt in my project. I want to write UI test for some fragments. I need to mock the viewModel in the test class and associate it to the fragment under the test.. I read dagger hilt document but I didn't find any solution. class…
maryam
  • 1,437
  • 2
  • 18
  • 40
11
votes
3 answers

How to model parent-child relationship in Android MVVM VMs?

I'm working on an Android piano "quiz" app - users tap on the piano keys and then click the yellow "check" button to submit the answer for evaluation and see the correct answer drawn on the piano. The main QuizActivity has this layout: The upper…
Leprechaun
  • 852
  • 6
  • 25
11
votes
4 answers

HiltWorkerFactory: Configure WorkManagerInitializer at AppStartup

WorkManagerInitializer requires to configure setWorkerFactory to inject dependencies to the Worker class. The [document][1] explains about workManager Initialization at AppStartup, but does not provide any insights on how to configure…
11
votes
2 answers

Android Hilt - multi module project cast error

I am trying to use hilt for my project which contains dynamic features. I am facing an error that I cannot fully understand why. I get an error like this: java.lang.ClassCastException:…
Pehr Sibusiso
  • 862
  • 1
  • 14
  • 27
11
votes
2 answers

@CustomTestApplication value cannot be annotated with @HiltAndroidApp

If the Application has a Custom Application object. It is needed to annotate this with @HiltAndroidApp ex: @HiltAndroidApp class AppCore: Application Appcore has some initialization logic which is needed for the app to run Now in the Instrumentation…
10
votes
1 answer

ViewModel constructor should be annotated with @Inject instead of @AssistedInject

I want to provide some dependency at run time to ViewModel using Hilt. I have followed the solution that was directed by d.android.com this. @HiltViewModel public class ViewViewModel extends ViewModel { ... @AssistedFactory public…
Patriotic
  • 2,103
  • 4
  • 26
  • 36
10
votes
2 answers

Dagger Hilt with multiple implementations of an interface

With Dagger2 it's easy to explicitly create components and list their dependencies. But I can't seem to find a way to provide different implementations of an interface to lets say a fragment. For example, my app has 2 production modes: paid and…
Dmytro Karataiev
  • 1,214
  • 1
  • 14
  • 19
10
votes
0 answers

Espresso Mock Web Server not waiting OkHttp Idling Resource with Dagger Hilt

I am trying to write UI test for my Fragments with Espresso and MockWebServer. I am using OkHttp Idling Resource to tell Espresso to wait for the API calls to complete. But somehow, sometimes my tests are working but sometimes not, because Espresso…
ysfcyln
  • 2,857
  • 6
  • 34
  • 61