Questions tagged [dagger-2]

Dagger 2 is a dependency injection framework for Java and Android. It implements the full stack with generated code

Dagger 2 is a compile-time dependency injection framework for and .

It uses code generation and compile-time validation taking the approach started with Dagger 1 to it's ultimate conclusion. Dagger 2 eliminates all reflection and improves code clarity by removing the traditional ObjectGraph/Injector in favor of user-specified @Component interfaces. Dagger 2 implements the full stack with generated code.

Useful Links

3161 questions
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
4 answers

What is the proper way to implement .addCallback() when providing RoomDatabase via Dagger 2?

I'm using Dagger 2 to create and share my RoomDatabase where necessary in my app. I'm trying to implement addCallback() so I can override the database's onCreate() function and use it to insert my initial database values. This is where I'm running…
Chris Feldman
  • 201
  • 2
  • 4
20
votes
2 answers

kotlin + Dagger2 : cannot be provided without an @Provides-annotated method

I don't understand why am I getting this error: Error:(12, 2) error: [dagger.android.AndroidInjector.inject(T)] java.util.Map,? extends…
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
20
votes
4 answers

How do I use AndroidInjection class in custom views or other android classes?

My issue with the Android-specific pattern is, if you use their AndroidInjection class, there is no way to members inject other objects besides Activities/Fragments/custom views/adapters, except with the Application Component. This is because you…
Vivek Maskara
  • 1,073
  • 11
  • 29
20
votes
9 answers

Inject ViewModel using Dagger 2 + Kotlin + ViewModel

class SlideshowViewModel : ViewModel() { @Inject lateinit var mediaItemRepository : MediaItemRepository fun init() { What goes here? } So I'm trying to learn Dagger2 so I can make my apps more testable. Problem is, I've already integrated…
easycheese
  • 5,859
  • 10
  • 53
  • 87
20
votes
9 answers

Dagger @ContributesAndroidInjector ComponentProcessor was unable to process this interface

I was testing new feature of dagger: Android module. And I am not able to compile the code when I use @ContributesAndroidInjector I am always getting following error: Error:(12, 8) error: dagger.internal.codegen.ComponentProcessor was unable to…
Rostyslav Roshak
  • 3,859
  • 2
  • 35
  • 56
20
votes
4 answers

Android Dagger2 + OkHttp + Retrofit dependency cycle error

Hey there I am using Dagger2, Retrofit and OkHttp and I am facing dependency cycle issue. When providing OkHttp : @Provides @ApplicationScope OkHttpClient provideOkHttpClient(TokenAuthenticator auth,Dispatcher dispatcher){ return new…
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
20
votes
12 answers

cannot find symbol class DaggerAppComponent or cannot find symbol class DaggerActivityComponent

I get this error after adding inject on my class then it gives me a compilation error. If I remove @Inject static ApiService mApiService; it's working fine And I'm using 2 Application class those are extended MultidexApplication because I have…
mbpatel
  • 501
  • 1
  • 5
  • 19
20
votes
2 answers

how it works @BindsInstance dagger 2

I have recently updated dagger 2.8 to 2.9 dagger. and documentation of the last release have been added as follows: -Added @BindsInstance for component builders to easily bind instances that are constructed outside of the graph. -Producers: Added…
Juanes30
  • 2,398
  • 2
  • 24
  • 38
19
votes
1 answer

CustomScope may not reference bindings with different scopes

I am new to dagger, I have defined my application component like this @Singleton @Component(modules = {ApplicationModule.class}) public interface ApplicationComponent { void inject(BaseActivity activity); Context context(); } This is my…
19
votes
3 answers

How to provide database to MyFirebaseMessagingService using Dagger 2 so that I can store fcm message locally in android

How can I make it possible to pass database Instance to the MyFirebaseMessagingService class which extends FirebaseMessagingService so that I can save data payload locally? Note: I already setup dagger 2 in my app, it's working perfectly. Below is…
19
votes
1 answer

How to implement Dagger for worker classes?

Since the Worker class is created by the framework (WorkerManager), how can we use @Inject fields into the Worker?
19
votes
4 answers

Error: @dagger.android.ContributesAndroidInjector was used, but dagger.android.processor.AndroidProcessor was not found

I am trying to setup Dagger 2.12 and I'm getting this error: error: @dagger.android.ContributesAndroidInjector was used, but dagger.android.processor.AndroidProcessor was not found on the processor path Here's how I've configured Dagger: My…
Micah Simmons
  • 2,078
  • 2
  • 20
  • 38
19
votes
8 answers

Error: cannot find symbol variable DaggerAppComponent

While trying to integrate latest Dagger 2 version, I am facing problem of Dagger auto generation. Dagger is not auto generating DaggerAppComponent in spite of several Rebuilds and Make Module App process. Application class: public class…
reactivedroid
  • 752
  • 1
  • 7
  • 14
19
votes
2 answers

Application does not implement dagger.android.HasDispatchingActivityInjector

I have a subclass of Application that I am conforming to HasDispatchingActivityInjector, but when I try and run my app it will crash, saying: Unable to start activity ComponentInfo{com.test.testing/com.test.testing.ui.main.MainActivity}:…
Jordan
  • 1,213
  • 1
  • 9
  • 11