Learning Android Development

Dagger 2 Multibindings Illustrated

Picture by Masaaki Komori on Unsplash

Dagger 2 has a multibindings capability with official reference as below. It took me quite a while to understand and get working example up.

So decided to make easier illustration on it, and make all the examples in github (note: the code is in Java, to easier match the original Reference document. If you like to see the Kotlin code version, refer here).

Dagger 2 Multibindings is simply a feature that package dependencies from different modules into either a SET or MAP of dependencies. It could then be injected all together to the target object.

Set multibindings

We use @IntoSet to bind a value into the Set Collection

@Module
class MyModuleA {
@Provides
@IntoSet
static String provideOneString(DepA depA, DepB depB) {
return "ABC";
}
}

--

--