-
-
Notifications
You must be signed in to change notification settings - Fork 512
Dependency Injection
Lawnicons uses Hilt for dependency injection (DI). Hilt is a DI library built on top of Dagger that simplifies Dagger setup and usage in Android applications. It helps us write more modular, testable, and maintainable code by managing how objects (dependencies) are created and provided to other objects.
- Reduced Boilerplate: Hilt significantly cuts down on the manual setup Dagger often requires.
-
Lifecycle Management: Hilt automatically manages the lifecycle of injected dependencies, tying them to standard Android component lifecycles (like
Application,Activity,ViewModel). - Standardization: Provides a standard way to do DI in Android apps.
- Testability: Makes it easier to replace real dependencies with test doubles (fakes or mocks) in your tests.
You'll encounter these Hilt annotations throughout the codebase:
-
@HiltAndroidApp:- Applied to the
LawniconsApplication.ktclass. - This triggers Hilt's code generation, including a base class for your application that supports DI.
LawniconsApplication.class // Hilt needs to be initialized in an Application class
- Applied to the
-
@AndroidEntryPoint:- Used on Android components like
Activity(for example,MainActivity.kt). - It tells Hilt to prepare these components for injection. Hilt can then provide dependencies to these components.
- Used on Android components like
-
@HiltViewModel:- Applied to
ViewModelclasses (for example,LawniconsViewModelImpl.kt). - This allows Hilt to create and provide ViewModel instances, along with their dependencies, to the UI layer (Activities or Composables).
- Applied to
-
@Inject constructor:- Used on the constructor of a class (for example, most Repository implementations like
IconRepositoryImpl.kt). - This tells Hilt how to create instances of that class. If all of its constructor parameters can also be provided by Hilt, Hilt can automatically create it.
- Used on the constructor of a class (for example, most Repository implementations like
-
@Module:- Applied to classes that provide bindings for types that cannot be constructor-injected (like interfaces or classes from external libraries, such as Retrofit services).
- Modules are typically located in the
di/andapi/packages. - Example:
IconRepositoryModule.ktprovides theIconRepositoryinterface.
-
@Provides:- Used inside
@Moduleclasses on functions. - These functions tell Hilt how to create and provide instances of certain types.
- Example: A function in
GithubApiModule.ktannotated with@Providescreates and returns aGitHubContributorsAPI(Retrofit service) instance.
- Used inside
-
@InstallIn(...):- Used with
@Moduleto specify which Hilt component(s) the module's bindings should be available in (for example,SingletonComponent::classmakes bindings available application-wide,ViewModelComponent::classfor ViewModels).
- Used with
-
Application Setup:
LawniconsApplicationis annotated with@HiltAndroidApp. -
Activity/Screen Setup:
MainActivityis annotated with@AndroidEntryPoint. -
ViewModel Creation: When a Composable requests a ViewModel using
hiltViewModel(), Hilt creates or provides an instance of that@HiltViewModel. -
ViewModel Dependencies: If a ViewModel's constructor (annotated with
@Inject) requires, for example, anIconRepository, Hilt looks for how to provideIconRepository. -
Repository Provision:
- Hilt finds a module (like
IconRepositoryModule.kt) that has an@Providesfunction returningIconRepository. - This function might return an instance of
IconRepositoryImpl. - If
IconRepositoryImplitself has an@Inject constructor(for example, requiring anApplicationcontext or an API service), Hilt provides those dependencies too.
- Hilt finds a module (like
-
API Service Provision (Example):
- If
GitHubContributorsRepository(used byContributorsViewModel) needsGitHubContributorsAPI, Hilt looks to a module likeGithubApiModule.kt. - An
@Providesfunction in that module configures and creates the Retrofit service instance forGitHubContributorsAPI.
- If
This system allows classes to simply declare their dependencies in their constructors (or via field injection in certain Android components, though constructor injection is preferred), and Hilt takes care of constructing and providing those dependencies.
You can explore the modules in the di/ and api/ packages to see specific examples of how dependencies like Repositories and Retrofit services are provided.
Found an issue or have a suggestion for this wiki? Please let us know!
- Home
- Getting Started
- Architecture
- Key Feature Explanations
- Troubleshooting for Developers
- Project Glossary