Dagger 2 – two provides method that provide same interface

I recently post the answer to a question like this in this post : Dagger 2 : error while getting a multiple instances of same object with @Named You need to use @Named(“someName”)in your module like this: @Module public class ApplicationModule { private Shape rec; private Shape circle; public ApplicationModule() { rec = new Rectangle(); … Read more

Dagger and Kotlin. Dagger doesn’t generate component classes

You need to have the kapt processor in build.gradle: kapt { generateStubs = true } dependencies { … compile “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version” compile ‘com.google.dagger:dagger:2.0.2’ kapt ‘com.google.dagger:dagger-compiler:2.0.2’ … } This extension will generate the code for dagger. Additionally, for newer gradle versions, you can also apply the plugin in your build.gradle: apply plugin: ‘kotlin-kapt’ dependencies { … compile … Read more

Dagger2 : How to use @Provides and @Binds in same module

@Binds and @ContributesAndroidInjector methods must be abstract, because they don’t have method bodies. That means that they must go on an interface or abstract class. @Provides methods may be static, which means they can go on abstract classes and Java-8-compiled interfaces, but non-static (“instance”) @Provides methods don’t work on abstract classes. This is explicitly listed … Read more

Dagger 2 multibindings with Kotlin

As it described in the Kotlin reference To make Kotlin APIs work in Java we generate Box<Super> as Box<? extends Super> for covariantly defined Box (or Foo<? super Bar> for contravariantly defined Foo) when it appears as a parameter. You can use @JvmSuppressWildcards for avoiding it, just as following: @Inject lateinit var foo: Set<@JvmSuppressWildcards Foo>

Dagger2 Custom Scopes : How do custom-scopes (@ActivityScope) actually work?

Actually there is no magic. Custom scope annotations are just annotations. They can have any name. First function of scopes is a way to tell Dagger compiler which scopes are allowed within scoped component. That’s why using @ActivityScope dependency in non-@ActivityScope component will fire a compilation error. In fact components can declare many scopes (e.g. … Read more

Dagger 2 error: dependency “cannot be provided without an @Inject constructor” while it actually annotated with @Inject

I got this same error because I forgot to expose the objects provided by the modules in the parent component to the other components that are depend on it. Parent component example: @Singleton @Component(modules = {AppModule.class}) public interface AppComponent { AppPref exposeAppPref(); /* my issue was caused by forgot this line, the method name doesn’t … Read more