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 in the Dagger FAQ, under the sections “Why can’t @Binds and instance @Provides methods go in the same module?” and “What do I do instead?”.

If your @Provides method doesn’t use instance state, you can mark it static, and it can go onto an abstract class adjacent to your @Binds methods. If not, consider putting the bindings like @Binds and @ContributesAndroidInjector into a separate class–possibly a static nested class–and including that using the includes attribute on Dagger’s @Module annotation.

Leave a Comment