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
22
votes
1 answer

How to mock the view model with Hilt for unit testing fragments?

I've got an android app setup for dependency injection using Hilt, and would like to unit test my fragments. I'm currently creating my view model using: private val viewModel: ExampleViewModel by viewModels() And I am creating the fragment for…
kmell
  • 223
  • 2
  • 4
21
votes
2 answers

How to use Hilt to inject a safe-args argument into a viewmodel?

I have found a similar question here. At the time of writing this question there is only this answer avaliable, which does not provide any help to me, and I believe also to the person who asked the question. I checked the repo which is linked in the…
21
votes
6 answers

jetpack compose viewModel() is giving error "has no zero argument constructor" with hilt

In my project, compose viewModel() method is giving error "has no zero argument constructor" when I am using hilt. @Composable fun HomeScreen(homeViewModel: HomeViewModel = viewModel()) { ... } @HiltViewModel class…
Vivart
  • 14,900
  • 6
  • 36
  • 74
20
votes
1 answer

How to pass parameter to viewmodel constructor using HiltViewModel library with jetpack compose

How to pass parameter to viewmodel constructor using HiltViewModel library with jetpack compose My code: @HiltViewModel class GetPurchaseViewModel @Inject constructor(val id:Long) : ViewModel() { private val service= RestModule var state =…
Rafael Souza
  • 1,143
  • 2
  • 13
  • 29
20
votes
3 answers

How can Hilt be used effectively in multi-module apps following CLEAN architecture principles?

I am building an Android app following the Clean Architecture Principles. Here is what I've got: app module: Contains all the Android dependencies. Uses MVVM with ViewModel from the arch components. ViewModels only communicate with UseCases, which…
19
votes
2 answers

Injecting CoroutineWorker using Hilt

I am trying to inject a coroutine worker using dagger hilt, I 've followed all the instructions in the documentation https://developer.android.com/training/dependency-injection/hilt-jetpack which was intended for "Worker" not "coroutine…
Mina Isaac
  • 303
  • 1
  • 2
  • 6
19
votes
1 answer

Dagger Hilt provide alternative Modules for different flavors/build types

I try to migrate an App to Dagger Hilt. In my old setup I switched a Module for a Debug Version in Debug builds or for different product flavors. E.g.: @Module open class NetworkModule { @Provides @Singleton open fun…
dipdipdip
  • 2,326
  • 1
  • 21
  • 31
17
votes
2 answers

Handle multiple retrofit client using dagger hilt as dependency injection?

I want to use two different backend in my android application with different response format, i am using hilt as dependency injection with retrofit for network calling, which is great to work on. as i have added my 2nd server network files and in…
Manzoor Ahmad
  • 481
  • 6
  • 21
17
votes
1 answer

Field Injection via Hilt outside of Fragment and Activity

I was wondering that is it possible to use field injection outside of fragment or activity? I know I can use constructor injection but, I am wondering is it possible with field injection, as well. I think it was possible with Dagger. When I try to…
haliltprkk
  • 1,428
  • 3
  • 14
  • 26
17
votes
2 answers

java.lang.RuntimeException: Hilt classes generated from @HiltAndroidTest are missing

I am trying to run a simple Robolectric test with Hilt. @HiltAndroidTest @Config(application = HiltTestApplication::class) @RunWith(AndroidJUnit4::class) class SplashActivityTest2 { @get:Rule var hiltRule = HiltAndroidRule(this) @Test …
theapache64
  • 10,926
  • 9
  • 65
  • 108
17
votes
3 answers

dagger.hilt.android.internal.managers.ViewComponentManager$FragmentContextWrapper cannot be cast to android.app.Activity

I'm using a third-party library that has some custom view implementations. In the implementation, they call Utils.showSoftKeyboard(context as Activity). This, along with using the fragment as an @AndroidEntryPoint causes the following…
Carter Hudson
  • 1,176
  • 1
  • 11
  • 23
17
votes
2 answers

How to inject app: Context in ViewModel with Hilt?

Hello, I'm trying to inject view model using Hilt, but I get the following error: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.wordssample, PID: 25250 java.lang.RuntimeException: Unable to start activity…
Tarsila Costalonga
  • 924
  • 2
  • 11
  • 21
17
votes
2 answers

Hilt dependency injection for UI testing says "HiltAndroidRule" wasn't added but it was

I'm trying to use Hilt for dependency injection but it gives the error java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule. The HiltAndroidRule is added…
Questioner
  • 2,451
  • 4
  • 29
  • 50
16
votes
3 answers

The best way to wrap sharedPreference object in hilt

I have a sharedPreference object, and I want to make it as dependency inject component through the project. // sharedPreference object private const val PREF_TAG = "tag" object MyPreference { fun getStoredTag(context: Context): String { …
ccd
  • 5,788
  • 10
  • 46
  • 96
16
votes
2 answers

Does Hilt replace Dagger2?

Digging into Google's DI framework Dagger2 to decide for a DI Framewor to use it in an middle sized app, I also noticed Hilt So according to its overview description: Hilt works by code generating your Dagger setup code for you. This takes away most…
Pavel Pipovic
  • 339
  • 1
  • 3
  • 12
1 2
3
88 89