Dagger is a library for dependency injection on JVM-based systems, including Android. Dependency injection is an important technique for building software systems that are maintainable and testable.
You’re likely already doing dependency injection, maybe without even realizing it. Dependency injection is nowhere near as complex as its name implies.
In this book, you’ll update an existing app named Busso to use dependency injection with Dagger and Hilt. The Busso app is a simple app that allows you to find bus stops near you and information about arrival times.
This book will serve you as a central point that holds all the information you need to dive deep into Dagger and Hilt, to apply it to your personal and production level projects.
How to read this book
The book is aimed at Android developers who aren’t familiar with dependency injection and libraries like Dagger and Hilt, or developers who know little about the libraries but haven’t had the chance to use it in real projects.
If you’re completely new to dependency injection, we recommend reading it one chapter at a time, in the order of sections and chapters as available in the table of contents.
If you’re familiar with the fundamentals of dependency injection, you can skip to “Section II: Introducing Dagger” instead, and continue learning about dependency injection with Dagger library.
If you’re already using Dagger in your projects, but want to know more about complex topics, jump over to “Section IV: Advanced Dagger”. You’ll build complex use cases there on a real-world project, learn about multi-binding and modularization.
If you’re already proficient with Dagger library, you can skip to “Section V: Introducing Hilt” and learn about dependency injection with Hilt on Android.
This book is split into five main sections:
Section I: DI Fundamentals
In this section, you’ll get motivated to use a Dependency Injection (DI) library like Dagger by learning all about the problem you need to solve: dependency.
You’ll understand what dependencies are and why you need to control them to create successful apps. You’ll get to know the Busso App, which you’ll work on, and improve throughout this book. It’s a client-server app where the server is implemented using Ktor.
You’ll take your next step toward implementing better apps that are easier to test and modify. You’ll keep the concept of mass of the project in mind. In the process, you’ll learn more about Scope and see how it relates to dependency.
You’ll also use techniques that would work in a world without frameworks like Dagger or Hilt to create a fully testable app.
Section II: Introducing Dagger
In this section, you’ll learn what Dagger is, how it works, and how it slashes the amount of code you need to write by hand when you implement dependency injection in your app.
You’ll learn how to deal with constructor, field and method injection with Dagger, how to simplify the implementation of @Module by using @Binds in cases when you have abstraction and its implementation, and how to use @Singleton to solve a very common problem.
You’ll learn everything you need to know about Dagger @Modules and you’ll experiment with using different fundamental annotations like @Binds, @Provides and BindsOptionalOf as well as useful interfaces like dagger.Lazy and Provider. You’ll also learn what qualifiers are, how to implement them with @Named and how to use custom @Qualifiers.
Section III: Components and Scope management
In this section, you’ll migrate the Busso App from the homemade framework to Dagger. In the process, you’ll learn how to migrate the existing ServiceLocators and Injectors to the equivalent Dagger @Modules and @Components, how to provide existing objects with a customized Builder for the @Component using @Component.Builder, and how to use @Component.Factory as a valid alternative to @Component.Builder.
The first migration will not be optimal — there will still be some fundamental aspects you will improve.
Later, you’ll learn even more about @Components and dependencies. In particular, you’ll learn why @Singleton is not so different from the other @Scopes, why you might need a different approach in managing component dependencies, what type of dependency exist between @Components with @Singleton, @ActivityScope and @FragmentScope scope, etc.
Section IV: Advanced Dagger
In this section, you’ll dive deeper into the advanced features of Dagger like multi-binding. Multibinding is a very interesting feature of Dagger because it simplifies the integration of new features using a plugin pattern you’ll learn in this section.
You’ll implement a simple framework that allows you to integrate new services in the Busso app in a very simple and declarative way. You’ll learn all you need to know about multi-binding with Set and Map.
Section V: Introducing Hilt
In the last section, you’ll learn everything you need to know about Hilt. Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.
Hilt is built on top of the DI library Dagger to benefit from the compile-time correctness, runtime performance, scalability, and Android Studio support that Dagger provides.
You’ll migrate the existing Busso app to use Hilt library.