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
16
votes
8 answers

Dagger-Hilt: @ViewModelInject is not injecting MyViewModel and crash?

In exploring the ViewModelInject of Dagger-Hilt, I follow the example in https://developer.android.com/training/dependency-injection/hilt-jetpack#viewmodels I try to inject the ViewModel into my activity as follow import…
Elye
  • 53,639
  • 54
  • 212
  • 474
15
votes
2 answers

Hilt in non-android module clean architecture setup

I have a multi modular android app setup which consists of a Data, Domain and Presentation module. The Domain module is java-only. I know it's possible to support hilt in non-android modules by adding: Domain build.gradle implementation…
Tim
  • 669
  • 1
  • 8
  • 27
15
votes
3 answers

Injecting CoroutineWorker with Hilt : Could not instantiate woker

Same questions have been asked but they didn't work for me , In the start I was using the latest version of work manager which is 2.7 alpha 3 but I downgraded since its only compatible to android 12 preview sdk , The error still remained there ! It…
Waqas Tahir
  • 7,171
  • 5
  • 25
  • 47
15
votes
1 answer

Hilt viewmodel injection into instrumentation tests

I was searching quite a lot for how to inject ViewModel into tests so I can test it. Lets say the viewmodel have a constructor injection with some business logic interactor. I can inject it into fragments easily but no success in…
Alex
  • 327
  • 3
  • 11
15
votes
6 answers

HILT : lateinit property repository has not been initialized in ViewModel

I am facing this issue in multi module android project with HILT. kotlin.UninitializedPropertyAccessException: lateinit property repository has not been initialized in MyViewModel My modules are App Module Viewmodel module UseCase…
Arun
  • 510
  • 1
  • 4
  • 21
14
votes
2 answers

Jetpack Compose instrument test with @HiltViewModel

So I want to test my jetpack compose project. It's easy enough running an instrument test following [these instructions]1 on android dev site, but when you add @HiltViewModel injection into the combination things get complicated. I'm trying to test…
Vahid
  • 1,829
  • 1
  • 20
  • 35
14
votes
2 answers

The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found

im getting this gradle sync error - The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found. can someone help please ? :) im using kotlin dsl, i have libs.kt in buildSrc buildSrc -…
Kochchy
  • 591
  • 1
  • 6
  • 17
13
votes
4 answers

How to handle back navigation with Jetpack Compose + Navigation (without fragments)

I am trying to navigate lets say from onboarding to dashboard and beyond and pop the onboarding once user hits dashboard, but still with 'back action' I end up on onboarding again. Here is the sample code: @AndroidEntryPoint class MainActivity :…
13
votes
6 answers

@HiltViewModel annotated class should contain exactly one @Inject annotated constructor

I keep getting this error: public final class MainViewModel extends androidx.lifecycle.ViewModel { ^ @HiltViewModel annotated class should contain exactly one @Inject annotated constructor. [Hilt] Processing did not complete. See error above for…
Atick Faisal
  • 1,086
  • 1
  • 6
  • 13
13
votes
3 answers

Hilt field injection in the super Fragment or ViewModel

I'm using Dagger-Hilt for dependency injection in my Android project, now I have this situation where I have a base abstract Fragment BaseViewModel.kt abstract class BaseViewModel constructor( val api: FakeApi, ) : ViewModel() { //... …
Hamza Sharaf
  • 811
  • 9
  • 25
13
votes
3 answers

Hilt ViewModel has no zero argument constructor

Cannot create an instance of class com.comp.app.winners.WinnersViewModel Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor Getting an error when trying to resolve a…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
13
votes
2 answers

What is difference between ActivityRetainedComponent @ActivityRetainedScope and ActivityComponent @ActivityScoped in dagger hilt android

What is difference between ActivityRetainedComponent @ActivityRetainedScope and ActivityComponent @ActivityScoped in dagger hilt android
13
votes
1 answer

Dagger Hilt not injecting on tests

I'm trying to run a test using Dagger Hilt with Robolectric: @RunWith(RobolectricTestRunner::class) @UninstallModules(LevelModule::class, AppModule::class) @Config(sdk = [16, 28], application =…
Lucas Lima
  • 1,465
  • 2
  • 16
  • 28
13
votes
5 answers

Provide Activity instance with Hilt

How can I translate something like this: @Module abstract class BaseActivityModule { @Binds abstract fun provideActivity(activity: A): AppCompatActivity companion object { @Provides …
GuilhE
  • 11,591
  • 16
  • 75
  • 116
12
votes
1 answer

How to generate objects with the same type in Hilt?

@Module @InstallIn(SingletonComponent::class) object NetworkModule { @Singleton @Provides fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit { return Retrofit.Builder() …
pnkj
  • 406
  • 5
  • 17