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
19
votes
1 answer

Dagger 2: What does @Module(includes =) do?

I'm working on a project and I'm trying to make it as modular as possible. I'm trying to use the @Module(includes = {}) annotation to achieve my goals, and it's not working too well. I have a gradle module for all my Java code and within that each…
MattBoothDev
  • 1,294
  • 2
  • 15
  • 25
19
votes
2 answers

Is this a correct way to use Dagger 2 for Android app in unit test to override dependencies with mocks/fakes?

For 'regular' Java project overriding the dependencies in the unit tests with mock/fake ones is easy. You have to simply build your Dagger component and give it to the 'main' class that drives you application. For Android things are not that simple…
Ognyan
  • 13,452
  • 5
  • 64
  • 82
18
votes
4 answers

Android error "android.app.Application does not implement dagger.android.HasActivityInjector"

I'm trying to run my app and every time I click on a button and it calls a specific Activity (ExtendedActivity) I get the following error : Process: com.converter.android.dailycope, PID: 28573 java.lang.RuntimeException: Unable to…
porthfind
  • 1,581
  • 3
  • 17
  • 30
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
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
18
votes
2 answers

Kotlin and Dagger: Can I use @Inject to an object still make it nullable/optional?

I need something that can make my class nullable/optional because of the runtime errors that is happening on some devices. Is this possible? class MyFragment extends Fragment { @Inject var presenter: MyPresenter? = null // Other codes…
Paula Kristin
  • 813
  • 2
  • 11
  • 27
18
votes
2 answers

Beside testing, why do we need Dagger 2?

At this point, my understanding of Dependency Injection (DI) is only from this article. I'm interested to try, but I just need to clarify some things: Many refer DI as a tool to reduce boilerplate code. But according to that tutorial, the setup of…
Hendra Anggrian
  • 5,780
  • 13
  • 57
  • 97
18
votes
2 answers

Dagger 2, Library modules and @Singleton

I'm trying to use Dagger 2 in an Android Project that has several Android Library modules and I'd like to be able to provide singleton scoped instances of classes from these modules. Currently I'm able to define Components inside the library modules…
Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69
18
votes
2 answers

Generating a MembersInjector for [Class]. Prefer to run the dagger processor over that class instead

I have these warnings and I don't understand what they mean. Can someone point me to something? For classes I inject into (where there is a component.inject(this) statement) Note: Generating a MembersInjector for [class] Prefer to run the dagger…
znat
  • 13,144
  • 17
  • 71
  • 106
18
votes
1 answer

Can dagger injection done in static method?

I have this network module . I want to inject Network module in static method of ErrorUtils. @Module public class NetworkModule { private final String END_POINT = "https://www.myurl.com/"; @Provides @Singleton public OkHttpClient…
c__c
  • 1,574
  • 1
  • 19
  • 39
18
votes
1 answer

Why scope annotations have RUNTIME retention in Dagger 2?

Dagger 2 has compile-time validation, so why do the scope annotations have RUNTIME retention? Why are they needed at runtime?
netimen
  • 4,199
  • 6
  • 41
  • 65
18
votes
3 answers

dagger 2 circular dependency

In a project I am working on I have 2 classes that are highly dependent on one another: @Singleton class WorkExecutor { @Inject Provider services; ... public void execute(Work w){ w.execute(services.get()); …
bennyl
  • 2,886
  • 2
  • 29
  • 43
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

Dagger2 - How to conditionally choose modules at runtime

I have a BIG Android app that needs to run different code for depending on the OS version, the manufacturer, and many other things. This app however needs to be a single APK. It needs to be smart enough at runtime to determine which code to use.…
Paul Nogas
  • 309
  • 3
  • 16