Dagger @Reusable scope vs @Singleton

Use @Singleton if you rely on singleton behavior and guarantees. Use @Reusable if an object would only be a @Singleton for performance reasons. @Reusable bindings have much more in common with unscoped bindings than @Singleton bindings: You’re telling Dagger that you’d be fine creating a brand-new object, but if there’s a convenient object already created … Read more

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

Probably this is more a workaround that proper support for test module overriding, but it allows to override production modules with test one. The code snippets below shows simple case when you have just one component and one module, but this should work for any scenario. It requires a lot of boilerplate and code repetition … Read more

What is the use case for @Binds vs @Provides annotation in Dagger2

@Binds can be perfectly equivalent to a @Provides-annotated method like this: @Provides public HomePresenter provideHomePresenter() { return new HomePresenterImp(); } …though you’d probably prefer a variant that takes HomePresenterImp as a method parameter, which lets Dagger instantiate HomePresenterImp (assuming it has an @Inject constructor) including passing any dependencies it needs. You can also make this … Read more

Dagger2 dependency – Gradle

Installing Dagger 2 on Android Studio 2 // Application build.gradle dependencies { compile ‘com.google.dagger:dagger:2.4’ annotationProcessor “com.google.dagger:dagger-compiler:2.4” } Maven Repositories: Find the latest versions of the above dependencies in the Maven Repository: dagger dagger-compiler Notes: Android Studio < 2.2 Older versions of Android Studio need android-apt for annotation processing. // Project build.gradle buildscript { dependencies { … Read more

Kotlin Error: Dagger does not support injection into private fields

You have mistake here: @Inject internal var mSharedPreferences: SharedPreferences? = null This looks like you added @Inject annotation to the KotlinFragment class Please change it to this and it will work: var mSharedPreferences: SharedPreferences? = null @Inject set Here is the link to the documentation: https://kotlinlang.org/docs/reference/annotations.html

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

It’s seems that it have something to do with incremental compilation added in Gradle 2.10 I managed to fix it adding the following command to gradle: -Pandroid.incrementalJavaCompile=false You can add it in Android Studio in: File | Settings | Build, Execution, Deployment | Compiler adding it as a Command line option. edit as of 2.0.0-beta3 … Read more

Dagger 2 static provider methods in kotlin

Although I think zsmb13‘s solution is better, I found another solution which works @Module class AModule { @Module companion object { @JvmStatic @Provides fun providesA(): A = A() } // add other non-static provides here } However, note that there will be two generated classes: AModule_ProvidesAFactory and AModule_Companion_ProvidesAFactory rather than the one AModule_ProvidesAFactory class for … Read more

Dagger: IllegalArgumentException: No injector factory bound for Class

I believe you have forgot to put @ContributesAndroidInjector annotation: @Module public abstract class ActivityModule { @ContributesAndroidInjector abstract ProductListActivity contributeProductListActivity(); @ContributesAndroidInjector abstract ProductDetailsActivity contributeProductDetailsActivity(); } And include ViewModelModule within AppModule: @Module(includes = ViewModelModule.class) class AppModule { … } See this code that you have wrote: @Provides @Singleton ProductListRepository provideProductListRepository(ProductListRepository repository) { return repository; } What do … Read more