Search⌘ K
AI Features

Working with Hilt Modules

Explore how to implement Hilt modules for dependency injection beyond constructor injection. Understand how to define modules with @Module and @Provides annotations to create class instances like Retrofit services. Learn to inject these dependencies into your Android components and manage their lifecycle efficiently using Hilt.

Introduction

Hilt can be used for constructor-based dependency injection. However, we can’t use constructor-based DI in every scenarios. For example, if we create an instance of a class from an external library, constructor-based DI can’t be used. There are several other scenarios where this is the case. For these scenarios, the Hilt modules can be used for DI.

Defining a Hilt module

We’ll use an open-source country list API and create its instance using Hilt modules. To refresh our memory, let’s take a look at the ...