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
33
votes
6 answers

@Generated annotation using gradlew + dagger

I'm confronting a weird issue using gradlew(4.10.2) + dagger(2.18). The problem is when I call: ./gradlew :app:compileDebugAndroidTestKotlin The build fails with: Task :Common:compileDebugJavaWithJavac…
crgarridos
  • 8,758
  • 3
  • 49
  • 61
33
votes
1 answer

When to use Dagger in android application?

I know that Dagger is a dependency injection framework, but I have not still used it in my projects. I am starting a new project and wondering if Dagger is suitable for it. As far as I understand, using of Dagger leads to a lot of boilerplate code…
Sergey Novikov
  • 381
  • 3
  • 4
32
votes
1 answer

Android Dagger 2: Inject versus Provides

I have a question regarding Android Dagger 2 und the usage of @Inject and @Provide annotations. Given are the following two simplified examples: public class A { String msg; public A(String msg){ this.msg = msg; } } public class B { …
Christian Ammann
  • 888
  • 8
  • 19
30
votes
4 answers

Remove dagger generated classes from Android Studio's global search?

It is really irritating. I need more time to find some class because of dagger 2.
Mladen Rakonjac
  • 9,562
  • 7
  • 42
  • 55
29
votes
2 answers

Update to Kotlin 1.3.30 breaks build with Dagger 2.21

Build error after update from Kotling 1.3.21 to 1.3.30: AppComponent.java:16: error: [Dagger/MissingBinding] java.util.Map, javax.inject.Provider> …
yaroslav
  • 863
  • 1
  • 8
  • 20
29
votes
5 answers

How to set up dependency injection using Dagger for things other than Activities and Fragments?

I started setting up dependency injection using Dagger as follows. Please feel encouraged to correct my implementation since I might have mistakes in there! The implementation follows the android-simple example provided by the project. In the…
29
votes
2 answers

Dagger: Inject @Named strings?

EDIT 2023-06-28: Unless you have very good reason not to, just use Spring Boot as your platform and use its auto wiring along with all its other goodies coming for free. EDIT 2018-02-08: Sample project demonstrating how to do this at…
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
29
votes
6 answers

Using Dagger for dependency injection on constructors

So, I'm currently redesigning an Android app of mine to use Dagger. My app is large and complicated, and I recently came across the following scenario: Object A requires a special DebugLogger instance which is a perfect candidate for injection. …
Alex
  • 1,103
  • 1
  • 11
  • 24
27
votes
4 answers

LiveData is not updating its value after first call

I have been beating my head against the wall and I cannot understand why this is happening. I am working with the new Architectural Components for Android and I am having problems updating a LiveData with a List of Objects. I have two spinners. When…
joao86
  • 2,056
  • 1
  • 21
  • 23
27
votes
1 answer

How can I build a Dagger-based Android library without forcing consuming applications to use Dagger?

I'm working on an Android library that is basically a client for some REST services I've written. I have several storage classes, network queues, parsers, and so on, and like many such classes, they have dependencies on Context or on things like…
Dalbergia
  • 1,699
  • 1
  • 17
  • 26
27
votes
2 answers

Getting rid of Dagger 2 warning "Generating a MembersInjector"

Given the following classes abstract class AbstractClass { @Inject SomeDependency someDependency; } class SomeClass extends AbstractClass { @Inject AnotherDependency anotherDepenency; public void onCreate() { component = // Get…
Sven Jacobs
  • 6,278
  • 5
  • 33
  • 39
25
votes
4 answers

Looking for an example for Dagger assisted injection

From dagger-discuss@: I have a class that gets some dependencies from the object graph, and other dependencies from a caller at runtime. public class ImageDownloader { // Get these dependencies from the injector. private final HttpClient…
user1071136
  • 15,636
  • 4
  • 42
  • 61
24
votes
1 answer

@Named providers with same return types end up giving java.lang.IllegalArgumentException: Duplicate

Here is the issue, I am working on a LibGDX project where i have different modules for different platforms. This is how my android module looks like: @Module( includes = {BaseModule.class, NetModule.class}, injects =…
24
votes
2 answers

Java dependency injection: Dagger 1 vs Dagger 2, which is better?

What are the advantages of Dagger 2 over Dagger 1? So far I found (just) 2: Dagger 2 allows you to use code obfuscation with proguard Dagger 2 is faster (which is not much of an advantage when using it for android application but it is sure an…
Ognyan
  • 13,452
  • 5
  • 64
  • 82
24
votes
4 answers

Using Kotlin with Dagger

What's the best way to use Dagger to inject dependencies into classes, especially zero-arg constructor classes like Activities, with Dagger? Will Dagger 2 possibly bring improvements to the situation? Thanks in advance.
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
1 2
3
98 99