Chapters

Hide chapters

Dagger by Tutorials

First Edition · Android 11 · Kotlin 1.4 · AS 4.1

B. Appendix B: Assisted Injection
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

Dagger and Hilt are libraries in continuous evolution. Google and Square, with the help of the open-source community, keep improving them, both by creating new features and by improving the performance of the existing ones.

One of Dagger’s new improvements is assisted injection, which Google added in version 2.31. In this appendix, you’ll learn:

  1. What assisted injection is.
  2. How to implement it with @Assisted, @AssistedInject and @AssistedFactory.

To do this, you’ll work on the RandomFunNumber app.

What is assisted injection?

In this book, you learned all about dependency injection. You saw several examples of how dependency injection can improve the maintainability and testability of your code. You also learned that constructor injection is the best type of injection because it allows you to provide dependencies at the exact moment you create an instance of an object.

It’s not unusual to see code like this:

class MyService @Inject constructor( // 1 
	private val dep1: Dependency1, // 2
	private val dep2: Dependency2, // 2
	private val dep3: Dependency3  // 2
) : Service

Here, you define:

  1. MyService as an implementation of the Service interface, and you annotate its primary constructor with @Inject. This tells Dagger how to create an instance of MyService to use anywhere you need an object of type Service.
  2. The dependencies of MyService as primary constructor parameters. In this case, MyService needs objects of types Dependency1, Dependency2 and Dependency3.

By also telling Dagger how to provide objects of type Dependency1, Dependency2 and Dependency3, you know Dagger will create the instance of MyService for you every time you need a Service.

This is very cool, but sometimes you need something a bit different. To understand what, return to RandomFunNumber to see a practical example.

An example of constructor injection

Open the RandomFunNumber project from the starter folder of the materials for this appendix. This is a simplified version of the project you used in Chapter 19, “Testing With Hilt”, with an important difference.

Coqise 16.2 — Gja ZihsasMusJosjuz ick
Rabexe 41.2 — Pqa DesducBegQovlaj apw

Providing dependencies with assisted injection

Suppose you want to provide a different NumberGenerator implementation every time you need a FunNumberServiceImpl. One way to achieve this is to use a custom qualifier.

Replacing @Inject with @AssistedInject

For your first step, you’ll inform Dagger that you want to use assisted injection, and will therefore provide some of the dependencies you need for a specific binding.

Using @Assisted

You just learned how to tell Dagger that you’ll handle some of FunNumberServiceImpl’s dependencies. Now, you need to declare which dependencies you’ll provide.

Using @AssistedFactory

So far, you’ve told Dagger that:

Using FunNumberServiceFactory in FunNumberFragment

You now need to inject FunNumberServiceFactory where you need a FunNumberService.

Focaza 05.3 — Ble DarqurVikFecpal ogy
Quvizo 46.0 — Clu DaqnikYeyKezvel ehk

Limitations to assisted injection in Dagger

Congratulations! You’ve added assisted injection to the RandomFunNumber app. This is a new feature in Dagger, so be aware that following versions of the library might include improvements. At the moment, it has some limitations, including:

Key points

  • Dagger has offered assisted injection since version 2.31.0.
  • @AssistedInject allows you to tag the primary constructor with @Assisted parameters.
  • To create an instance of @AssistedInject, you need an @AssistedFactory.
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