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

Dagger2: no injector factory bound for fragment

I'm trying to convert a project that I'm building to use the dagger-android API for the DI framework, but I'm running into a dead end with an IllegalArgumentException when trying to inject a fragment using @ContributesAnroidInjector. The relevant…
C. Marr
  • 257
  • 1
  • 5
  • 9
17
votes
2 answers

How can I provide activity context with Dagger dependecies?

How can I provide activity context in mainModule class ? Thanks! The code looks like this: @Singleton @Component(modules = {AndroidInjectionModule.class, AppModule.class, ActivityBuilder.class}) public interface AppComponent { …
L.Dan
  • 259
  • 1
  • 2
  • 9
17
votes
1 answer

How do I fix Dagger 2 error '... cannot be provided [...]'?

This is a Canonical Question because this is a common error with Dagger 2. If your question was flagged as a duplicate please read this post carefully and make sure to understand what this error means and why it occured. If this post does not work…
David Medenjak
  • 33,993
  • 14
  • 106
  • 134
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
2 answers

Dagger 2.10 Android subcomponents and builders

Using the new (in 2.10) dagger.android classes, I'm trying to inject things using a Subcomponent that depends on other Modules, and, therefore, has a Builder with setters for those modules. The documentation on…
Petter Måhlén
  • 225
  • 2
  • 9
17
votes
2 answers

Lazy Injection with Dagger 2 on Android

I’m new to Dagger 2. I have this scenario, I wan't to inject an object across my app (in presenters, in api) I do not have a way to provide it initially. It is not created till after authentication at some stage in my app. From the documentation…
17
votes
1 answer

Dagger 2 - modules from different components

I am not quite sure how to solve this with dagger 2. Lets assume we have ApplicationModule that provides us ApplicationContext then we have ApplicationComponent that uses just this one module. Then on top of it we have ActivityModule and…
user3274539
  • 687
  • 2
  • 8
  • 18
16
votes
2 answers

Does Hilt replace Dagger2?

Digging into Google's DI framework Dagger2 to decide for a DI Framewor to use it in an middle sized app, I also noticed Hilt So according to its overview description: Hilt works by code generating your Dagger setup code for you. This takes away most…
Pavel Pipovic
  • 339
  • 1
  • 3
  • 12
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
3 answers

Understanding Dagger 2 @Component.Builder annotation

I'm reading this great tutorial that explains how @Component.Builderworks in Dagger 2. The author did a good job and the article is straight forward, but there still are some confusing I need to clarify: the default implementation of Dagger 2 looks…
Toni Joe
  • 7,715
  • 12
  • 50
  • 69
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
6 answers

How do I request injection in Android Fragments and Services?

I'm following this tutorial to add Dagger 2 to my Android project. After doing setup and creating the modules and components I can add the dependencies in an Activity like this: @Override protected void onCreate(Bundle savedInstanceState) { …
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131
16
votes
1 answer

Dagger 2 Singleton Component Depend On Singleton

I've got a strange problem here, and I'm not quite sure why what I'm doing isn't allowed. I've got the following modules: @Module public final class AppModule { private Context mContext; @Provides @Singleton @AppContext public Context…
tmtrademark
  • 944
  • 6
  • 21
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…