Questions tagged [dagger]

Dagger is a dependency injection library for Java and Android.

Dagger, by Square, is a fast dependency injection library for Java and Android. It is compatible with JSR 330. Dagger intentionally leaves out some features and imposes some restrictions in order to attain rapid startup times by eliminating slow runtime reflection, especially on Android.

Dagger 2 is a fork maintained by Google which eliminates reflection in the graph generation phase.

For instance, Dagger does not support the following:

  1. Method Injections.
  2. private and final field injections.

Useful Links

1471 questions
24
votes
1 answer

What are the specific benefits of using DI on Android?

What are the specific benefits or advantages of using a dependency injection framework for Android, like Dagger, Transfuse or RoboGuice? For example, what kind of apps would benefit the most from using DI? is there more of a performance advantage,…
Acapulco
  • 3,373
  • 8
  • 38
  • 51
23
votes
3 answers

Dagger 2 cannot access Retrofit

I'm trying to provide an instance of Retrofit to my Repository using Dagger 2 (with Android module). Buy I'm facing the error: Error:cannot access Retrofit Other instances like Picasso was injected with success, I just have problems with…
fe_araujo_
  • 1,859
  • 1
  • 14
  • 15
23
votes
2 answers

CustomView dependency injection with dagger 2 (within activity scope)

My question is similar to this. So for instance, I have a LiveData implementation: public class CustomLiveData extends LiveData { @Inject public CustomLiveData(@ActivityContext Context context) { //.... } } that I…
sinek
  • 2,458
  • 3
  • 33
  • 55
23
votes
3 answers

How to resolve a circular dependency while still using Dagger2?

I have two classes, Foo and Bar, which depend on each other, as well as various other classes. I am using Dagger-2 for dependency injection, but if I naively add the circular dependency, Dagger hits a stack overflow at runtime. What's a good way…
23
votes
2 answers

Generic @Inject'd fields in an abstract superclass

Consider a MVP-ish set of types. An abstract Presenter exists, with a View interface: public interface View { //... } public abstract class AbstractPresenter { @Inject V view; //... } Then, lets have a specific concrete…
Colin Alworth
  • 17,801
  • 2
  • 26
  • 39
22
votes
7 answers

dagger android support to androidx.fragment

How to inject a fragment from the package androidx.fragment.app.Fragment ? I'm using the dagger-android framework to inject my dependencies in my code. As the documentation says I do this to inject my fragment @Override public void onAttach(Activity…
Irving Dev
  • 389
  • 2
  • 8
22
votes
2 answers

How do I inject into a Servlet with Dagger 2?

I asked (and answered) the same question for Dagger 1 here. How would I do something similar for Dagger 2, now that ObjectGraph.inject no longer exists. This question could be generalized to: How do you do members injection if the object must be…
Ben
  • 4,785
  • 3
  • 27
  • 39
21
votes
1 answer

Binding Into Map With KClass Type

I am trying to bind subclasses of ViewModel into a map by their KClass types: @Module abstract class ViewModelModule { @Binds @IntoMap @ViewModelKey(MyViewModel::class) abstract fun bindsMyViewModel(viewModel: MyViewModel): ViewModel …
Bryan
  • 14,756
  • 10
  • 70
  • 125
21
votes
3 answers

Refreshing Dagger 2 instance from Android Application class

I have a set of @Singleton and @Provides method in my module class for the purpose of creating Singleton instance throughout the application. Everything works fine except few bottle neck scenarios like as follows: STEP 1. I am creating a Retrofit…
Chandru
  • 5,954
  • 11
  • 45
  • 85
21
votes
2 answers

What is dagger exactly, and how it works

I know this may be not the correct way to ask question but after reading lot and lot I am still confused about daggers and how it works and why we should use it. Since its been used in my current working project. Please somebody give me little hint…
eLemEnt
  • 1,741
  • 14
  • 21
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
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

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
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…