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
70
votes
5 answers

Dagger 2 static provider methods in kotlin

With the recent versions of dagger 2 one of the improvements made are the possibility of having static provide methods. Simply so: @Provides static A providesA() { return A(); } I was wondering how does one go about doing this in kotlin? I've…
Fred
  • 16,367
  • 6
  • 50
  • 65
69
votes
12 answers

Kotlin Error: Dagger does not support injection into private fields

I use in kotlin activity ViewPager and I want in Kotlin Fragment use the dagger injection. I have got Error: Dagger does not support injection into private fields. In Java Fragment the dagger injection work. Why can i not inject dagger in kotlin…
eurosecom
  • 2,932
  • 4
  • 24
  • 38
69
votes
21 answers

Dagger 2, sometimes on compiling I get "cannot find symbol class DaggerApplicationComponent"

Recent after update Android Studio (2.0.7) (maybe this is the cause) sometimes when building i get that error. Idea is that usually compilation goes well but sometimes I get dagger error. Is possible that is problem in Dagger configuration? Error…
user1542447
  • 761
  • 1
  • 6
  • 9
66
votes
6 answers

Dagger2 dependency - Gradle

I'm trying to add Dagger2 to my project in Android Studio but I can't find proper dependency to paste in build.gradle. Could you help and send me the proper line?
Dabler
  • 877
  • 1
  • 7
  • 11
66
votes
9 answers

How do you override a module/dependency in a unit test with Dagger 2.0?

I have a simple Android activity with a single dependency. I inject the dependency into the activity's onCreate like this: Dagger_HelloComponent.builder() .helloModule(new HelloModule(this)) .build() .initialize(this); In my…
G. Lombard
  • 3,569
  • 1
  • 29
  • 30
63
votes
1 answer

Dagger @Reusable scope vs @Singleton

From the User's Guide: Sometimes you want to limit the number of times an @Inject-constructed class is instantiated or a @Provides method is called, but you don’t need to guarantee that the exact same instance is used during the lifetime of…
philo
  • 3,580
  • 3
  • 29
  • 40
62
votes
7 answers

Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?

I have Google this problem, but the results are not work for me. The detail as following. public final class App extends com.zhixin.wedeep.common.BaseApplication implements androidx.lifecycle.LifecycleOwner { ^ // Expected…
Cyrus
  • 8,995
  • 9
  • 31
  • 58
62
votes
5 answers

Dagger 2 - what is the purpose of a @Singleton annotation class

From the dagger 2 Documentation I noticed that you can have a @Singleton annotated class. What is the purpose of marking a class as @Singleton as I have tried to do this in my code but a singleton object is NOT produced. I'm not clear on what use…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
57
votes
5 answers

Android Unit Tests with Dagger 2

I have an Android app that uses Dagger 2 for dependency injection. I am also using the latest gradle build tools that allow a build variant for unit testing and one for instrumentation tests. I am using java.util.Random in my app, and I want to mock…
Pikaling
  • 8,282
  • 3
  • 27
  • 44
56
votes
6 answers

Dagger 2 injecting Android Application Context

I am using Dagger 2 and have it working however I now need access to the Android Application Context. Its not clear to me how to inject and get access to the context. I have tried to do this as follows: @Module public class MainActivityModule { …
user3521637
  • 1,622
  • 2
  • 18
  • 25
55
votes
6 answers

cannot be provided without an @Inject constructor or from an @Provides-annotated method

I am using Android Dagger2 but I am getting the error below. My AppModule class is: @Module public class AppModule { @Provides public DownloadFilePresenterImp provideDownloadfilePresenterImp(DownloadFileView downloadFileView) { …
ant2009
  • 27,094
  • 154
  • 411
  • 609
55
votes
4 answers

Dagger 2 examples

Dagger 2 is around the corner but the available examples wouldn't even compile right off the box, and the documentation is a copy-paste-replace from Dagger 1. Does anyone have an example of a proper application working on Google's Dagger 2?
MLProgrammer-CiM
  • 17,231
  • 5
  • 42
  • 75
53
votes
3 answers

Problems with singletons when using component dependencies

I'm having a problem in understanding why the following code doesn't work. I have following project structure: @Component(modules = CCModule.class) public interface CComponent { XXX getXXX(); } where @Module public class CCModule { …
Cativail
  • 1,040
  • 1
  • 9
  • 18
52
votes
7 answers

Android lifecycle library ViewModel using dagger 2

I have a ViewModel class just like the one defined in the Connecting ViewModel and repository section of Architecture guide. When I run my app I get a runtime exception. Does anyone know how to get around this? Should I not be injecting the…
TheHebrewHammer
  • 3,018
  • 3
  • 28
  • 45
51
votes
3 answers

Can I just inject super class when use dagger2 for dependency injection?

I use Dagger2 for DI in my android application. I found that I have to write inject method for every class that uses @Inject field. Is there a way that I can just inject the parent class so that I don't have to call inject on every subclass? Take…
Chris.Zou
  • 4,506
  • 6
  • 31
  • 38