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
13
votes
3 answers

Dagger 2 generated test component not recognized

I'm hoping that this is just something I'm doing wrong here. I'm trying to use Dagger 2.0 to inject dependencies for my JUnit tests (not Espresso tests, just pure JUnit). So, I have a 'main' java module and a 'test' java module. In the main…
Alex
  • 1,103
  • 1
  • 11
  • 24
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

Dagger + Kotlin not injecting

I studying Dagger 2 for DI and I just did this code to inject the Retrofit: NetModule.kt @Module class NetModule(val baseUrl: String) { @Provides @Singleton fun provideRetrofit() : Retrofit{ [some logic here] …
Leandro Borges Ferreira
  • 12,422
  • 10
  • 53
  • 73
13
votes
1 answer

Dagger 2 on Android, missing error messages

I'm using Dagger 2 in my Android project and I'm having trouble debugging it. I know that the compilation fails because of an error in my dagger 2 setup (had it before) but it's almost impossible to track it down because I don't get a proper error…
13
votes
1 answer

Unresolved reference dagger 2 + kotlin + android gradle

I'm testing out Dagger 2 with Kotlin in an Android project. I was inspired by the Android Clean Architecture repo. I have two modules in my gradle build, one is "app" and one is "module". Module contains one class call Model. In my app gradle…
wontondon
  • 305
  • 2
  • 9
13
votes
5 answers

Dagger2 component with more than one dependencies

This is what I currently have and it works: @FragmentScope @Component(dependencies = {FacebookComponent.class}, modules = {FragmentFacebookLoginModule.class}) public interface FragmentFacebookLoginComponent { void…
Ralph Bergmann
  • 3,015
  • 4
  • 30
  • 61
13
votes
1 answer

How to inject dependencies into any kind of object with Dagger2?

According to http://konmik.github.io/snorkeling-with-dagger-2.html i could just add inject(Anything anything) into AppComponent.java, but this doesn't work for me, in the articles example: @Singleton @Component(modules = AppModule.class) public…
chrystolin
  • 1,067
  • 1
  • 9
  • 17
12
votes
1 answer

How to generate objects with the same type in Hilt?

@Module @InstallIn(SingletonComponent::class) object NetworkModule { @Singleton @Provides fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit { return Retrofit.Builder() …
pnkj
  • 406
  • 5
  • 17
12
votes
1 answer

Hilt injection in Android Services

I want to inject a class in Service. Lets have a look at the code below: class DeviceUtil @Inject constructor() { ... } @AndroidEntryPoint class LocationUpdateService : Service() { @Inject lateinit var deviceUtil: DeviceUtil …
Amir Raza
  • 2,320
  • 1
  • 23
  • 32
12
votes
2 answers

Why I still get "Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6"

I am developing an Android project with Kotlin and Dagger 2. I have a NetworkModule it is supposed to provide a singleton instance of Retrofit. In which I define all those provider functions. All code snippet below are inside NetworkModule…
user842225
  • 5,445
  • 15
  • 69
  • 119
12
votes
2 answers

How to access context in repository [MVVM]

I am developing an Android application that makes use of the MVVM architecture. My problem is that my repository (that is responsible for fetching JSON from the web) needs access to a context. I've read several suggestions on StackOverflow. So far…
Josip Domazet
  • 2,246
  • 2
  • 14
  • 37
12
votes
1 answer

This annotation is not applicable to target member property without backing field or delegate

Here's my code: @Module class SharedPrefs { @Module companion object { val KEY_COOKIE = "cookie" @JvmStatic @Provides fun putPref(key: String, value: String?) { val prefs =…
Johann
  • 27,536
  • 39
  • 165
  • 279
12
votes
1 answer

Passing data to PageKeyedDataSource

I am using PageKeyedDataSource to make paging by calling an API and using Retrofit. And I am using Dagger 2 to make the dependency injection. @Provides Repository provideRepository(...) { ... } @Provides PageKeyedVideosDataSource…
Soon Santos
  • 2,107
  • 22
  • 43
12
votes
2 answers

Dagger 2.15 - How to inject a dependency in Application class

I am unable to inject a depency in DaggerApplication class The relevant classes are as follows Application class App : DaggerApplication() { @Inject lateinit var mSomeClass : SomeClass // This is always NULL override fun applicationInjector():…
silent_control
  • 592
  • 5
  • 10
12
votes
2 answers

Singleton object becomes null after app is resumed

For my Android project, I need global singleton Cache object to access data about a user through the app. A problem occurs when an app goes into the background and after some time of using other apps I try to open app variables in Cache objects are…