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

Dagger 2 singletons not working

Using Dagger 2, I'm trying to inject a singleton object at multiple locations in a single scope. However, it seems my solution instead creates a new instance each time. In this test project, I have a MainActivity which initializes the DaggerModule.…
jomni
  • 759
  • 8
  • 21
16
votes
1 answer

Dagger2 - null instead of injected object

For making things simple, suppose I want to inject EmailValidator from apache validators into my activity: public class MainActivity extends FragmentActivity { @Inject EmailValidator emailValidator; @Override protected void…
slnowak
  • 1,839
  • 3
  • 23
  • 37
15
votes
1 answer

Hilt viewmodel injection into instrumentation tests

I was searching quite a lot for how to inject ViewModel into tests so I can test it. Lets say the viewmodel have a constructor injection with some business logic interactor. I can inject it into fragments easily but no success in…
Alex
  • 327
  • 3
  • 11
15
votes
6 answers

HILT : lateinit property repository has not been initialized in ViewModel

I am facing this issue in multi module android project with HILT. kotlin.UninitializedPropertyAccessException: lateinit property repository has not been initialized in MyViewModel My modules are App Module Viewmodel module UseCase…
Arun
  • 510
  • 1
  • 4
  • 21
15
votes
3 answers

Should I dispose disposables in a Singleton class ( Android RxJava 2 )

When I subscribe({}) to an Observable in a Singleton class, do I need to call .dispose() method at some point? and if yes, when and where? because a singleton will remain until the App is running. something like this (Kotlin): @Singleton class…
Amir Khorsandi
  • 3,542
  • 1
  • 34
  • 38
15
votes
3 answers

Provide function dependency using Dagger 2

I would like to provide a function as dependency using Dagger 2: @Module class DatabaseModule { @Provides @Singleton fun provideDatabase(application: Application, betaFilter: (BetaFilterable) -> Boolean): Database { return…
15
votes
1 answer

Dagger 2 with Android Studio 3.0 Preview (Canary 2) using annotationProcessor instead of android-apt

"A long time ago in a galaxy far, far away...." Ok, long story short - I decided to give Android Studio 3.0 Preview (Canary 2) a shot and I cannot make it work with Dagger 2 using annotationProcessor instead of android-apt. The error message I get…
15
votes
1 answer

How can Dagger 2 be used to inject using multiple components into the same object

So I have an ApplicationComponent for injecting singletons into my fragments and presenters, but I'm trying to create a component to inject into the same presenter that the AppComponent does. Something along these lines. @Component{modules =…
Stampede10343
  • 844
  • 1
  • 6
  • 21
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
2 answers

Dagger2 generated class is all of a sudden missing from Android Studio

I have been using Dagger2 in Android Studio for months now but today all of sudden it stopped working and is giving me the following error error: cannot find symbol return Dagger_Injector.builder() ^ symbol: variable…
nemo
  • 485
  • 1
  • 3
  • 15
15
votes
1 answer

How to use Dagger's 2 @Named qualifier in Kotlin

Recently I had a problem with @Named qualifier in Kotlin. I thought that changing from this: var boldTypeface: Typeface? = null [Inject] set into this var boldTypeface: Typeface? = null [Inject Named("bold")] set or var boldTypeface: Typeface? =…
Damian Petla
  • 8,963
  • 5
  • 44
  • 47
14
votes
4 answers

setFragmentResult doesn't work onClick listener

hello my problem is the following, I have 2 fragments, one receives with SetFragmentResultListener and another sends with setFragmentResult The problem is that setFragmentResult does not work inside an OnClickListener but it does work…
14
votes
3 answers

Dagger can not find classes generated by other annotation processor

I have written a simple Annotation Processor (just for fun) that will generate some boilerplate code that I have been writing in my previous project. It actually generates a module like following by collecting annotations on Activity…
14
votes
2 answers

Dagger 2 androidx fragment incompatible types

I'm using Dagger 2.21 and when I try to do @Module internal abstract class FragmentModule { @ContributesAndroidInjector internal abstract fun loginFragment() : LoginFragment } and @Singleton @Component(modules =…
14
votes
2 answers

Using dagger-injected objects in attachBaseContext

I need to access my SharedPreferences instance in the attachBaseContext of my activity (so I can set the locale there), but the injected SharedPreferences instance is not available there as the injection is happening in the onCreate method, which is…
Analizer
  • 1,594
  • 16
  • 30