{"id":190,"date":"2026-10-20T11:28:00","date_gmt":"2026-10-20T11:28:00","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=190"},"modified":"2026-11-06T08:48:25","modified_gmt":"2026-11-06T08:48:25","slug":"dependency-injection-in-android-with-kotlin-manual-di-constructor-injection-hilt-fundamentals","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/dependency-injection-in-android-with-kotlin-manual-di-constructor-injection-hilt-fundamentals\/","title":{"rendered":"Dependency Injection in Android with Kotlin: Manual DI, Constructor Injection &#038; Hilt Fundamentals"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Dependency Injection (DI) is one of the most important architectural concepts for building scalable, maintainable, and testable Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As Android projects grow, classes depend on other classes to perform their responsibilities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, a ViewModel may depend on a repository, a repository may depend on a Retrofit API service, and the API service may depend on an OkHttp client.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But who should create these dependencies?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Should every class create its own objects?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How can we replace real dependencies with fake implementations during testing?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How can we share expensive resources such as databases and network clients?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These are some of the problems Dependency Injection helps solve.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll explore Dependency Injection using Kotlin, Jetpack Compose, ViewModel, and Hilt fundamentals.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll also build a practical User Profile application using manual Dependency Injection.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What You&#8217;ll Learn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you&#8217;ll understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Dependency Injection is<\/li>\n\n\n\n<li>Why Dependency Injection matters<\/li>\n\n\n\n<li>Tight coupling vs loose coupling<\/li>\n\n\n\n<li>Dependency Inversion Principle<\/li>\n\n\n\n<li>Constructor Injection<\/li>\n\n\n\n<li>Property Injection<\/li>\n\n\n\n<li>Method Injection<\/li>\n\n\n\n<li>Manual Dependency Injection<\/li>\n\n\n\n<li>Dependency Containers<\/li>\n\n\n\n<li>Dependency lifetimes<\/li>\n\n\n\n<li>Interface-based dependencies<\/li>\n\n\n\n<li>Dependency Injection vs Service Locator<\/li>\n\n\n\n<li>Hilt fundamentals<\/li>\n\n\n\n<li>Hilt annotations<\/li>\n\n\n\n<li>ViewModel injection<\/li>\n\n\n\n<li>Repository injection<\/li>\n\n\n\n<li>Testing with fake dependencies<\/li>\n\n\n\n<li>Production best practices<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Is Dependency Injection?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection is a design technique where an object receives the dependencies it needs from an external source rather than creating them internally.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider a ViewModel that needs a repository.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Without Dependency Injection<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserViewModel : ViewModel() {\n\n    private val repository = UserRepositoryImpl()\n\n    fun loadUser() {\n        repository.getUser()\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel creates its own repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This introduces tight coupling.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel is directly responsible for constructing its dependency.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">With Dependency Injection<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserViewModel(\n    private val repository: UserRepository\n) : ViewModel() {\n\n    fun loadUser() {\n        repository.getUser()\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now the repository is provided through the constructor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel does not need to know how the repository is created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is Dependency Injection.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Why Is Dependency Injection Important?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Consider a production Android application with the following components:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Jetpack Compose UI\n        |\n        v\n    UserViewModel\n        |\n        v\n    UserRepository\n        |\n        v\n    Retrofit API\n        |\n        v\n    OkHttp Client<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each component depends on another component.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Without a consistent dependency management strategy, object creation can become complicated.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits of Dependency Injection<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reduced coupling<\/li>\n\n\n\n<li>Easier unit testing<\/li>\n\n\n\n<li>Better separation of concerns<\/li>\n\n\n\n<li>Centralized dependency construction<\/li>\n\n\n\n<li>Improved maintainability<\/li>\n\n\n\n<li>Easier replacement of implementations<\/li>\n\n\n\n<li>Clearer object lifetimes<\/li>\n\n\n\n<li>Better scalability<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection does not automatically guarantee good architecture.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a technique that helps organize object creation and dependency ownership.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Understanding Tight Coupling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Tight coupling occurs when a class depends directly on a specific implementation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class NotificationManager {\n\n    private val emailService = EmailService()\n\n    fun sendNotification() {\n        emailService.sendEmail()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Problem<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">NotificationManager creates EmailService directly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Replacing email notifications with push notifications requires modifying NotificationManager.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Testing may also become difficult if EmailService performs real network operations.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Understanding Loose Coupling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Loose coupling means components depend on stable contracts rather than unnecessary implementation details.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface NotificationService {\n\n    fun sendNotification()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Email Implementation<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class EmailNotificationService : NotificationService {\n\n    override fun sendNotification() {\n        println(\"Email notification sent\")\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Push Implementation<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class PushNotificationService : NotificationService {\n\n    override fun sendNotification() {\n        println(\"Push notification sent\")\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Consumer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class NotificationManager(\n    private val service: NotificationService\n) {\n\n    fun notifyUser() {\n        service.sendNotification()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">NotificationManager depends on the NotificationService contract.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Different implementations can be supplied without changing the consumer.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Dependency Inversion Principle<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Dependency Inversion Principle (DIP) is one of the SOLID design principles.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It encourages high-level components to depend on abstractions rather than concrete low-level implementations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Without Dependency Inversion<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>UserViewModel\n     |\n     v\nRoomUserRepository<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel depends directly on a specific repository implementation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">With Dependency Inversion<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"> <code>      UserViewModel\n             |\n             v\n       UserRepository\n             ^\n             |\n      RoomUserRepository<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Kotlin Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface UserRepository {\n\n    suspend fun getUsername(): String\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>class RoomUserRepository : UserRepository {\n\n    override suspend fun getUsername(): String {\n        return \"Android Developer\"\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserViewModel(\n    private val repository: UserRepository\n) : ViewModel()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection and Dependency Inversion are related but different.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection describes how dependencies are supplied.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Inversion describes how architectural dependencies should be organized.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. Types of Dependency Injection<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There are three commonly discussed injection techniques.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Constructor Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dependencies are provided through a constructor.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserRepository(\n    private val apiService: ApiService\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Constructor Injection is generally preferred because dependencies are explicit and available when the object is created.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Property Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dependencies are assigned to properties.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class AnalyticsManager {\n\n    lateinit var analyticsService: AnalyticsService\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Property Injection can be useful when a framework creates an object and constructor injection is unavailable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, it can introduce partially initialized objects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dependencies are passed into individual methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ReportGenerator {\n\n    fun generate(\n        formatter: ReportFormatter\n    ) {\n        formatter.format()\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Method Injection is useful when a dependency is needed only for a particular operation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. Why Constructor Injection Is Preferred<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constructor Injection provides several advantages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class ProductRepository(\n    private val api: ProductApi,\n    private val database: ProductDatabase\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The class cannot be created without its required dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Explicit dependencies<\/li>\n\n\n\n<li>Easier testing<\/li>\n\n\n\n<li>Better immutability<\/li>\n\n\n\n<li>Reduced initialization errors<\/li>\n\n\n\n<li>Clear object construction<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer constructor injection for ordinary Kotlin classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use framework-supported field injection only when necessary.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. What Is Manual Dependency Injection?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Manual Dependency Injection means developers create and connect dependencies themselves without using a dedicated DI framework.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val api = ProductApiImpl()\n\nval repository = ProductRepositoryImpl(api)\n\nval viewModel = ProductViewModel(repository)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dependencies are created outside their consumers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each component receives what it needs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advantages<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No DI framework required<\/li>\n\n\n\n<li>Easy to understand<\/li>\n\n\n\n<li>Full control over object creation<\/li>\n\n\n\n<li>Suitable for small applications<\/li>\n\n\n\n<li>Useful for learning DI fundamentals<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Limitations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>More setup code<\/li>\n\n\n\n<li>Complex dependency graphs become harder to maintain<\/li>\n\n\n\n<li>Scope management requires additional care<\/li>\n\n\n\n<li>Boilerplate grows with application complexity<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. What Is a Dependency Container?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A dependency container is a component responsible for creating and holding shared dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class AppContainer {\n\n    private val api = UserApiImpl()\n\n    val userRepository: UserRepository =\n        UserRepositoryImpl(api)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">AppContainer centralizes dependency construction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Other application components receive dependencies from the container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Shared object management<\/li>\n\n\n\n<li>Centralized configuration<\/li>\n\n\n\n<li>Clear dependency ownership<\/li>\n\n\n\n<li>Easier replacement of implementations<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid turning the container into a globally accessible object that every class queries directly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer passing dependencies to consumers explicitly.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. Understanding Dependency Lifetimes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Different objects require different lifetimes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Lifetimes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Application Lifetime<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suitable for shared resources such as Room databases and configured network clients.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Activity Lifetime<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suitable for dependencies associated with a particular Activity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ViewModel Lifetime<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suitable for screen-level state holders and their owned resources.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Operation Lifetime<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suitable for short-lived objects used during individual operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Application\n   |\n   +-- AppContainer\n   |      |\n   |      +-- Retrofit\n   |      +-- Room Database\n   |      +-- Repository\n   |\n   +-- MainActivity\n          |\n          +-- UserViewModel<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Choose dependency lifetimes based on ownership, resource usage, and concurrency requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do not retain Activity references inside application-scoped objects.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Practical Mini-Project: User Profile App with Manual Dependency Injection<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s build a simple User Profile application using Kotlin, Jetpack Compose, ViewModel, StateFlow, and manual Dependency Injection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Features<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Display user profile information<\/li>\n\n\n\n<li>Load user data through a repository<\/li>\n\n\n\n<li>Update the displayed username<\/li>\n\n\n\n<li>Manage UI state using StateFlow<\/li>\n\n\n\n<li>Inject dependencies through constructors<\/li>\n\n\n\n<li>Centralize object creation<\/li>\n\n\n\n<li>Test with fake repositories<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Technology Stack<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kotlin<\/li>\n\n\n\n<li>Jetpack Compose<\/li>\n\n\n\n<li>Material 3<\/li>\n\n\n\n<li>ViewModel<\/li>\n\n\n\n<li>StateFlow<\/li>\n\n\n\n<li>Coroutines<\/li>\n\n\n\n<li>Manual Dependency Injection<\/li>\n\n\n\n<li>MVVM<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This example uses an in-memory repository to focus on Dependency Injection.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. Recommended Project Structure<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>com.example.manualdi\/\n\u2502\n\u251c\u2500\u2500 MyApplication.kt\n\u251c\u2500\u2500 MainActivity.kt\n\u2502\n\u251c\u2500\u2500 di\/\n\u2502   \u2514\u2500\u2500 AppContainer.kt\n\u2502\n\u251c\u2500\u2500 domain\/\n\u2502   \u251c\u2500\u2500 model\/\n\u2502   \u2502   \u2514\u2500\u2500 User.kt\n\u2502   \u2514\u2500\u2500 repository\/\n\u2502       \u2514\u2500\u2500 UserRepository.kt\n\u2502\n\u251c\u2500\u2500 data\/\n\u2502   \u2514\u2500\u2500 InMemoryUserRepository.kt\n\u2502\n\u2514\u2500\u2500 presentation\/\n    \u251c\u2500\u2500 UserUiState.kt\n    \u251c\u2500\u2500 UserViewModel.kt\n    \u251c\u2500\u2500 UserViewModelFactory.kt\n    \u2514\u2500\u2500 UserProfileScreen.kt<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12. Step 1 \u2014 Create the User Model<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>domain\/model\/User.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi.domain.model\n\ndata class User(\n    val id: Int,\n    val name: String,\n    val email: String\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The User model represents the application data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not depend on Android framework classes.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">13. Step 2 \u2014 Create the Repository Interface<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>domain\/repository\/UserRepository.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi.domain.repository\n\nimport com.example.manualdi.domain.model.User\nimport kotlinx.coroutines.flow.Flow\n\ninterface UserRepository {\n\n    fun observeUser(): Flow&lt;User&gt;\n\n    suspend fun updateUsername(name: String)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The repository contract defines operations for observing and updating a user.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel will depend on this interface rather than a concrete implementation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">14. Step 3 \u2014 Implement the Repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>data\/InMemoryUserRepository.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi.data\n\nimport com.example.manualdi.domain.model.User\nimport com.example.manualdi.domain.repository.UserRepository\n\nimport kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.Flow\nimport kotlinx.coroutines.flow.update\n\nclass InMemoryUserRepository : UserRepository {\n\n    private val userState = MutableStateFlow(\n        User(\n            id = 1,\n            name = \"Android Developer\",\n            email = \"developer@example.com\"\n        )\n    )\n\n    override fun observeUser(): Flow&lt;User&gt; {\n        return userState\n    }\n\n    override suspend fun updateUsername(name: String) {\n\n        val updatedName = name.trim()\n\n        if (updatedName.isBlank()) {\n            return\n        }\n\n        userState.update { currentUser -&gt;\n            currentUser.copy(name = updatedName)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The repository stores the current user in MutableStateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whenever the username changes, the repository emits updated data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This implementation is not persistent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Data will be lost when the application process terminates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use Room or DataStore when durable storage is required.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">15. Step 4 \u2014 Create the Application Container<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>di\/AppContainer.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi.di\n\nimport com.example.manualdi.data.InMemoryUserRepository\nimport com.example.manualdi.domain.repository.UserRepository\n\nclass AppContainer {\n\n    val userRepository: UserRepository =\n        InMemoryUserRepository()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">AppContainer creates the repository and owns its lifetime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The repository can be shared across different application components.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">16. Step 5 \u2014 Create the Application Class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>MyApplication.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi\n\nimport android.app.Application\nimport com.example.manualdi.di.AppContainer\n\nclass MyApplication : Application() {\n\n    lateinit var container: AppContainer\n        private set\n\n    override fun onCreate() {\n        super.onCreate()\n\n        container = AppContainer()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Application class creates the dependency container once per application process.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">AndroidManifest Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Register the custom Application class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;application\n    android:name=\".MyApplication\"\n    ... &gt;\n&lt;\/application&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The ellipsis represents your existing application attributes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the remaining manifest configuration generated by Android Studio.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">17. Step 6 \u2014 Create the UI State<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/UserUiState.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi.presentation\n\nimport com.example.manualdi.domain.model.User\n\ndata class UserUiState(\n    val user: User? = null,\n    val isLoading: Boolean = true,\n    val errorMessage: String? = null\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The UI state represents the information required to render the User Profile screen.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">18. Step 7 \u2014 Create the ViewModel<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/UserViewModel.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi.presentation\n\nimport androidx.lifecycle.ViewModel\nimport androidx.lifecycle.viewModelScope\n\nimport com.example.manualdi.domain.repository.UserRepository\n\nimport kotlinx.coroutines.flow.SharingStarted\nimport kotlinx.coroutines.flow.StateFlow\nimport kotlinx.coroutines.flow.catch\nimport kotlinx.coroutines.flow.map\nimport kotlinx.coroutines.flow.stateIn\nimport kotlinx.coroutines.launch\n\nclass UserViewModel(\n    private val repository: UserRepository\n) : ViewModel() {\n\n    val uiState: StateFlow&lt;UserUiState&gt; =\n        repository.observeUser()\n            .map { user -&gt;\n                UserUiState(\n                    user = user,\n                    isLoading = false\n                )\n            }\n            .catch { exception -&gt;\n                emit(\n                    UserUiState(\n                        isLoading = false,\n                        errorMessage = \"Unable to load profile\"\n                    )\n                )\n            }\n            .stateIn(\n                scope = viewModelScope,\n                started = SharingStarted.WhileSubscribed(5_000),\n                initialValue = UserUiState()\n            )\n\n    fun updateUsername(name: String) {\n\n        viewModelScope.launch {\n            repository.updateUsername(name)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel receives UserRepository through constructor injection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not create the repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The repository Flow is transformed into UI state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow exposes the latest profile state to Jetpack Compose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Production Note<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For a real application, handle update failures explicitly and preserve coroutine cancellation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also consider using a separate editable draft state for complex forms.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">19. Step 8 \u2014 Create the ViewModel Factory<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/UserViewModelFactory.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi.presentation\n\nimport androidx.lifecycle.ViewModel\nimport androidx.lifecycle.ViewModelProvider\n\nimport com.example.manualdi.domain.repository.UserRepository\n\nclass UserViewModelFactory(\n    private val repository: UserRepository\n) : ViewModelProvider.Factory {\n\n    override fun &lt;T : ViewModel&gt; create(\n        modelClass: Class&lt;T&gt;\n    ): T {\n\n        if (\n            modelClass.isAssignableFrom(\n                UserViewModel::class.java\n            )\n        ) {\n\n            @Suppress(\"UNCHECKED_CAST\")\n            return UserViewModel(repository) as T\n        }\n\n        throw IllegalArgumentException(\n            \"Unknown ViewModel class\"\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The factory creates UserViewModel with its required repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is a practical example of manual constructor injection.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">20. Step 9 \u2014 Create the Jetpack Compose Screen<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/UserProfileScreen.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi.presentation\n\nimport androidx.compose.foundation.layout.*\nimport androidx.compose.material3.*\nimport androidx.compose.runtime.*\nimport androidx.compose.ui.Alignment\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.unit.dp\n\nimport androidx.lifecycle.compose.collectAsStateWithLifecycle\n\n@OptIn(ExperimentalMaterial3Api::class)\n@Composable\nfun UserProfileScreen(\n    viewModel: UserViewModel\n) {\n\n    val uiState by viewModel.uiState\n        .collectAsStateWithLifecycle()\n\n    var username by rememberSaveable {\n        mutableStateOf(\"\")\n    }\n\n    Scaffold(\n        topBar = {\n            TopAppBar(\n                title = {\n                    Text(\"Manual DI Profile\")\n                }\n            )\n        }\n    ) { paddingValues -&gt;\n\n        Column(\n            modifier = Modifier\n                .fillMaxSize()\n                .padding(paddingValues)\n                .padding(16.dp),\n            verticalArrangement =\n                Arrangement.spacedBy(16.dp)\n        ) {\n\n            when {\n\n                uiState.isLoading -&gt; {\n\n                    CircularProgressIndicator()\n                }\n\n                uiState.errorMessage != null -&gt; {\n\n                    Text(\n                        text = uiState.errorMessage.orEmpty(),\n                        color = MaterialTheme.colorScheme.error\n                    )\n                }\n\n                uiState.user != null -&gt; {\n\n                    val user = uiState.user!!\n\n                    Text(\n                        text = \"User Profile\",\n                        style = MaterialTheme.typography.headlineSmall\n                    )\n\n                    Text(\n                        text = \"Name: ${user.name}\"\n                    )\n\n                    Text(\n                        text = \"Email: ${user.email}\"\n                    )\n\n                    HorizontalDivider()\n\n                    OutlinedTextField(\n                        value = username,\n                        onValueChange = {\n                            username = it\n                        },\n                        label = {\n                            Text(\"New Username\")\n                        },\n                        modifier = Modifier.fillMaxWidth(),\n                        singleLine = true\n                    )\n\n                    Button(\n                        onClick = {\n                            viewModel.updateUsername(username)\n                            username = \"\"\n                        },\n                        enabled = username.isNotBlank(),\n                        modifier = Modifier.fillMaxWidth()\n                    ) {\n\n                        Text(\"Update Username\")\n                    }\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The screen observes ViewModel state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It forwards update requests to the ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The screen does not construct the repository or perform data access directly.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">21. Step 10 \u2014 Create MainActivity<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>MainActivity.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.manualdi\n\nimport android.os.Bundle\n\nimport androidx.activity.ComponentActivity\nimport androidx.activity.compose.setContent\n\nimport androidx.compose.material3.MaterialTheme\nimport androidx.compose.runtime.remember\n\nimport androidx.lifecycle.viewmodel.compose.viewModel\n\nimport com.example.manualdi.presentation.UserProfileScreen\nimport com.example.manualdi.presentation.UserViewModel\nimport com.example.manualdi.presentation.UserViewModelFactory\n\nclass MainActivity : ComponentActivity() {\n\n    override fun onCreate(\n        savedInstanceState: Bundle?\n    ) {\n\n        super.onCreate(savedInstanceState)\n\n        val app = application as MyApplication\n\n        setContent {\n\n            MaterialTheme {\n\n                val factory = remember {\n                    UserViewModelFactory(\n                        repository = app.container.userRepository\n                    )\n                }\n\n                val userViewModel: UserViewModel =\n                    viewModel(factory = factory)\n\n                UserProfileScreen(\n                    viewModel = userViewModel\n                )\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MainActivity retrieves the repository from the application container.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel factory injects the repository into UserViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel then exposes UI state to Jetpack Compose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Complete Dependency Flow<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>MyApplication\n      |\n      v\nAppContainer\n      |\n      v\nInMemoryUserRepository\n      |\n      v\nUserViewModelFactory\n      |\n      v\nUserViewModel\n      |\n      v\nUserProfileScreen<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This demonstrates manual Dependency Injection in a real Android application.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">22. Required Dependencies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a Kotlin Android project with Jetpack Compose enabled.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure the following dependencies are available.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n\n    implementation(\n        \"androidx.activity:activity-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-viewmodel-ktx:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-viewmodel-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-runtime-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"org.jetbrains.kotlinx:kotlinx-coroutines-android:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.compose.material3:material3\"\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace version placeholders with compatible stable versions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the Compose BOM where appropriate.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">23. Expected Application UI<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Initial Profile Screen<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|        Manual DI Profile       |\n|                                |\n| User Profile                   |\n|                                |\n| Name: Android Developer        |\n|                                |\n| Email: developer@example.com   |\n|                                |\n| ------------------------------ |\n|                                |\n| &#91; New Username               ] |\n|                                |\n|       &#91; Update Username ]      |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">After Updating Username<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|        Manual DI Profile       |\n|                                |\n| User Profile                   |\n|                                |\n| Name: Kotlin Expert            |\n|                                |\n| Email: developer@example.com   |\n|                                |\n| ------------------------------ |\n|                                |\n| &#91; New Username               ] |\n|                                |\n|       &#91; Update Username ]      |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When the username changes:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>The UI calls the ViewModel.<\/li>\n\n\n\n<li>The ViewModel calls the repository.<\/li>\n\n\n\n<li>The repository updates StateFlow.<\/li>\n\n\n\n<li>The ViewModel receives the updated user.<\/li>\n\n\n\n<li>Jetpack Compose renders the new username.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">24. What Is Hilt in Android?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt is a Dependency Injection library built on top of Dagger.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It provides standardized dependency management for Android components.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt reduces much of the manual setup required for larger dependency graphs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Without Hilt<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Developers manually create:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Application containers<\/li>\n\n\n\n<li>Dependency factories<\/li>\n\n\n\n<li>ViewModel factories<\/li>\n\n\n\n<li>Shared dependency instances<\/li>\n\n\n\n<li>Dependency wiring<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">With Hilt<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt generates dependency management code based on annotations and configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Less manual dependency wiring<\/li>\n\n\n\n<li>Android lifecycle integration<\/li>\n\n\n\n<li>Constructor injection<\/li>\n\n\n\n<li>Component scopes<\/li>\n\n\n\n<li>ViewModel integration<\/li>\n\n\n\n<li>Easier dependency replacement in tests<\/li>\n\n\n\n<li>Compile-time dependency graph validation<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">25. Important Hilt Annotations<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Annotation<\/th><th>Purpose<\/th><\/tr><tr><td>@HiltAndroidApp<\/td><td>Initializes Hilt application integration<\/td><\/tr><tr><td>@AndroidEntryPoint<\/td><td>Enables injection into supported Android classes<\/td><\/tr><tr><td>@Inject<\/td><td>Requests constructor or field injection<\/td><\/tr><tr><td>@HiltViewModel<\/td><td>Integrates ViewModel creation with Hilt<\/td><\/tr><tr><td>@Module<\/td><td>Declares dependency bindings<\/td><\/tr><tr><td>@InstallIn<\/td><td>Specifies the Hilt component<\/td><\/tr><tr><td>@Provides<\/td><td>Provides dependencies through a function<\/td><\/tr><tr><td>@Binds<\/td><td>Binds an implementation to an abstraction<\/td><\/tr><tr><td>@Singleton<\/td><td>Scopes an instance to the singleton component<\/td><\/tr><tr><td>@ApplicationContext<\/td><td>Qualifies an application Context dependency<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">26. Hilt Application Setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a custom Application class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@HiltAndroidApp\nclass MyApplication : Application()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Register it in AndroidManifest.xml.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;application\n    android:name=\".MyApplication\"\n    ... &gt;\n&lt;\/application&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">@HiltAndroidApp enables Hilt&#8217;s generated application-level dependency container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt requires appropriate Gradle plugin, annotation processing, and runtime dependency configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the current compatible versions recommended by the Android and Dagger documentation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">27. Constructor Injection with Hilt<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt supports constructor injection for classes it can construct.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserRepositoryImpl @Inject constructor(\n    private val apiService: UserApiService\n) : UserRepository {\n\n    override suspend fun getUser(): User {\n        return apiService.getUser()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">@Inject tells Hilt how to construct UserRepositoryImpl.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt must also know how to provide UserApiService.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Constructor injection alone does not automatically bind UserRepositoryImpl to the UserRepository interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An interface binding must be configured when consumers request the interface type.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">28. Hilt ViewModel Injection<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt integrates with Android ViewModel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@HiltViewModel\nclass UserViewModel @Inject constructor(\n    private val repository: UserRepository\n) : ViewModel() {\n\n    private val _username =\n        MutableStateFlow(\"\")\n\n    val username: StateFlow&lt;String&gt; =\n        _username.asStateFlow()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">@HiltViewModel enables Hilt-aware ViewModel creation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The repository is supplied through constructor injection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Activity Setup<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@AndroidEntryPoint\nclass MainActivity : ComponentActivity() {\n\n    override fun onCreate(\n        savedInstanceState: Bundle?\n    ) {\n        super.onCreate(savedInstanceState)\n\n        setContent {\n\n            val viewModel: UserViewModel = viewModel()\n\n            UserScreen(viewModel)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This assumes the repository and all of its dependencies have valid Hilt bindings.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">29. Understanding @Provides<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use @Provides when Hilt cannot construct a dependency through constructor injection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, Retrofit requires builder configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module\n@InstallIn(SingletonComponent::class)\nobject NetworkModule {\n\n    @Provides\n    @Singleton\n    fun provideRetrofit(): Retrofit {\n\n        return Retrofit.Builder()\n            .baseUrl(\"https:\/\/example.com\/\")\n            .build()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt uses the provider function to create Retrofit.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A real API integration also requires the appropriate converter, HTTP configuration, and service bindings.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">30. Understanding @Binds<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use @Binds to connect an implementation to an interface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module\n@InstallIn(SingletonComponent::class)\nabstract class RepositoryModule {\n\n    @Binds\n    abstract fun bindUserRepository(\n        implementation: UserRepositoryImpl\n    ): UserRepository\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When a class requests UserRepository, Hilt can provide UserRepositoryImpl.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer @Binds for straightforward interface-to-implementation bindings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use @Provides when object creation requires custom logic.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">31. Understanding Hilt Scopes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt scopes control how dependency instances are shared within components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Scopes<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Scope<\/th><th>Lifetime<\/th><\/tr><tr><td>@Singleton<\/td><td>SingletonComponent lifetime<\/td><\/tr><tr><td>@ActivityRetainedScoped<\/td><td>Retained Activity component lifetime<\/td><\/tr><tr><td>@ViewModelScoped<\/td><td>ViewModel component lifetime<\/td><\/tr><tr><td>@ActivityScoped<\/td><td>Activity component lifetime<\/td><\/tr><tr><td>@FragmentScoped<\/td><td>Fragment component lifetime<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Singleton\nclass AnalyticsRepository @Inject constructor()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">@Singleton means one instance per SingletonComponent, not necessarily one instance across multiple operating-system processes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use scopes only when sharing an instance is appropriate.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">32. Dependency Injection vs Service Locator<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection and Service Locator are related techniques but have different characteristics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dependency Injection<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserViewModel(\n    private val repository: UserRepository\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The dependency is explicitly provided.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Service Locator<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserViewModel {\n\n    private val repository =\n        ServiceLocator.getUserRepository()\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The class retrieves its dependency from a central registry.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Comparison<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Dependency Injection<\/th><th>Service Locator<\/th><\/tr><tr><td>Dependencies are supplied<\/td><td>Dependencies are requested<\/td><\/tr><tr><td>Dependencies are explicit<\/td><td>Dependencies may be hidden<\/td><\/tr><tr><td>Constructor contracts are clear<\/td><td>Global access is possible<\/td><\/tr><tr><td>Testing is straightforward<\/td><td>Testing may require registry changes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Recommendation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer explicit constructor injection for application classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use centralized containers primarily for constructing and supplying dependencies.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">33. Testing with Fake Dependencies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection makes it easier to test components independently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fake Repository<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class FakeUserRepository : UserRepository {\n\n    private val userState = MutableStateFlow(\n        User(\n            id = 1,\n            name = \"Test User\",\n            email = \"test@example.com\"\n        )\n    )\n\n    override fun observeUser(): Flow&lt;User&gt; {\n        return userState\n    }\n\n    override suspend fun updateUsername(name: String) {\n\n        userState.update {\n            it.copy(name = name)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The fake repository does not require a database or network connection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It provides controlled data for testing.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">34. ViewModel Unit Testing<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Test\nfun `updating username updates repository`() = runTest {\n\n    val repository = FakeUserRepository()\n\n    val viewModel = UserViewModel(repository)\n\n    viewModel.updateUsername(\"Kotlin Developer\")\n\n    advanceUntilIdle()\n\n    val user = repository.observeUser().first()\n\n    assertEquals(\n        \"Kotlin Developer\",\n        user.name\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Because UserViewModel uses viewModelScope, configure Dispatchers.Main with a test dispatcher in JVM unit tests.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use kotlinx-coroutines-test and a suitable MainDispatcherRule.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This ensures ViewModel coroutines run predictably during testing.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">35. Common Dependency Injection Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Creating Dependencies Inside ViewModels<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer constructor injection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Using Global Mutable Singletons Everywhere<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use appropriately scoped dependencies and explicit ownership.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Injecting Concrete Implementations Unnecessarily<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use interfaces when they provide useful architectural boundaries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Creating Too Many Interfaces<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Do not create abstractions without a meaningful purpose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Incorrect Dependency Scopes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid retaining short-lived Android components in long-lived objects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Overusing @Singleton<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not every dependency needs application-wide sharing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Using Field Injection When Constructor Injection Is Available<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer explicit constructor dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 8: Ignoring Testing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use fake dependencies to test ViewModels and business logic.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 9: Mixing Dependency Construction with Business Logic<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Keep dependency creation separate from application behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 10: Introducing Hilt Too Early Without Understanding DI<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Learn constructor injection and dependency ownership first.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">36. Production Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Follow these recommendations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prefer constructor injection.<\/li>\n\n\n\n<li>Depend on abstractions where useful.<\/li>\n\n\n\n<li>Keep dependency construction centralized.<\/li>\n\n\n\n<li>Choose appropriate dependency lifetimes.<\/li>\n\n\n\n<li>Avoid unnecessary global state.<\/li>\n\n\n\n<li>Use application Context only when appropriate.<\/li>\n\n\n\n<li>Avoid leaking Activities or Fragments.<\/li>\n\n\n\n<li>Keep ViewModels independent of dependency construction.<\/li>\n\n\n\n<li>Use @Binds for simple interface bindings.<\/li>\n\n\n\n<li>Use @Provides for custom dependency creation.<\/li>\n\n\n\n<li>Scope expensive shared resources appropriately.<\/li>\n\n\n\n<li>Test components using fake dependencies.<\/li>\n\n\n\n<li>Avoid unnecessary DI abstractions.<\/li>\n\n\n\n<li>Use Hilt when the dependency graph justifies it.<\/li>\n\n\n\n<li>Keep dependency boundaries consistent with your architecture.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">37. Dependency Injection Quick Reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Concept<\/th><th>Purpose<\/th><\/tr><tr><td>Dependency Injection<\/td><td>Supply dependencies externally<\/td><\/tr><tr><td>Constructor Injection<\/td><td>Inject through constructor<\/td><\/tr><tr><td>Property Injection<\/td><td>Assign dependencies to properties<\/td><\/tr><tr><td>Method Injection<\/td><td>Supply dependencies to methods<\/td><\/tr><tr><td>Dependency Inversion<\/td><td>Depend on abstractions<\/td><\/tr><tr><td>Manual DI<\/td><td>Manually construct dependencies<\/td><\/tr><tr><td>Dependency Container<\/td><td>Centralize object creation<\/td><\/tr><tr><td>Hilt<\/td><td>Android DI framework<\/td><\/tr><tr><td>Dagger<\/td><td>Compile-time DI framework<\/td><\/tr><tr><td>@Inject<\/td><td>Constructor or field injection<\/td><\/tr><tr><td>@HiltAndroidApp<\/td><td>Application-level Hilt setup<\/td><\/tr><tr><td>@AndroidEntryPoint<\/td><td>Android component integration<\/td><\/tr><tr><td>@HiltViewModel<\/td><td>Hilt ViewModel integration<\/td><\/tr><tr><td>@Module<\/td><td>Dependency binding definitions<\/td><\/tr><tr><td>@Provides<\/td><td>Custom dependency creation<\/td><\/tr><tr><td>@Binds<\/td><td>Interface implementation binding<\/td><\/tr><tr><td>@Singleton<\/td><td>SingletonComponent scope<\/td><\/tr><tr><td>Fake Repository<\/td><td>Test implementation<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">38. Practical Exercises<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 1: Constructor Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a Logger interface and inject it into a class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 2: Interface-Based Dependencies<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Implement EmailNotificationService and PushNotificationService.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 3: Manual Dependency Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a repository and inject it into a ViewModel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 4: Dependency Container<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create an AppContainer that manages shared dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 5: User Profile Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build the complete User Profile mini-project.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 6: Fake Repository<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the real repository with a fake implementation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 7: ViewModel Testing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Test ViewModel behavior using a fake repository.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 8: Hilt Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Configure @HiltAndroidApp in a sample project.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 9: Hilt ViewModel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a ViewModel using @HiltViewModel and constructor injection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 10: Repository Binding<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use @Binds to connect a repository implementation to its interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">39. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is Dependency Injection in Android?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection is a technique where classes receive required dependencies externally rather than constructing them internally.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why use Dependency Injection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It reduces coupling, improves testability, and centralizes dependency management.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is Constructor Injection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Constructor Injection provides dependencies through a class constructor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is Manual Dependency Injection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Manual DI means developers create and connect dependencies without a dedicated DI framework.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is Hilt?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt is a Dependency Injection library built on Dagger that provides standardized Android integration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is Hilt required for Android development?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Manual DI is suitable for many applications, particularly smaller projects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between Hilt and Dagger?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dagger is a general compile-time Dependency Injection framework.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt builds on Dagger and provides Android-specific components, scopes, and integrations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is @Inject?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">@Inject identifies constructors or fields that participate in dependency injection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is @Binds?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">@Binds connects an implementation to an interface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is @Provides?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">@Provides defines how to create a dependency through a provider function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Hilt automatically create every dependency?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt requires valid bindings for every requested dependency in its graph.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can Dependency Injection improve testing?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It allows real dependencies to be replaced with controlled fake implementations.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What We&#8217;ve Learned<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we explored:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dependency Injection fundamentals<\/li>\n\n\n\n<li>Tight coupling and loose coupling<\/li>\n\n\n\n<li>Dependency Inversion Principle<\/li>\n\n\n\n<li>Constructor Injection<\/li>\n\n\n\n<li>Property and Method Injection<\/li>\n\n\n\n<li>Manual Dependency Injection<\/li>\n\n\n\n<li>Dependency Containers<\/li>\n\n\n\n<li>Dependency lifetimes<\/li>\n\n\n\n<li>Interface-based architecture<\/li>\n\n\n\n<li>Practical User Profile application<\/li>\n\n\n\n<li>ViewModel constructor injection<\/li>\n\n\n\n<li>Hilt fundamentals<\/li>\n\n\n\n<li>Important Hilt annotations<\/li>\n\n\n\n<li>@Provides and @Binds<\/li>\n\n\n\n<li>Hilt scopes<\/li>\n\n\n\n<li>Dependency Injection vs Service Locator<\/li>\n\n\n\n<li>Testing with fake dependencies<\/li>\n\n\n\n<li>Production best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection is an important foundation for maintainable Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining Kotlin, Jetpack Compose, ViewModel, StateFlow, repositories, and clear dependency ownership, developers can create applications that are easier to extend and test.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The most important principle is simple:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Classes should receive their dependencies instead of unnecessarily creating them.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In our next tutorial:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Article 44 \u2014 Hilt Dependency Injection in Android with Jetpack Compose: Complete Setup &amp; MVVM Integration<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Hilt Gradle configuration<\/li>\n\n\n\n<li>@HiltAndroidApp<\/li>\n\n\n\n<li>@AndroidEntryPoint<\/li>\n\n\n\n<li>@Inject constructor<\/li>\n\n\n\n<li>@HiltViewModel<\/li>\n\n\n\n<li>@Module and @InstallIn<\/li>\n\n\n\n<li>@Provides and @Binds<\/li>\n\n\n\n<li>Hilt scopes<\/li>\n\n\n\n<li>Retrofit injection<\/li>\n\n\n\n<li>Room Database injection<\/li>\n\n\n\n<li>Repository injection<\/li>\n\n\n\n<li>Navigation Compose integration<\/li>\n\n\n\n<li>Hilt testing<\/li>\n\n\n\n<li>Practical production-style Android mini-project<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Happy Coding!<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continue learning modern Android development with <strong>Moiks Developer Blog<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master Dependency Injection in Android using Kotlin and Jetpack Compose. Learn manual DI, constructor injection, dependency inversion, Hilt fundamentals, and build a practical MVVM application.<\/p>\n","protected":false},"author":1,"featured_media":191,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[3,333,10],"tags":[],"class_list":["post-190","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-development","category-android-tutorials","category-hilt"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=190"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/190\/revisions\/192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/191"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}