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
22
votes
7 answers

dagger android support to androidx.fragment

How to inject a fragment from the package androidx.fragment.app.Fragment ? I'm using the dagger-android framework to inject my dependencies in my code. As the documentation says I do this to inject my fragment @Override public void onAttach(Activity…
Irving Dev
  • 389
  • 2
  • 8
22
votes
2 answers

Building an Android Instant App with Application Component from Dagger

I'm currently experimenting with InstantApps and would like to include dagger into my project. I'm facing an issue setting up an application AppComponent. My application component includes all the feature dagger modules of my app. I basically…
Quentin Colle
  • 246
  • 2
  • 8
22
votes
2 answers

How to inject primitive variables in Kotlin?

I am using Dagger2 for DI in my Android app, and using this code for injecting classes into my Activity is fine: @field:[Inject ApplicationContext] lateinit var context: Context but, lateinit modifier is not allowed on primitive type properties in…
22
votes
3 answers

Singleton component cannot depend on scoped components

While working on Android application using Dagger2 for dependency injects while defining Dagger component I'm getting this error Error:(13, 1) error: This @Singleton component cannot depend on scoped components: @Singleton…
Sushant Kumar
  • 243
  • 3
  • 8
22
votes
3 answers

Dagger2 does not generate "Dagger_" files

Im using dagger 2 since a while, but today trying to compile got this error: Error:(13, 31) error: cannot find symbol class Dagger_GlobalComponent Error:(38, 21) error: cannot find symbol variable Dagger_GlobalComponent So here is the code: public…
FireZenk
  • 1,056
  • 1
  • 16
  • 28
22
votes
2 answers

How do I inject into a Servlet with Dagger 2?

I asked (and answered) the same question for Dagger 1 here. How would I do something similar for Dagger 2, now that ObjectGraph.inject no longer exists. This question could be generalized to: How do you do members injection if the object must be…
Ben
  • 4,785
  • 3
  • 27
  • 39
21
votes
1 answer

Unit test Dagger Fragments with FragmentScenario

I am trying to test fragment interactions using the Android Jetpack Navigation Component and the fragment-testing library. My app is using java + Dagger2 as DI. To test the navigation I have created a JUnit test: @Test public void…
21
votes
1 answer

How to access SharedPreferences in a ViewModel?

I am developing an Android application using MVVM. I need to access SharedPreferences in a ViewModel, but I do not know how to do it. I know that it is possible to access a context when inheriting AndroidViewModel, but I want to know if it is…
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
21
votes
2 answers

Issue Setting up Dagger 2.x for Android on AndroidStudio3.0 Canary4

I get following errors while setting up dagger 2.x in Android Studio 3.0 Canary 4 Error:(71, 20) Failed to resolve: com.google.dagger:dagger:2.x Error:(73, 20) Failed to resolve: com.google.dagger:dagger-android:2.x Error:(74, 20) Failed to resolve:…
21
votes
1 answer

Binding Into Map With KClass Type

I am trying to bind subclasses of ViewModel into a map by their KClass types: @Module abstract class ViewModelModule { @Binds @IntoMap @ViewModelKey(MyViewModel::class) abstract fun bindsMyViewModel(viewModel: MyViewModel): ViewModel …
Bryan
  • 14,756
  • 10
  • 70
  • 125
21
votes
4 answers

Dagger 2 error: android.content.Context cannot be provided without an @Provides-annotated method

I am working on implementing dagger 2 in my project. For this, i have written below lines of code: @Inject VideoControllerView mediaController; @Module public class PlayerModule { @Provides VideoControllerView…
Naseeb Sheoran
  • 423
  • 2
  • 8
  • 21
21
votes
7 answers

kapt Build Fails With Dagger Android Processor

I am attempting to include the Dagger Android Processor (documented here) in my project with the Kotlin Annotation Processing Tool (kapt). I have included the proper dependencies in my build.gradle file: apply plugin: 'com.android.application' apply…
Bryan
  • 14,756
  • 10
  • 70
  • 125
21
votes
3 answers

Refreshing Dagger 2 instance from Android Application class

I have a set of @Singleton and @Provides method in my module class for the purpose of creating Singleton instance throughout the application. Everything works fine except few bottle neck scenarios like as follows: STEP 1. I am creating a Retrofit…
Chandru
  • 5,954
  • 11
  • 45
  • 85
21
votes
2 answers

Dagger 2: how to change provided dependencies at runtime

In order to learn Dagger 2 i decided to rewrite my application but I'm stuck with finding the proper solution for the following problem. For the purpose of this example let's assume we have an interface called Mode: public interface Mode { …
Marcin
  • 893
  • 7
  • 19
21
votes
2 answers

Dagger 2 injecting multiple instances of same object type

Background I am converting my app to MVP architecture and found Dagger 2 to be useful to inject dependencies when needed. My app needs to communicate with two web apis (my own and a third party api). There may be times where requests to my own api…
Much Overflow
  • 3,142
  • 1
  • 23
  • 40