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

Use Dagger modules without the "injects" directive

I am trying to make Dagger work without the "injects" directive inside the @Module annotation. I am basing my test project on the Android Simple Dagger example This is the part that is giving me problems: @Module( injects = HomeActivity.class, …
spCoder
  • 318
  • 2
  • 9
16
votes
2 answers

Inject only certain params in constructor

I have the presenter class PhonePresenter @Inject constructor( private val preference: DataPreference, private val ioScheduler: Scheduler = Schedulers.io()) ioScheduler is a default parameter. I want to inject only preference:…
vmtrue
  • 1,724
  • 18
  • 36
16
votes
6 answers

Cannot create an instance of custom ViewModel

I am using dagger2 library. whenever I am trying to run my project is says not able to create instance of view model class. main activity where I am trying to create an instance ((MovieApplication)…
16
votes
3 answers

Dagger 2 issue overriding single provides annotated method from a module in a library which app uses

GitHub Project Link I have made a project on GitHub which is a model of the dagger 2 architecture of my projects actual architecture. This question will be based off of the GitHub project. I have provided many code snippets in this question,…
qazimusab
  • 1,031
  • 1
  • 7
  • 14
16
votes
1 answer

Dagger 2 on Android: inject same dependency in Activity and retained Fragment

I have objects of classes F1 and F2 that I want to inject in a retained Fragment. I also have an object of class A that depends on Activity, and I want it to be injected in that Activity and in a retained Fragment attached to that Activity's…
16
votes
2 answers

Better Dagger dependency injection based on build type and flavor?

I am using aproach to provide modules to Dagger described in answer of this question Android Customize Workflow for Product Flavors Our approach is a bit different - we have Modules in debug build type in src/debug/java and in release build type in…
Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101
16
votes
1 answer

Extending ObjectGraph with overriding module

I'm playing with Dagger right now and apparently some of the features don't work for me. I'm actually trying to extend my ObjectGraph (via ObjectGraph.plus()) with mock module which overrides one of the real modules in already created graph. But…
Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
15
votes
2 answers

Dagger 2 on Android @Singleton annotated class not being injected

I am currently trying to integrate Dagger 2 into an Android application. My project setup is as follows: library app (depends on library) In my library project I defined a class that I'll later inject into other classes that need it (Activities…
AgentKnopf
  • 4,295
  • 7
  • 45
  • 81
15
votes
1 answer

Dagger example built through eclipse fails with 'Please ensure that code generation was run for this module.'

I'm using Dagger for dependency injection in Android, using Eclipse to build. I've cloned android-activity-graphs to use as an example. I've set up my environment according to staxgr from https://github.com/square/dagger/issues/126 These are my…
sudocoder
  • 1,164
  • 1
  • 13
  • 26
15
votes
1 answer

Injecting generic class with Dagger

I have an abstract base class in my project public abstract class BaseActivity> implements IBaseView{ Into which I try to inject a generic class like this: @Inject protected T mPresenter; Is there any…
Malthan
  • 7,005
  • 3
  • 20
  • 22
14
votes
1 answer

How to work with DaggerApplication and DaggerAppCompatActivity

I'm trying to get my head around the new Dagger2 APIs and support for Android. I'm using Dagger2 version 2.15: implementation 'com.google.dagger:dagger:2.15' implementation 'com.google.dagger:dagger-android:2.15' implementation…
Reza Bigdeli
  • 1,142
  • 1
  • 12
  • 25
14
votes
2 answers

java.lang.IllegalArgumentException: No injector factory bound for Class

The error I have is following: Caused by: java.lang.IllegalArgumentException: No injector factory bound for Class. Injector factories were bound for supertypes of MyActivity_: [MyActivity]. Did you mean to bind an injector factory for the…
aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
14
votes
1 answer

Dagger 2.11 - A binding with matching key exists in component

I have a small scenario where I have this following structure where I'm trying to inject fragment manager in baseActivity Fragment but for some reason I'm running out of luck :( @Singleton @Component(modules = { AppModule.class, …
user3354265
  • 777
  • 1
  • 10
  • 26
14
votes
2 answers

IncompleteAnnotationException: dagger.Provides missing element type

When I add the following code an error comes: @Singleton @Provides fun provideGson() : Gson { return Gson() } I really don't know why.
wall
  • 161
  • 1
  • 4
14
votes
1 answer

Context cannot be provided without an @Provides-annotated method, but it is?

I have the following simple module: @Module public class ApplicationModule { private CustomApplication customApplication; public ApplicationModule(CustomApplication customApplication) { this.customApplication = customApplication; …
Eduardo Naveda
  • 1,880
  • 3
  • 15
  • 30