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

Dagger 2 module dependency graph: bound multiple times

I'm new to Dagger 2, trying to port a (quite) complex application to it. We have several dependencies on 'common' libraries (shared with other projects). Those 'common' libraries sometimes depend on other 'common' libraries. Each library exposes a…
Jens Geiregat
  • 129
  • 1
  • 7
10
votes
1 answer

The generated class for Component of Dagger 2 is not found in compileTestJava of Gradle's Java Plugin

Well, I'm migrating my Android project to use the Clean Architecure: https://github.com/android10/Android-CleanArchitecture This means that part of my code is within the domain module (pure Java, no dependency with Android). For this project, I'm…
Fernando Camargo
  • 3,085
  • 4
  • 30
  • 49
10
votes
1 answer

Creating test dependencies when using Dagger2

While reading the docs for dagger 2 I cannot find an easy way to provide a dependency when building an app for testing. The only clue I've found is this: Dagger 2 doesn't support overrides. Modules that override for simple testing fakes can…
Julian Suarez
  • 4,499
  • 4
  • 24
  • 40
10
votes
1 answer

Dagger - android.app.IntentService with @Inject

Is it possible to Inject object exposed through dagger into android.app.IntentService? If so, how I can do that? I want to have something like that. public class SomeService extends android.app.IntentService { @Inject Synchronizer…
lstrzelecki
  • 671
  • 7
  • 13
10
votes
3 answers

Robolectric not using test application

According to this link I can create a test application which Robolectric will automatically start using in tests. I cannot get this to work. I am using Dagger for dependency injection and have created injection wrapper classes for Activity and…
user3301081
10
votes
1 answer

Dagger can't find injectable members on a module

I'm using Dagger for dependency injection in an Android project, and can compile and build the app fine. The object graph appears to be correct and working, but when I add dagger-compiler as a dependency to get errors at compile time, it reports…
SeanPONeil
  • 3,901
  • 4
  • 29
  • 42
9
votes
1 answer

Duplicate bindings with TestInstallIn for instrumented test

I'm using Hilt to inject some dependencies like so: @Module @InstallIn(SingletonComponent::class) object SomethingModule { @Provides fun provideSomething(@ApplicationContext context: Context): Something { //return RealSomething …
alisonthemonster
  • 1,095
  • 1
  • 11
  • 28
9
votes
5 answers

@InstallIn can only be used on @Module or @EntryPoint classes

I am new at the dependency injection on Android. I am using Dagger-Hilt and in AppModule class that I generated for the DB providers I got an error and the project doesn't compile. The error is @InstallIn can only be used on @Module or @EntryPoint…
enesigneci
  • 301
  • 1
  • 4
  • 13
9
votes
0 answers

Dagger2 fragment injection

Trying to learn Dagger2 and using Dagger2 2.14.1. I can't get past: no suitable method found for inject(SettingsFragment) SettingsFragment is: public class SettingsFragment extends PreferenceFragmentCompat implements…
Eric
  • 781
  • 1
  • 10
  • 18
9
votes
2 answers

dagger-android custom scopes

I'm confused about scoped dependencies in Dagger using dagger-android. Using @ContributesAndroidInjetor I have a code something like the following: @Module public abstract class ActivityBindingModule { @ContributesAndroidInjector(modules =…
9
votes
2 answers

Room Empty Db after application restart

I am trying to use Room for my new app. But whenever I restart my app, I am unable to retrieve the old data. It looks like my app is creating a new instance of the database every time, but I am not sure why. Here are my classes based on MVP pattern.…
fR0DDY
  • 795
  • 1
  • 8
  • 20
9
votes
2 answers

Kapt, Kotlin, Dagger2 Error while annotation processing

I'm getting this gradle error after adding com.google.dagger:dagger-android-support dependency in a project with kotlin. This is part of my build gradle build.gradle apply plugin: 'com.android.application' apply plugin: 'kotlin-android' …
rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56
9
votes
4 answers

Dagger2 where inject @Named @Provides in dependent module?

I use dagger2 demo by https://guides.codepath.com/android/Dependency-Injection-with-Dagger-2. I want to use cached and non_cached retrofit call. I create in NetModule.java @Provides @Named("cached") @Singleton OkHttpClient provideOkHttpClient(Cache…
eurosecom
  • 2,932
  • 4
  • 24
  • 38
9
votes
1 answer

MockWebServer with Retrofit and Dagger

I tried to implement instrumentation test for my android project. In my main project, I used dagger and retrofit and I provide the retrofit from my Module. This is my Module that provide retrofit: @Module public class NetModule { @Provides …
Tobi
  • 171
  • 8
9
votes
2 answers

Handling dagger component on orientation change

Assuming what have been said here, it's the developer's responsibility to keep the component instance in order to implement their own scope logic (since a scoped method will return the same instance for a given component). What's the clean way to…
Rafi Panoyan
  • 822
  • 9
  • 21