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
14
votes
2 answers

Dagger code giving NoClassDefFoundError when Espresso Tests are run, normal run works fine

Started exploring Espresso 2.0, but seem to have run into a hiccup. I cannot get the tests to successfully run against any project which includes Dagger. When I run the tests I get the following Exception (entire stacktrace at the…
pturner
  • 686
  • 5
  • 14
14
votes
1 answer

Module depending on another module in Dagger

I'm trying to use Dagger to do Dependency Injection on an app that I'm building, and running into trouble constructing proper DAGs when I have one package's Module depending on values provided by the Injector (presumably provided by another…
pablo.meier
  • 2,339
  • 4
  • 21
  • 29
14
votes
1 answer

How to inject into static classes using Dagger?

I want to introduce dependency injection through Dagger to a project. The following code acts as an example to describe the problem of injection into static classes. The static method setupTextView() is called from multiple classes: public abstract…
JJD
  • 50,076
  • 60
  • 203
  • 339
13
votes
2 answers

What is difference between ActivityRetainedComponent @ActivityRetainedScope and ActivityComponent @ActivityScoped in dagger hilt android

What is difference between ActivityRetainedComponent @ActivityRetainedScope and ActivityComponent @ActivityScoped in dagger hilt android
13
votes
1 answer

Dagger Hilt not injecting on tests

I'm trying to run a test using Dagger Hilt with Robolectric: @RunWith(RobolectricTestRunner::class) @UninstallModules(LevelModule::class, AppModule::class) @Config(sdk = [16, 28], application =…
Lucas Lima
  • 1,465
  • 2
  • 16
  • 28
13
votes
2 answers

Dagger2 Error: Module Must Be Set

I was trying to do SubScoping in Dagger2. However, I am not able to figure out this compilation error:-> ...MyApplicationModule must be set which happens in my LogInFragment. If someone will try to throw some light on this error. I would really be…
Kamil Kamili
  • 1,757
  • 5
  • 24
  • 39
13
votes
2 answers

NPE when getting Robolectric ShadowApplication with Volley and Dagger

In my Android application I have set up Volley. Robolectric.application is initialized and all other tests runs smoothly. I get this error when trying to get mocked HTTP response. This is my test: @RunWith(MyRobolectricTestRunner.class) public class…
Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101
12
votes
4 answers

Hilt creating different instances of view model inside same activity

After recently migrating from Dagger to Hilt I started observing very strange behavior with respect to ViewModels. Below is the code snippet: @HiltAndroidApp class AndroidApplication : Application() {} @Singleton class HomeViewModel…
ritesh4302
  • 336
  • 1
  • 5
  • 11
12
votes
3 answers

@Binds methods must have only one parameter whose type is assignable to the return type

I am migrating to the new dagger android 2.11 All set up based on the Google blueprint :MVP-Dagger.but I am having this error : Error:(22, 57) error: @Binds methods must have only one parameter whose type is assignable to the return type Here in…
Mohamed ALOUANE
  • 5,349
  • 6
  • 29
  • 60
12
votes
2 answers

Android Dagger updating values outside module

I am using dagger for DI in an android application. I can provide variables to other classes outside the modules, but how to update them? Example: Login I need a AuthenticationModule which can Provide a User. When the app starts, the User is not…
Martin Golpashin
  • 1,032
  • 9
  • 28
12
votes
1 answer

can Dagger be used to perform injection on a Content Provider?

I have recently been integrating Dagger into a project that uses ContentProviders. I create a single ObjectGraph instance in my custom Application object, and basically in each managed component: Activity, Fragment, Service ... Then, I call…
homerman
  • 3,369
  • 1
  • 16
  • 32
12
votes
5 answers

Inject database in a ContentProvider with dagger

I want to inject a singleton SqliteOpenHelper in a ContentProvider. However, it seems that the ContentProvider instance is being built before the Application instance is being created (getApplicationContext() returns null). When can I inject the…
Catalin Morosan
  • 7,897
  • 11
  • 53
  • 73
12
votes
1 answer

Dagger with Android: How do I inject the current context?

Back when I used RoboGuice, I was able to constructor inject Context into of my classes and RoboGuice would pick the appropriate Context (injects in an Activity would have the Activity context, injects in Application would have the current…
Paul
  • 4,422
  • 5
  • 29
  • 55
12
votes
3 answers

Android functional testing with Dagger

I’m trying to test an Activity with Mockito & Dagger. I have been able to inject dependencies to Activity in my application but when testing the Activity, I have not been able to inject mock to the Activity. Should I inject Activity to test or let…
11
votes
2 answers

Android Hilt - multi module project cast error

I am trying to use hilt for my project which contains dynamic features. I am facing an error that I cannot fully understand why. I get an error like this: java.lang.ClassCastException:…
Pehr Sibusiso
  • 862
  • 1
  • 14
  • 27