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
150
votes
4 answers

Dagger 2 subcomponents vs component dependencies

Dagger 1's plus() method is something I used quite often in previous applications, so I understand situations where you might want to have a subcomponent with full access to the parent graphs bindings. In what situation would it be beneficial to use…
Bradley Campbell
  • 9,298
  • 6
  • 37
  • 47
124
votes
1 answer

"Prefer to run the dagger processor over that class instead" in Kotlin

I'm getting this note in the build whenever I do an inject into a kotlin class (btw, I have a mixed android project with both kotlin and java). For example, after this gradle task: compileStagingDebugJavaWithJavac (StagingDebug is my build variant),…
Fábio Carballo
  • 3,245
  • 5
  • 26
  • 36
105
votes
2 answers

How to set up DAGGER dependency injection from scratch in Android project?

How to use Dagger? How to configure Dagger to work in my Android project? I'd like to use Dagger in my Android project, but I find it confusing. EDIT: Dagger2 is also out since 2015 04 15, and it's even more confusing! [This question is a "stub" on…
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
88
votes
5 answers

Dagger- Should we create each component and module for each Activity/ Fragment

I've been working with dagger2 for a while. And I got confused wether to create an own component/module for each Activity/ Fragment. Please help me clarify this: For example, We have an app, and the app has about 50 screens. We will implement the…
Mr Mike
  • 899
  • 1
  • 7
  • 7
71
votes
5 answers

Dagger: IllegalArgumentException: No injector factory bound for Class

I am new to Dagger 2. I have 2 Activities, I want to use injected ViewModel for both. Here is my ViewModuleFactory : @Singleton public class ProductViewModelFactory implements ViewModelProvider.Factory { private final Map
dev_android
  • 8,698
  • 22
  • 91
  • 148
71
votes
17 answers

Dagger 2 component not generated

In my module, in my base Application class component = DaggerCompClassComponent.builder() .classModule(new ModuleClass()).build(); it can not find DaggerCompClassComponent. I have on module build.gradle apply plugin:…
ghita
  • 2,746
  • 7
  • 30
  • 54
69
votes
12 answers

Kotlin Error: Dagger does not support injection into private fields

I use in kotlin activity ViewPager and I want in Kotlin Fragment use the dagger injection. I have got Error: Dagger does not support injection into private fields. In Java Fragment the dagger injection work. Why can i not inject dagger in kotlin…
eurosecom
  • 2,932
  • 4
  • 24
  • 38
69
votes
5 answers

Difference between Dagger and ButterKnife Android

Can anyone point out the difference between Dagger and Butterknife? I know that Butterknife is a view injection library and Dagger is a dependency injection library. But the documentation online seems a bit overhead for me. According to Butterknife…
user2511882
  • 9,022
  • 10
  • 51
  • 59
66
votes
9 answers

How do you override a module/dependency in a unit test with Dagger 2.0?

I have a simple Android activity with a single dependency. I inject the dependency into the activity's onCreate like this: Dagger_HelloComponent.builder() .helloModule(new HelloModule(this)) .build() .initialize(this); In my…
G. Lombard
  • 3,569
  • 1
  • 29
  • 30
63
votes
8 answers

Dagger and Butter Knife vs. Android Annotations

I am evaluating Dependency Injection (DI) frameworks for an Android app. The top contenders are: Dagger (with Butter Knife) and Android Annotations. I understand that Dagger and ButterKnife are from the same source- square and they complement each…
62
votes
5 answers

Dagger 2 - what is the purpose of a @Singleton annotation class

From the dagger 2 Documentation I noticed that you can have a @Singleton annotated class. What is the purpose of marking a class as @Singleton as I have tried to do this in my code but a singleton object is NOT produced. I'm not clear on what use…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
62
votes
4 answers

Does "Avoid dependency injection frameworks" in the Android Memory Guide apply to Dagger as well?

So I have come across this best practices on Android articles on memory performance. http://developer.android.com/training/articles/memory.html They said Avoid dependency injection frameworks Using a dependency injection framework such as Guice or…
toy
  • 11,711
  • 24
  • 93
  • 176
58
votes
3 answers

How to use dagger in a android library project

I'm currently trying to add Dagger to my android projects. For the apps projects its easy and clear to me, how to build the ObjectGraph. But I dont quite know whats the best way to do this in my android library projects. Should I keep building the…
sebastian
  • 2,610
  • 2
  • 17
  • 28
55
votes
4 answers

Dagger 2 examples

Dagger 2 is around the corner but the available examples wouldn't even compile right off the box, and the documentation is a copy-paste-replace from Dagger 1. Does anyone have an example of a proper application working on Google's Dagger 2?
MLProgrammer-CiM
  • 17,231
  • 5
  • 42
  • 75
52
votes
2 answers

Is ApplicationComponent deprecated?

I just started using Dagger Hilt for dependency injection on Android and I get a warning on Android Studio saying 'ApplicationComponent' is deprecated. Deprecated in Java. I didn't find anything related to this warning while Googling, is it actually…
Kostas Andrianos
  • 1,551
  • 2
  • 16
  • 21
1
2 3
98 99