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
51
votes
3 answers

Can I just inject super class when use dagger2 for dependency injection?

I use Dagger2 for DI in my android application. I found that I have to write inject method for every class that uses @Inject field. Is there a way that I can just inject the parent class so that I don't have to call inject on every subclass? Take…
Chris.Zou
  • 4,506
  • 6
  • 31
  • 38
50
votes
6 answers

Dagger 2 error: dependency "cannot be provided without an @Inject constructor" while it actually annotated with @Inject

I've started using Dagger 2 and faced strange issue that looks like a bug to me. I have 3 modules, that are composed into one subcomponent, which in turn extends/pluses higher level component. Subcomponent is pretty simple: just combination of…
dominus
  • 1,082
  • 1
  • 11
  • 14
48
votes
6 answers

Can't locate import javax.inject.Inject package

I'm trying to implement Dagger as a dependency injector in an IntelliJ project, but my code is failing on: import javax.inject.Inject; Intellij is finding the 'javax' package, but not the 'inject' package, so it fails. I am new to Android, so I…
Indigo Nai
  • 531
  • 1
  • 4
  • 7
46
votes
4 answers

Dagger 2 - two provides method that provide same interface

lets say I have: public interface Shape {} public class Rectangle implements Shape { } public class Circle implements Shape { } and I have a ApplicationModule which needs to provides instances for both Rec and Circle: @Module public class…
Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101
43
votes
2 answers

Dagger 2 - Why is this a dependency cycle?

I'm trying to inject the application's Context into 2 other objects, an AuthManager and an ApiClient. Both of them depends on said context, and the ApiClient depends on the AuthManager. Why is this a dependency cycle, if Context doesn't have a…
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
41
votes
7 answers

Failed to resolve variable '${project.groupId}'

I migrate my project to AndroidX and I got these errors when building the project: [TAG] Failed to resolve variable '${project.groupId}' [TAG] Failed to resolve variable '${project.version}' [TAG] Failed to resolve variable…
MHogge
  • 5,408
  • 15
  • 61
  • 104
40
votes
5 answers

Dagger 2: Cannot be provided without an @Provides-annotated method

I just started learning dagger2 and faced a strange issue that looks like a bug to me. Here's the module: @Module public class SimpleModule { @Provides Cooker providerCooker() { return new Cooker("tom", "natie"); …
hanchen ke
  • 455
  • 1
  • 4
  • 7
40
votes
5 answers

Dagger 2 Generic Type class inject error

I'm not able to let MyClass here being injected due to its Generic nature. Dagger complains with this error: Error:(187, 10) error: com.test.MyClass has type parameters, cannot members inject the raw type. via: …
Andrea Baccega
  • 27,211
  • 13
  • 45
  • 46
38
votes
5 answers

Dagger injection not working for "object" in Kotlin

After spending a ludicrous amount of time trying to figure out why my dagger injections weren't working; I realised that the "object" type in Kotlin was the problem. The following did not work, the injected "property" was null. object SomeSingleton…
Nihilanth
  • 401
  • 1
  • 4
  • 8
38
votes
3 answers

Specifying order of annotation processors

I'm trying to run Dagger 2 as well as Lombok on my Java project. Lombok has to run first, of course, but whether it actually does seems to be up to chance. At first I suspected I could specify the order by the respective position of the library jars…
Torque
  • 3,319
  • 2
  • 27
  • 39
36
votes
4 answers

DaggerAppComponent not created

With dagger 2.10 I used to be able to create the app component by doing sAppComponent = DaggerAppComponent.builder() .appModule(new AppModule(this)) .sessionModule(new SessionModule()) .netModule(new…
kingston
  • 11,053
  • 14
  • 62
  • 116
35
votes
4 answers

Dagger + Retrofit. Adding auth headers at runtime

I'm wondering if there is a way for Dagger to know that it should recreate an object when new data is available. The instance I am speaking of is with the request headers I have for retrofit. At some point (when the user logs in) I get a token that…
AIntel
  • 1,087
  • 5
  • 14
  • 27
35
votes
8 answers

How to inject into a BroadcastReceiver

Does someone already had to inject an already existing class, with some business logic, into a BroadcastReceiver using dagger? I'm using dagger 1 and already found a nice example (https://github.com/adennie/fb-android-dagger) but, I could not find…
carlosmaciel
  • 801
  • 1
  • 6
  • 17
34
votes
5 answers

Can I extend a custom Application in Espresso?

I'm attempting to set up Dagger in my Espresso instrumentation tests in order to mock out calls to external resources (RESTful services in this case). The pattern I followed in Robolectric for my unit testing was to extend my production Application…
Jim Kirkbride
  • 379
  • 1
  • 4
  • 7
34
votes
1 answer

Dagger on default constructors

I am trying to get Dagger up an working on my project. However I get the following exception on one of my classes during compilation: error: No injectable members on Foo. Do you want to add an injectable constructor? However, the class have no…
foens
  • 8,642
  • 2
  • 36
  • 48
1
2
3
98 99