Chapters

Hide chapters

Dagger by Tutorials

First Edition · Android 11 · Kotlin 1.4 · AS 4.1

13. Multibinding
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 sections, you learned how Dagger works by migrating the Busso app from a homemade injection framework based on the ServiceLocator pattern to a fully scoped Dagger app. Great job!

Now, it’s time to use what you’ve learned to add a new feature to the Busso App, which will display messages from different endpoints at the top of the BusStop screen. For instance, you can display information about the weather and any traffic problems at your destination.

You also want to let the user add or remove new endpoints in a simple and declarative way. To do this, you’ll implement a small framework called an information plugin framework. Its high-level architecture looks like this:

Figure 13.1 — Information from a server
Figure 13.1 — Information from a server

Busso connects to different endpoints, gets some information and displays the messages at the top of the BusStop screen. This is a very simple use case that allows you to learn what Dagger multibinding is.

In this chapter’s starter project, you’ll find a version of Busso that already contains the first implementation of the information plugin framework.

In this chapter, you’ll examine the existing code, learning:

  • What multibinding is.
  • How to use multibinding with Set.

Multibinding is a very interesting Dagger feature because it simplifies how you integrate new features by using a plugin pattern, which you’ll learn all about in this chapter.

The information plugin framework

As you read in the introduction, Busso already contains a small framework that lets you fetch information to display at the top of the BusStop Fragment.

You can already see this very simple feature at work. Right now, it displays the coordinates of your current location:

Figure 13.2 — The WhereAmI information
Figure 13.2 — The WhereAmI information

Note: If the architecture of the information plugin framework is already clear to you, just skip to the Introducing Dagger Multibinding section ahead.

When you open the Busso project with Android Studio, you’ll see the source directory structure in Figure 13.3:

Figure 13.3 — The initial source directory structure
Figure 13.3 — The initial source directory structure

In particular, you’ll see a new plugins package. Here are its sub-packages and what they contain:

  • api: The main abstraction of the framework.
  • di: Dagger definitions.
  • impl: Implementation of the main abstraction.
  • model: A very simple class that models the information you’ll receive from the server.
  • ui: The presenter and viewbinder of the framework.
  • whereami: The classes you’ll need to implement the feature that displays the coordinates of your current location, as shown in Figure 13.3. This is the first feature that uses the information plugin framework.

An in-depth description of all the code would take too much space and time, so in this chapter, you’ll focus on the aspects related to dependency injection and, of course, Dagger.

Dagger configuration

The main aspect of this framework is the Dagger configuration you find in the plugins.di package. Open InformationPluginModule.kt in plugins.di and look at its code:

InformationPluginRegistry

When it comes to working with plugins, frameworks, including the information plugin framework, have some important characteristics in common. They all need to:

The WhereAmI information plugin

To implement an information plugin, you need to follow these steps:

Creating an endpoint

Assuming that you have an actual endpoint to call, your first step is to define an implementation of InformationEndpoint, which you find in plugins.api.

Yifiri 42.7 — Bzo AtbuwxipiapUfwkaahj giejyej
Bayexa 12.0 — Pni AftuwvadiavUynquoqd keabqaq

Defining a @Module and creating InformationPluginSpec

Now, you need to tell Dagger how to create the objects it needs for WhereAmI. Open WhereAmIModule.kt in plugins.whereami.di and look at the code:

Registering InformationPluginSpec

This is the last and most important step in the process. Once you define InformationPluginSpec, you need to register it to InformationPluginRegistry to make it available to the framework. At the moment, you do this in Main.kt, which looks like this:

Witedi 70.3 — Pto RtayaIrI odtozmuleun
Tonaca 17.5 — Rmi GtaheAmO ujyoymeneem

Introducing Dagger multibinding

To understand how multibinding helps implement the information plugin framework, take a moment to go over what you’ve done so far. You basically defined:

Using multibinding with Set

Your first step is to use Dagger multibinding with Set to implement the information plugin registration mechanism. This allows you to:

Refactoring InformationPluginRegistry

You want all the InformationPluginSpecs you register to be in a Set<LocationPluginInfo>, so you need to change InformationPluginRegistry accordingly. To do this, open InformationPluginRegistryImpl.kt in plugins.impl and apply the following changes:

Registering WhereIAm

This is the most interesting part: How can you register your plugin declaratively? Open WhereAmIModule.kt in plugins.whereami.di and you’ll see that you’re already telling Dagger how get an InformationPluginSpec for the WhereAmI plugin:

Cleaning up your code

Open Main.kt and remove the excess code so it looks like this:

Using @JvmSuppressWildcards

When you build now, you get the following error:

Implementing @JvmSuppressWildcard

To implement this, open InformationPluginRegistryImpl.kt in plugins.impl and apply this change:

Gigudo 95.5 — Kku McihoOEh wofemoex zcafap veimexi
Fetugu 51.1 — Zbu CkeyoOEx yuvixaum xzogeq tiuheha

Adding a new information service plugin

As you learned in the first chapter of this book, it’s not important how fast you implement a feature but how fast you can change or extend it. To appreciate Dagger’s multibinding capabilities, you’re now going to add a new information plugin.

Defining an endpoint

Create a new package, plugins.weather.endpoint, then add a new file named WeatherEndpoint.kt to it with the following code:

Creating the weather information @Module

Now, you need to create a @Module to tell Dagger about the InformationEndpoint you just created. Create a new package, plugins.weather.di, add a new file named WeatherModule.kt, then give it this code:

Adding a weather plugin

To add the WeatherModule to the modules in ApplicationComponent, open ApplicationComponent.kt in di and apply the following change:

Checking your work

Now, build and run your app and you’ll see something like Figure 13.7:

Zuhovu 06.7 — Vwa Woobwuk nazukois ytemob kaamice
Sejiqe 60.6 — Mte Seenjuj culacoey vduliy guokaga

Lofole 60.7 — Dla Voufdir siwecuix fguxus huulefe
Xumezi 43.1 — Vyu Qiodyez bezusiay rzuges soamofa

More about Multibinding with Set: @ElementsIntoSet

In the previous example, you learned how to use @IntoSet to add a specific binding to a Set that Dagger creates for you and makes available to the dependency graph of the app.

Miroqa 37.9 — Bye iczarqaciel ktunafh im spi Hanxa ahg
Kafafi 29.4 — Qga utdarvecoeb nliqidz od nya Jifso axz

Key points

  • Dagger multibinding allows you to add functionality to your app in an easy and declarative way.
  • You can use multibinding with both Set and Map.
  • @IntoSet allows you to populate a Set when you initialize the dependency graph.
  • Types are fundamental to Dagger. Use @JvmSuppressWildcards to fix a variance problem that occurs when Dagger resolves the objects to inject.
  • @ElementsIntoSet allows you to put more than one object into a multibinding Set.
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