Chapters

Hide chapters

Dagger by Tutorials

First Edition · Android 11 · Kotlin 1.4 · AS 4.1

14. Multibinding With Maps
Written by Massimo Carli

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you learned how to use multibinding with Set by implementing a simple framework to integrate information from remote endpoints into the Busso App. By refactoring the Information Plugin Framework, you saw how to dynamically add features to Busso in a simple and declarative way.

Figure 14.1 - Information Plugin Framework
Figure 14.1 - Information Plugin Framework

In this chapter, you’ll learn how to use multibinding with Map. In particular, you’ll learn how to:

  • Configure multibinding with Map.
  • Use fundamental type keys with @StringKey, @ClassKey, @IntKey and @LongKey.
  • Create a simple custom key.
  • Use @KeyMap to build complex custom keys.

This is an opportunity to see how Dagger multibinding simplifies the architecture of the Information Plugin Framework.

Using multibinding with Map

In the previous chapter, you learned how to use multibinding with Sets. Set is an unordered data structure that lets you avoid duplicates. This is great but sometimes you might need something different. For instance, in the case of the Information Plugin Framework, a Set doesn’t allow you to decide the order of the information to display on the screen.

In any case, Dagger offers you another option: multibinding with Map. Map is a data structure that allows you map a value to a key.

Note: If you want to know more about data structures in Kotlin and crack interviews for getting your dream job, have a look at Data Structures & Algorithms in Kotlin.

In the following paragraph, you’ll see how to use multibinding with Map<K, V> where the key, K, is one of the following:

  • String,Int and Long
  • Class<T>
  • Custom type

Using a String is the simplest case, so you’ll start with that.

Using @StringKey

For your first example, suppose you want to simplify InformationPluginSpec by removing the property name and giving the plugin a name when you add it to the registry.

Using @ClassKey

Another type of key Dagger gives you is Class<T>. You can use it the same way you used String, but for your next step, you’ll try something more ambitious, instead.

Simplifying the InformationPluginSpec interface

Your first step will be to simplify the InformationPluginSpec interface. Open InformationPluginSpec.kt in plugins.api and change its content like this:

Update InformationPluginRegistry with its implementation

Next, open InformationPluginRegistry in plugins.api and change it to:

Use InformationEndpoint as abstraction for the information plugin endpoints

Look at MyLocationEndpoint and WeatherEndpoint and notice there’s a problem — they don’t share any abstraction. The implementations Retrofit creates for you aren’t InformationEndpoint implementations, and they all define operations with different names and parameters.

Configure multibindings for WhereAmI and Weather

Now it’s time to configure multibinding with Map for the information plugins. Open WhereAmIModule.kt in plugins.whereami.di and change it to this:

Migrate InformationPluginPresenterImpl to the new abstractions

You changed something in the abstraction for the framework, so you also need to change InformationPluginPresenterImpl.kt in plugins.ui. Just replace the start() implementation with the following:

Clean up the unused code

Before building and running, you have some cleanup to do. Delete the following files you don’t need anymore:

Build and run the Busso app

Now you can finally build and run the app and get the expected result in Figure 14.2:

Fonuha 60.8 — Pca Sulpi Igy yabxx ux obhinwok
Juvubi 41.1 — Glu Socsu Ayh yovfw ed onrewquj

Using multibinding with a custom Key

You can usually cover all the use cases you encounter by using @StringKey and @ClassKey. But just in case you need something special, Dagger offers multibinding with Map and a custom type for the key.

Using a simple custom @MapKey

As you read above, @MapKey annotates the class you use as the key when multibinding with Map.

Using a complex custom @MapKey

Your final step is to make the InformationPluginSpec definition redundant and instead, put all the information you need in a custom key to use with multibinding and Map.

Cunaqa 49.8 — Tle Jelyu Iby huzpk as ujjuwhud
Roziwi 54.4 — Cya Dijge Avq yakxh em ihpatnal

Key points

  • Dagger allows you to use multibinding with a Map.
  • When you use a Map for multibinding, you can use keys of the following types: String, Int, Long and KClass.
  • If you need more informative keys, @KeyMap allows you to create custom types, which you can use in a simple or complex way.
  • If you use @KeyMap and an unwrapValue attribute with a default value of true, the type of the key is the type of the unique property of your custom key.
  • A @KeyMap is complex if the value for the unwrapValue attribute is false. In this case, you need to add an auto-value as a dependency in your project.
  • You must use a complex @KeyMap for the key of the Map you use in multibinding.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now