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
19
votes
5 answers

How to give same instance of ViewModel to both the Parent and Child fragment

There are two Fragments: ParentFragment and ChildFragment. ChildFragment has been added to a view of the ParentFragment. Now using Dagger2 for Android has the ParentFragmentModule with a method: @Provides fun provideViewModel(fragment:…
19
votes
8 answers

Error: cannot find symbol variable DaggerAppComponent

While trying to integrate latest Dagger 2 version, I am facing problem of Dagger auto generation. Dagger is not auto generating DaggerAppComponent in spite of several Rebuilds and Make Module App process. Application class: public class…
reactivedroid
  • 752
  • 1
  • 7
  • 14
19
votes
2 answers

App crashes on start caused by NPE in android.content.Context.getString

We have a really weird crash, which points to the system classes. It appears on application start. Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.android/com.myapp.android.main.BaseMainActivity}: …
Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61
19
votes
3 answers

How to configure dagger + gradle

I have a project and migrating to gradle dependency, but I find myself with an issue trying to setup dagger with gradle, the first time I compile it work perfectly (or if I clean) but if I try it twice then it gives me error like: Error:(13, 14)…
Necronet
  • 6,704
  • 9
  • 49
  • 89
19
votes
1 answer

Singletons in Dagger 1.x

When using Dagger, I found that I'm getting multiple instances of a singleton when I inject it wherever I need it. I've annotated the class and the provides method with @Singleton. Can anyone think of why this is happening? Edit: If it helps, I have…
Raghuveer
  • 1,737
  • 20
  • 27
19
votes
4 answers

Android Dagger Dependency Injection fails on private Fields

I'm new to dagger (though I have experience with DI from working on Java EE WebApps using Weld). What I'm trying to do is to inject a dependency into a class. The field is private. Dagger then throws an exception stating it can't inject into a…
salgmachine
  • 519
  • 1
  • 3
  • 14
18
votes
5 answers

Injecting Androidx Fragments using Dagger 2

I want to inject my Androidx fragments using dagger 2. In my activity I have: public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject Repository repository; @Inject…
mahdi jamshidian
  • 195
  • 1
  • 1
  • 10
18
votes
4 answers

Does dagger 2 has any benefit for performance?

I'm a newbie in dagger 2 and dependence injection in android. I'm hearing that a lot of android developers use dagger 2. I understand that It manages dependencies between classes and with it we will not use anymore the keyword "new" but I want to…
nimmp
  • 329
  • 2
  • 9
18
votes
1 answer

Understanding Android Architecture Components example GithubBrowserSample: ViewModelModule, ViewModel parameters

One of the most up to date samples covering Android Architecture Components is GithubBrowserSample provided by Google. I reviewed the code and a few questions arose: I have noticed that ViewModelModule is included in AppModule. It means that all…
18
votes
1 answer

dagger android support proguard rules

I'm using Dagger2 android-support library with Proguard but i can't compile my project because of this error : Warning:dagger.android.AndroidInjector: can't find referenced class…
Mahdi Nouri
  • 1,391
  • 14
  • 29
17
votes
3 answers

Using Dagger2 with Lombok

Has anyone used Lombok 1.16 with Dagger2? My current code looks like: @AllArgsConstructor(onConstructor = @__(@Inject)) public class JuiceMaker { private final Apple apple; The error is: JuiceMaker cannot be provided without an @Inject…
Jessica
  • 721
  • 1
  • 6
  • 13
17
votes
2 answers

What is actual usage of "HasFragmentInjector" in Dagger 2

I have implemented dagger2 v2.2 previously but as now they have added dagger.android part also. so I am creating sample project with that. I am aware about old methodology of @Provide and @Modules and @Components etc annotations but from Dagger 2.8+…
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
17
votes
4 answers

Square Dagger IllegalStateException: Module adapter for class MyApplicationModule could not be loaded

I am using eclipse and Dagger 1.2.2 for my Android project. I managed to implement a test application with Dagger. But with my "real" application I get: java.lang.RuntimeException: Unable to create application app.MyApplication:…
user2368140
17
votes
4 answers

Getting Dagger to inject mock objects when doing Espresso functional testing for Android

I've recently gone whole-hog with Dagger because the concept of DI makes complete sense. One of the nicer "by-products" of DI (as Jake Wharton put in one of his presentations) is easier testability. So now I'm basically using Espresso to do some…
17
votes
3 answers

Gradle dagger lint: ignore by package

I've created an Android project using Gradle. I've added a Dagger library: dependencies { compile 'com.squareup.dagger:dagger-compiler:1.2.1' compile 'com.squareup.dagger:dagger:1.2.1' } After trying to compile the project I get a build…
midnight
  • 3,420
  • 3
  • 36
  • 58