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
40
votes
2 answers

How to make Jersey work with Dagger dependency injection?

Jersey normally uses HK2 dependency injection, but I would like to use Jersey with Dagger 2. Both Dagger and HK2 implement JSR 330, which I have taken as evidence that this should be possible without too much effort. I found ways to make Jersey work…
Rinke
  • 6,095
  • 4
  • 38
  • 55
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
39
votes
2 answers

Using Dagger 2 to inject into service

I have an app which is basically a service that runs all the time and alarms the user when something happens. When the service creates the alarm, it needs to give it his context so that the alarm can do callbacks to the service when something…
39
votes
2 answers

Dagger 2 Custom Scope for each Fragment (or Activity etc...)

I've looked at a couple different articles which seem to suggest two different ways of doing custom scoping in Dagger 2: MVP Presenters that Survive Configuration Changes Part-2 (Github repo): Uses unique custom scopes for each fragment, e.g.…
bcorso
  • 45,608
  • 10
  • 63
  • 75
38
votes
7 answers

Dagger2 + Kotlin: lateinit property has not been initialized

I'm trying to inject the ViewModelFactory into my Activity, but it keeps throwing this same error: lateinit property viewModelFactory has not been initialized. I can't find what I may be doing wrong. See the code above from my…
Guilherme Lima Pereira
  • 1,402
  • 5
  • 17
  • 35
38
votes
7 answers

Android Kotlin : Error Unresolved reference: DaggerAppComponent

I have installed Kotlin plugin today into an existing project with Dagger 2. Before Kotlin was installed I had no issues with Dagger. However, now the compiler complains : Error:(5, 32) Unresolved reference: DaggerAppComponent Error:Execution…
Mike6679
  • 5,547
  • 19
  • 63
  • 108
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
34
votes
9 answers

Is there any way of making IntelliJ IDEA recognizing Dagger 2 generated classes in a Java project?

Context I have started a personal project in java with Gradle as the build system and I want to use Dagger 2 as a DI. The main reason of doing that is to get used to that library and be able to use it easily in bigger projects. What have I…
Alberto S.
  • 7,409
  • 6
  • 27
  • 46
33
votes
4 answers

UninitializedPropertyAccessException: lateinit property has not been initialized

I have a class which i inject into a ViewModel + ViewModel factory, when initialise the view model in the onCreate method in activity, its says the value being passed through is not initialised. Below is my code I am quite new to Kotlin so have…
DanYon
  • 343
  • 1
  • 3
  • 8
33
votes
1 answer

Dagger 2 Injecting Constructors

I'm starting to use Dagger 2 in an application I'm developing but I have some questions about how Dagger 2 works. I get the all the logic behind the @Provides methods and the @Inject annotation for initialising your dependencies, but the @Inject…
Thiago Loddi
  • 2,212
  • 4
  • 21
  • 35
32
votes
2 answers

Espresso generating FileNotFoundException, when used with Dagger

I have been wrestling with a legacy Android app, attempting to add testing and proper architecture to it. The app has a main LaunchActivity which runs a series of checks on launch. Originally, the activity was using Dagger to, rather poorly, 'inject…
32
votes
1 answer

Dagger 2 scope and subcomponents

I am trying to make my app better and code more maintainable using Dagger2 I caught general idea, but still cannot figure out how scopes are managed by Dagger2 I injected dagger into my project (sounds funny). I created ApplicationComonent…
CROSP
  • 4,499
  • 4
  • 38
  • 89
31
votes
2 answers

RuntimeException with Dagger 2 on Android 7.0 and Samsung devices

On my Google Play console I see quite a lot crash reports since I started to use Dagger 2, but only on Android 7.0 and mainly on Samsung devices, some Huawai and Motorola devices and some rare Xperia devices: java.lang.RuntimeException: at…
Denis Knauer
  • 1,020
  • 9
  • 18
30
votes
2 answers

Dagger singleton vs Kotlin object

To define a singleton, should I use Kotlin object declaration or to make an ordinary Kotlin class and inject it using dagger? In my opinion the first option is definitely easier but there may be a reason to use dagger in this situation that I'm not…
Mahozad
  • 18,032
  • 13
  • 118
  • 133