{"id":196,"date":"2026-10-20T19:37:00","date_gmt":"2026-10-20T19:37:00","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=196"},"modified":"2026-11-06T08:48:24","modified_gmt":"2026-11-06T08:48:24","slug":"advanced-hilt-dependency-injection-in-android-scopes-qualifiers-modules-testing","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/advanced-hilt-dependency-injection-in-android-scopes-qualifiers-modules-testing\/","title":{"rendered":"Advanced Hilt Dependency Injection in Android: Scopes, Qualifiers, Modules &#038; Testing"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Dependency Injection is a foundational part of modern Android architecture.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In previous tutorials, we learned how Hilt simplifies dependency creation and integration with ViewModel, Retrofit, Room, and Jetpack Compose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, production Android applications often have more complex requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Multiple APIs with different base URLs<\/li>\n\n\n\n<li>Different implementations of the same interface<\/li>\n\n\n\n<li>Application-wide shared dependencies<\/li>\n\n\n\n<li>Activity-specific resources<\/li>\n\n\n\n<li>ViewModel-specific sessions<\/li>\n\n\n\n<li>Expensive objects that should be created lazily<\/li>\n\n\n\n<li>Feature modules with independent dependencies<\/li>\n\n\n\n<li>Fake repositories for automated testing<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding advanced Hilt concepts helps developers manage these scenarios safely and efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll explore advanced Hilt techniques and build a practical Multi-API Dashboard application.<\/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>Hilt component hierarchy<\/li>\n\n\n\n<li>Dependency scopes and lifecycles<\/li>\n\n\n\n<li>SingletonComponent<\/li>\n\n\n\n<li>ActivityRetainedComponent<\/li>\n\n\n\n<li>ViewModelComponent<\/li>\n\n\n\n<li>ActivityComponent<\/li>\n\n\n\n<li>Custom qualifiers<\/li>\n\n\n\n<li>Multiple Retrofit instances<\/li>\n\n\n\n<li>@Binds vs @Provides<\/li>\n\n\n\n<li>@ViewModelScoped dependencies<\/li>\n\n\n\n<li>Lazy injection<\/li>\n\n\n\n<li>Provider injection<\/li>\n\n\n\n<li>Assisted injection concepts<\/li>\n\n\n\n<li>Hilt modularization<\/li>\n\n\n\n<li>@TestInstallIn<\/li>\n\n\n\n<li>@UninstallModules<\/li>\n\n\n\n<li>@BindValue<\/li>\n\n\n\n<li>Dependency graph debugging<\/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. Understanding the Hilt Component Hierarchy<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt provides predefined components corresponding to Android lifecycles.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each component manages a set of dependencies and their lifetimes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Simplified Component Hierarchy<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SingletonComponent\n        |\n        +-- ActivityRetainedComponent\n        |          |\n        |          +-- ViewModelComponent\n        |\n        +-- ActivityComponent\n        |          |\n        |          +-- FragmentComponent\n        |          |\n        |          +-- ViewComponent\n        |\n        +-- ServiceComponent<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This diagram is simplified. Hilt also supports additional components, including ViewWithFragmentComponent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Components Matter<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A component determines:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Which bindings are available.<\/li>\n\n\n\n<li>How long scoped dependencies live.<\/li>\n\n\n\n<li>Which parent-component dependencies can be accessed.<\/li>\n\n\n\n<li>Which Android lifecycle owns the dependency graph.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Choosing the correct component is essential for avoiding memory leaks and unnecessary object creation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Understanding Hilt Scopes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A scope determines whether a dependency instance is reused within a Hilt component.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Hilt Scopes<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Scope<\/th><th>Component<\/th><th>Typical Lifetime<\/th><\/tr><tr><td>@Singleton<\/td><td>SingletonComponent<\/td><td>Application process<\/td><\/tr><tr><td>@ActivityRetainedScoped<\/td><td>ActivityRetainedComponent<\/td><td>Activity across configuration changes<\/td><\/tr><tr><td>@ViewModelScoped<\/td><td>ViewModelComponent<\/td><td>One ViewModel instance<\/td><\/tr><tr><td>@ActivityScoped<\/td><td>ActivityComponent<\/td><td>One Activity instance<\/td><\/tr><tr><td>@FragmentScoped<\/td><td>FragmentComponent<\/td><td>One Fragment instance<\/td><\/tr><tr><td>@ServiceScoped<\/td><td>ServiceComponent<\/td><td>One Service instance<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A scoped dependency is not necessarily created when its component is created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt generally constructs it when first requested.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. SingletonComponent and @Singleton<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SingletonComponent is the application-level Hilt component.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dependencies scoped with @Singleton are shared within this component.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Singleton\nclass AnalyticsManager @Inject constructor() {\n\n    fun trackEvent(event: String) {\n        println(\"Event: $event\")\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Common Singleton Dependencies<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Room database<\/li>\n\n\n\n<li>Retrofit instance<\/li>\n\n\n\n<li>OkHttpClient<\/li>\n\n\n\n<li>Shared repositories<\/li>\n\n\n\n<li>Application-wide configuration<\/li>\n\n\n\n<li>Analytics services<\/li>\n<\/ul>\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 Hilt SingletonComponent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not guarantee one instance across multiple Android processes.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. ActivityRetainedComponent<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ActivityRetainedComponent survives configuration changes such as screen rotation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@ActivityRetainedScoped\nclass UserSession @Inject constructor() {\n\n    val sessionId = System.currentTimeMillis()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The same ActivityRetainedScoped instance can be reused across Activity recreation caused by configuration changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The instance is destroyed when the retained Activity lifecycle ends.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Use Cases<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Activity-wide shared state<\/li>\n\n\n\n<li>Retained coordinators<\/li>\n\n\n\n<li>Resources shared across multiple ViewModels belonging to the same retained Activity<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Do not store references to Activity views or Activity Context inside ActivityRetainedScoped objects.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. ViewModelComponent and @ViewModelScoped<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ViewModelComponent manages dependencies associated with a particular Hilt ViewModel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@ViewModelScoped\nclass SearchSession @Inject constructor() {\n\n    val filters = mutableListOf&lt;String&gt;()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Inject into ViewModel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@HiltViewModel\nclass SearchViewModel @Inject constructor(\n    private val searchSession: SearchSession\n) : ViewModel()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SearchSession is scoped to the ViewModel component.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Multiple dependencies within the same ViewModel component can receive the same SearchSession instance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Different ViewModel instances receive separate ViewModelScoped instances.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use It<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use @ViewModelScoped for stateful collaborators that belong to a particular ViewModel.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. ActivityScoped Dependencies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ActivityScoped dependencies are associated with an Activity instance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@ActivityScoped\nclass ActivityTracker @Inject constructor() {\n\n    var screenCount = 0\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ActivityScoped dependencies do not survive Activity recreation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use ActivityRetainedScoped when sharing across configuration changes 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\">7. Scoped vs Unscoped Dependencies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Not every dependency requires a scope.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Unscoped Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class DateFormatter @Inject constructor()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt may create a new DateFormatter instance for each injection request.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scoped Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Singleton\nclass DateFormatter @Inject constructor()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt reuses the scoped instance within SingletonComponent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recommendation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer unscoped dependencies unless sharing an instance provides a meaningful benefit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Scopes are useful for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Expensive object creation<\/li>\n\n\n\n<li>Shared state<\/li>\n\n\n\n<li>Resource ownership<\/li>\n\n\n\n<li>Consistent instance identity<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid applying @Singleton automatically to every class.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. Understanding Custom Qualifiers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes an application requires multiple dependencies of the same type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Public Retrofit API<\/li>\n\n\n\n<li>Authentication Retrofit API<\/li>\n\n\n\n<li>Analytics Retrofit API<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">All three may use Retrofit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Without qualifiers, Hilt cannot distinguish which instance should be injected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Qualifiers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Qualifier\n@Retention(AnnotationRetention.BINARY)\nannotation class PublicApi<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Qualifier\n@Retention(AnnotationRetention.BINARY)\nannotation class AuthApi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Qualifiers identify different bindings that share the same Kotlin type.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. Multiple Retrofit Instances with Hilt<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Consider an application using two backend services.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Public API<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;jsonplaceholder.typicode.com\/<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Authentication API<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;api.example.com\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second URL is a placeholder representing a separate backend.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NetworkModule<\/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 provideOkHttpClient(): OkHttpClient {\n        return OkHttpClient.Builder().build()\n    }\n\n    @Provides\n    @Singleton\n    @PublicApi\n    fun providePublicRetrofit(\n        client: OkHttpClient\n    ): Retrofit {\n\n        return Retrofit.Builder()\n            .baseUrl(\n                \"https:\/\/jsonplaceholder.typicode.com\/\"\n            )\n            .client(client)\n            .addConverterFactory(\n                GsonConverterFactory.create()\n            )\n            .build()\n    }\n\n    @Provides\n    @Singleton\n    @AuthApi\n    fun provideAuthRetrofit(\n        client: OkHttpClient\n    ): Retrofit {\n\n        return Retrofit.Builder()\n            .baseUrl(\"https:\/\/api.example.com\/\")\n            .client(client)\n            .addConverterFactory(\n                GsonConverterFactory.create()\n            )\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 now has two Retrofit bindings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The qualifiers distinguish them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Both Retrofit instances share the same OkHttpClient in this example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If authentication requires different interceptors, TLS configuration, or timeouts, provide separately qualified OkHttpClient instances as well.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. Injecting Qualified Dependencies<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class PublicRepository @Inject constructor(\n    @PublicApi private val retrofit: Retrofit\n)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>class AuthRepository @Inject constructor(\n    @AuthApi private val retrofit: Retrofit\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The qualifier tells Hilt which Retrofit instance to supply.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When multiple instances of the same type exist, consistently qualify the relevant bindings and injection points.<\/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: Multi-API Dashboard<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll build a dashboard demonstrating advanced Hilt concepts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Features<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Fetch users from a public API<\/li>\n\n\n\n<li>Fetch posts from a public API<\/li>\n\n\n\n<li>Display data using Jetpack Compose<\/li>\n\n\n\n<li>Use two qualified Retrofit instances<\/li>\n\n\n\n<li>Inject API services<\/li>\n\n\n\n<li>Use repository abstractions<\/li>\n\n\n\n<li>Manage UI state with StateFlow<\/li>\n\n\n\n<li>Use a Hilt ViewModel<\/li>\n\n\n\n<li>Handle loading and errors<\/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>Hilt<\/li>\n\n\n\n<li>Retrofit<\/li>\n\n\n\n<li>Gson<\/li>\n\n\n\n<li>OkHttp<\/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>MVVM<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For this tutorial, both services use the same public demonstration backend with separate Retrofit configurations. In production, they could point to different APIs.<\/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.hiltadvanced\/\n\u2502\n\u251c\u2500\u2500 HiltAdvancedApplication.kt\n\u251c\u2500\u2500 MainActivity.kt\n\u2502\n\u251c\u2500\u2500 di\/\n\u2502   \u251c\u2500\u2500 ApiQualifiers.kt\n\u2502   \u251c\u2500\u2500 NetworkModule.kt\n\u2502   \u2514\u2500\u2500 RepositoryModule.kt\n\u2502\n\u251c\u2500\u2500 data\/\n\u2502   \u251c\u2500\u2500 remote\/\n\u2502   \u2502   \u251c\u2500\u2500 UserApi.kt\n\u2502   \u2502   \u251c\u2500\u2500 PostApi.kt\n\u2502   \u2502   \u251c\u2500\u2500 UserDto.kt\n\u2502   \u2502   \u2514\u2500\u2500 PostDto.kt\n\u2502   \u2502\n\u2502   \u2514\u2500\u2500 repository\/\n\u2502       \u2514\u2500\u2500 DashboardRepositoryImpl.kt\n\u2502\n\u251c\u2500\u2500 domain\/\n\u2502   \u2514\u2500\u2500 DashboardRepository.kt\n\u2502\n\u2514\u2500\u2500 presentation\/\n    \u251c\u2500\u2500 DashboardUiState.kt\n    \u251c\u2500\u2500 DashboardViewModel.kt\n    \u2514\u2500\u2500 DashboardScreen.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 Configure Hilt<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create an Android Studio project with Jetpack Compose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the Hilt and KSP plugins to your existing Gradle configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Root build.gradle.kts<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>plugins {\n    id(\"com.google.dagger.hilt.android\") version \"&lt;hilt-version&gt;\" apply false\n    id(\"com.google.devtools.ksp\") version \"&lt;ksp-version&gt;\" apply false\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">App build.gradle.kts<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Apply Hilt and KSP:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>plugins {\n    id(\"com.google.dagger.hilt.android\")\n    id(\"com.google.devtools.ksp\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the required dependencies to your existing Compose project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n\n    implementation(\n        \"com.google.dagger:hilt-android:&lt;hilt-version&gt;\"\n    )\n\n    ksp(\n        \"com.google.dagger:hilt-compiler:&lt;hilt-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        \"com.squareup.retrofit2:retrofit:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"com.squareup.retrofit2:converter-gson:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"com.squareup.okhttp3:okhttp:&lt;version&gt;\"\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use compatible stable versions of Kotlin, KSP, Hilt, and the Android Gradle Plugin.<\/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 Application Class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>HiltAdvancedApplication.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced\n\nimport android.app.Application\nimport dagger.hilt.android.HiltAndroidApp\n\n@HiltAndroidApp\nclass HiltAdvancedApplication : 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=\".HiltAdvancedApplication\"\n    ... &gt;\n&lt;\/application&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also add internet permission:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;uses-permission android:name=\"android.permission.INTERNET\" \/&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the remaining manifest configuration from your project.<\/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 Create Custom Qualifiers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>di\/ApiQualifiers.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.di\n\nimport javax.inject.Qualifier\n\n@Qualifier\n@Retention(AnnotationRetention.BINARY)\nannotation class UsersApi\n\n@Qualifier\n@Retention(AnnotationRetention.BINARY)\nannotation class PostsApi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">These qualifiers distinguish two Retrofit instances.<\/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 DTO Models<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>data\/remote\/UserDto.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.data.remote\n\ndata class UserDto(\n    val id: Int,\n    val name: String,\n    val email: String\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>data\/remote\/PostDto.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.data.remote\n\ndata class PostDto(\n    val id: Int,\n    val userId: Int,\n    val title: String,\n    val body: String\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">DTOs represent JSON responses returned by the remote API.<\/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 API Interfaces<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>data\/remote\/UserApi.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.data.remote\n\nimport retrofit2.http.GET\n\ninterface UserApi {\n\n    @GET(\"users\")\n    suspend fun getUsers(): List&lt;UserDto&gt;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>data\/remote\/PostApi.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.data.remote\n\nimport retrofit2.http.GET\n\ninterface PostApi {\n\n    @GET(\"posts\")\n    suspend fun getPosts(): List&lt;PostDto&gt;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Retrofit generates the API implementations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both endpoints are available through JSONPlaceholder.<\/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 Network Module<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>di\/NetworkModule.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.di\n\nimport com.example.hiltadvanced.data.remote.PostApi\nimport com.example.hiltadvanced.data.remote.UserApi\n\nimport dagger.Module\nimport dagger.Provides\nimport dagger.hilt.InstallIn\nimport dagger.hilt.components.SingletonComponent\n\nimport javax.inject.Singleton\n\nimport okhttp3.OkHttpClient\n\nimport retrofit2.Retrofit\nimport retrofit2.converter.gson.GsonConverterFactory\n\n@Module\n@InstallIn(SingletonComponent::class)\nobject NetworkModule {\n\n    private const val BASE_URL =\n        \"https:\/\/jsonplaceholder.typicode.com\/\"\n\n    @Provides\n    @Singleton\n    fun provideOkHttpClient(): OkHttpClient {\n        return OkHttpClient.Builder().build()\n    }\n\n    @Provides\n    @Singleton\n    @UsersApi\n    fun provideUsersRetrofit(\n        client: OkHttpClient\n    ): Retrofit {\n\n        return Retrofit.Builder()\n            .baseUrl(BASE_URL)\n            .client(client)\n            .addConverterFactory(\n                GsonConverterFactory.create()\n            )\n            .build()\n    }\n\n    @Provides\n    @Singleton\n    @PostsApi\n    fun providePostsRetrofit(\n        client: OkHttpClient\n    ): Retrofit {\n\n        return Retrofit.Builder()\n            .baseUrl(BASE_URL)\n            .client(client)\n            .addConverterFactory(\n                GsonConverterFactory.create()\n            )\n            .build()\n    }\n\n    @Provides\n    fun provideUserApi(\n        @UsersApi retrofit: Retrofit\n    ): UserApi {\n\n        return retrofit.create(UserApi::class.java)\n    }\n\n    @Provides\n    fun providePostApi(\n        @PostsApi retrofit: Retrofit\n    ): PostApi {\n\n        return retrofit.create(PostApi::class.java)\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 module provides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One shared OkHttpClient<\/li>\n\n\n\n<li>Two qualified Retrofit instances<\/li>\n\n\n\n<li>UserApi<\/li>\n\n\n\n<li>PostApi<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Using two Retrofit instances with the same base URL is intentional for demonstration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For this particular sample, one Retrofit instance would be sufficient without the qualifier requirement.<\/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 Repository Interface<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>domain\/DashboardRepository.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.domain\n\nimport com.example.hiltadvanced.data.remote.PostDto\nimport com.example.hiltadvanced.data.remote.UserDto\n\ninterface DashboardRepository {\n\n    suspend fun getUsers(): List&lt;UserDto&gt;\n\n    suspend fun getPosts(): List&lt;PostDto&gt;\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 the data operations needed by the ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In larger applications, map DTOs to domain models instead of exposing remote DTOs through the domain layer.<\/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 Implement the Repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>data\/repository\/DashboardRepositoryImpl.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.data.repository\n\nimport com.example.hiltadvanced.data.remote.PostApi\nimport com.example.hiltadvanced.data.remote.PostDto\nimport com.example.hiltadvanced.data.remote.UserApi\nimport com.example.hiltadvanced.data.remote.UserDto\nimport com.example.hiltadvanced.domain.DashboardRepository\n\nimport javax.inject.Inject\n\nclass DashboardRepositoryImpl @Inject constructor(\n    private val userApi: UserApi,\n    private val postApi: PostApi\n) : DashboardRepository {\n\n    override suspend fun getUsers(): List&lt;UserDto&gt; {\n        return userApi.getUsers()\n    }\n\n    override suspend fun getPosts(): List&lt;PostDto&gt; {\n        return postApi.getPosts()\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 receives both API services through constructor injection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not create Retrofit or OkHttpClient.<\/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 RepositoryModule<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>di\/RepositoryModule.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.di\n\nimport com.example.hiltadvanced.data.repository.DashboardRepositoryImpl\nimport com.example.hiltadvanced.domain.DashboardRepository\n\nimport dagger.Binds\nimport dagger.Module\nimport dagger.hilt.InstallIn\nimport dagger.hilt.components.SingletonComponent\n\nimport javax.inject.Singleton\n\n@Module\n@InstallIn(SingletonComponent::class)\nabstract class RepositoryModule {\n\n    @Binds\n    @Singleton\n    abstract fun bindDashboardRepository(\n        implementation: DashboardRepositoryImpl\n    ): DashboardRepository\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The module binds DashboardRepositoryImpl to DashboardRepository.<\/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 UI State<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/DashboardUiState.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.presentation\n\nimport com.example.hiltadvanced.data.remote.PostDto\nimport com.example.hiltadvanced.data.remote.UserDto\n\ndata class DashboardUiState(\n    val users: List&lt;UserDto&gt; = emptyList(),\n    val posts: List&lt;PostDto&gt; = emptyList(),\n    val isLoading: Boolean = false,\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 current dashboard.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">22. Step 11 \u2014 Create Hilt ViewModel<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/DashboardViewModel.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.presentation\n\nimport androidx.lifecycle.ViewModel\nimport androidx.lifecycle.viewModelScope\n\nimport com.example.hiltadvanced.domain.DashboardRepository\n\nimport dagger.hilt.android.lifecycle.HiltViewModel\n\nimport javax.inject.Inject\n\nimport kotlinx.coroutines.CancellationException\nimport kotlinx.coroutines.async\nimport kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.StateFlow\nimport kotlinx.coroutines.flow.asStateFlow\nimport kotlinx.coroutines.flow.update\nimport kotlinx.coroutines.launch\n\n@HiltViewModel\nclass DashboardViewModel @Inject constructor(\n    private val repository: DashboardRepository\n) : ViewModel() {\n\n    private val _uiState =\n        MutableStateFlow(DashboardUiState())\n\n    val uiState: StateFlow&lt;DashboardUiState&gt; =\n        _uiState.asStateFlow()\n\n    init {\n        loadDashboard()\n    }\n\n    fun loadDashboard() {\n\n        viewModelScope.launch {\n\n            _uiState.update {\n                it.copy(\n                    isLoading = true,\n                    errorMessage = null\n                )\n            }\n\n            try {\n\n                val usersDeferred = async {\n                    repository.getUsers()\n                }\n\n                val postsDeferred = async {\n                    repository.getPosts()\n                }\n\n                val users = usersDeferred.await()\n                val posts = postsDeferred.await()\n\n                _uiState.update {\n                    it.copy(\n                        users = users,\n                        posts = posts,\n                        isLoading = false\n                    )\n                }\n\n            } catch (exception: CancellationException) {\n\n                throw exception\n\n            } catch (exception: Exception) {\n\n                _uiState.update {\n                    it.copy(\n                        isLoading = false,\n                        errorMessage = \"Unable to load dashboard\"\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 ViewModel fetches users and posts concurrently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It updates StateFlow after both requests complete.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI observes the resulting state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Production Note<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A production implementation should consider retry policies, timeouts, error classification, and independent loading states for each dashboard section.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">23. Step 12 \u2014 Create the Jetpack Compose Dashboard<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/DashboardScreen.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.hiltadvanced.presentation\n\nimport androidx.compose.foundation.layout.*\nimport androidx.compose.foundation.lazy.LazyColumn\nimport androidx.compose.foundation.lazy.items\nimport androidx.compose.material3.*\nimport androidx.compose.runtime.*\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 DashboardScreen(\n    viewModel: DashboardViewModel\n) {\n\n    val uiState by viewModel.uiState\n        .collectAsStateWithLifecycle()\n\n    Scaffold(\n        topBar = {\n            TopAppBar(\n                title = {\n                    Text(\"Hilt Multi-API Dashboard\")\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(12.dp)\n        ) {\n\n            Button(\n                onClick = {\n                    viewModel.loadDashboard()\n                },\n                enabled = !uiState.isLoading\n            ) {\n\n                Text(\"Refresh Dashboard\")\n            }\n\n            if (uiState.isLoading) {\n                LinearProgressIndicator(\n                    modifier = Modifier.fillMaxWidth()\n                )\n            }\n\n            uiState.errorMessage?.let { message -&gt;\n\n                Text(\n                    text = message,\n                    color = MaterialTheme.colorScheme.error\n                )\n            }\n\n            Text(\n                text = \"Users: ${uiState.users.size}\",\n                style = MaterialTheme.typography.titleMedium\n            )\n\n            Text(\n                text = \"Posts: ${uiState.posts.size}\",\n                style = MaterialTheme.typography.titleMedium\n            )\n\n            LazyColumn(\n                verticalArrangement =\n                    Arrangement.spacedBy(8.dp)\n            ) {\n\n                item {\n\n                    Text(\n                        text = \"Users\",\n                        style = MaterialTheme.typography.titleLarge\n                    )\n                }\n\n                items(\n                    items = uiState.users,\n                    key = { \"user_${it.id}\" }\n                ) { user -&gt;\n\n                    Card(\n                        modifier = Modifier.fillMaxWidth()\n                    ) {\n\n                        Column(\n                            modifier = Modifier.padding(12.dp)\n                        ) {\n\n                            Text(user.name)\n\n                            Text(user.email)\n                        }\n                    }\n                }\n\n                item {\n\n                    Text(\n                        text = \"Recent Posts\",\n                        style = MaterialTheme.typography.titleLarge\n                    )\n                }\n\n                items(\n                    items = uiState.posts.take(10),\n                    key = { \"post_${it.id}\" }\n                ) { post -&gt;\n\n                    Card(\n                        modifier = Modifier.fillMaxWidth()\n                    ) {\n\n                        Column(\n                            modifier = Modifier.padding(12.dp)\n                        ) {\n\n                            Text(\n                                text = post.title,\n                                style = MaterialTheme.typography.titleMedium\n                            )\n\n                            Text(\n                                text = post.body,\n                                style = MaterialTheme.typography.bodyMedium\n                            )\n                        }\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\">DashboardScreen observes the ViewModel state and displays users and posts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI does not directly access Retrofit or the repository.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">24. Step 13 \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.hiltadvanced\n\nimport android.os.Bundle\n\nimport androidx.activity.ComponentActivity\nimport androidx.activity.compose.setContent\n\nimport androidx.compose.material3.MaterialTheme\n\nimport androidx.lifecycle.viewmodel.compose.viewModel\n\nimport com.example.hiltadvanced.presentation.DashboardScreen\nimport com.example.hiltadvanced.presentation.DashboardViewModel\n\nimport dagger.hilt.android.AndroidEntryPoint\n\n@AndroidEntryPoint\nclass MainActivity : ComponentActivity() {\n\n    override fun onCreate(\n        savedInstanceState: Bundle?\n    ) {\n\n        super.onCreate(savedInstanceState)\n\n        setContent {\n\n            MaterialTheme {\n\n                val dashboardViewModel: DashboardViewModel =\n                    viewModel()\n\n                DashboardScreen(\n                    viewModel = dashboardViewModel\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\">Hilt creates the ViewModel and injects DashboardRepository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The repository receives both API services.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The API services receive their qualified Retrofit instances.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">25. Expected Application UI<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|      Hilt Multi-API Dashboard  |\n|                                |\n|       &#91;Refresh Dashboard]      |\n|                                |\n| Users: 10                      |\n| Posts: 100                     |\n|                                |\n| Users                          |\n|                                |\n| +----------------------------+ |\n| | Leanne Graham              | |\n| | Sincere@april.biz          | |\n| +----------------------------+ |\n|                                |\n| +----------------------------+ |\n| | Ervin Howell               | |\n| | Shanna@melissa.tv          | |\n| +----------------------------+ |\n|                                |\n| Recent Posts                   |\n|                                |\n| +----------------------------+ |\n| | sunt aut facere...         | |\n| | quia et suscipit...        | |\n| +----------------------------+ |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The dashboard displays data returned by the public demonstration API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Actual content and availability depend on the API response.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">26. Understanding Lazy Injection<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes a dependency is expensive to create but may not be needed immediately.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dagger provides Lazy for deferred dependency construction.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class AnalyticsCoordinator @Inject constructor(\n    private val analytics: dagger.Lazy&lt;AnalyticsManager&gt;\n) {\n\n    fun trackScreen() {\n        analytics.get().trackEvent(\"screen_open\")\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 AnalyticsManager dependency is requested when get() is called.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dagger Lazy caches the result for that Lazy instance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Lazy injection can delay construction, but it does not automatically move expensive initialization off the main thread.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">27. Understanding Provider Injection<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Provider allows a dependency to be requested when needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class ReportCoordinator @Inject constructor(\n    private val formatterProvider:\n        javax.inject.Provider&lt;ReportFormatter&gt;\n) {\n\n    fun createReport() {\n\n        val formatter = formatterProvider.get()\n\n        formatter.format()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Difference Between Lazy and Provider<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Lazy<\/th><th>Provider<\/th><\/tr><tr><td>Caches the first result per Lazy instance<\/td><td>Requests a dependency on each get()<\/td><\/tr><tr><td>Useful for deferred initialization<\/td><td>Useful for on-demand retrieval<\/td><\/tr><tr><td>Reuses cached value<\/td><td>May return a new or scoped instance<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Provider does not guarantee a new instance every time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the dependency is scoped, Provider may return the same scoped instance.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">28. Understanding Assisted Injection<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Assisted injection is useful when an object needs both Hilt-provided dependencies and runtime values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Scenario<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A ViewModel requires:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>UserRepository from Hilt<\/li>\n\n\n\n<li>A runtime user ID<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Conceptual Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@HiltViewModel(\n    assistedFactory = UserViewModel.Factory::class\n)\nclass UserViewModel @AssistedInject constructor(\n    private val repository: UserRepository,\n    @Assisted private val userId: String\n) : ViewModel() {\n\n    @AssistedFactory\n    interface Factory {\n\n        fun create(userId: String): UserViewModel\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 provides UserRepository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The caller supplies userId through the assisted factory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For ordinary navigation arguments, SavedStateHandle is often the simpler approach.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Assisted injection is useful when runtime parameters cannot conveniently be obtained through SavedStateHandle or other existing mechanisms.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">29. Advanced Hilt Testing with @TestInstallIn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt allows test modules to replace production modules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module\n@TestInstallIn(\n    components = &#91;SingletonComponent::class],\n    replaces = &#91;RepositoryModule::class]\n)\nabstract class FakeRepositoryModule {\n\n    @Binds\n    abstract fun bindRepository(\n        implementation: FakeDashboardRepository\n    ): DashboardRepository\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 replaces the production repository binding in tests.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">FakeDashboardRepository must have a valid Hilt binding, such as an @Inject constructor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No real API calls<\/li>\n\n\n\n<li>Predictable test data<\/li>\n\n\n\n<li>Faster tests<\/li>\n\n\n\n<li>Better isolation<\/li>\n\n\n\n<li>Reliable failure simulations<\/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\">30. Understanding @UninstallModules<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">@UninstallModules removes specified production modules for an individual Hilt test.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@HiltAndroidTest\n@UninstallModules(RepositoryModule::class)\nclass DashboardTest {\n\n    @get:Rule\n    val hiltRule = HiltAndroidRule(this)\n\n    @Before\n    fun setup() {\n        hiltRule.inject()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This removes RepositoryModule from the test&#8217;s dependency graph.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You must provide replacement bindings for dependencies that are still required.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use @TestInstallIn for reusable test replacements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use @UninstallModules when an individual test needs different dependency configuration.<\/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 @BindValue<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">@BindValue allows a test to provide a dependency from a field.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@HiltAndroidTest\nclass DashboardTest {\n\n    @get:Rule\n    val hiltRule = HiltAndroidRule(this)\n\n    @BindValue\n    @JvmField\n    val repository: DashboardRepository =\n        FakeDashboardRepository()\n\n    @Before\n    fun setup() {\n        hiltRule.inject()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This example assumes the production binding for DashboardRepository has been removed or otherwise replaced appropriately.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid creating duplicate bindings for the same type and qualifier.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">32. Hilt in Multi-Module Android Applications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Large Android projects often use multiple Gradle modules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Structure<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>:app\n:core:common\n:core:network\n:core:database\n:core:designsystem\n:core:domain\n:feature:home\n:feature:profile\n:feature:settings<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended Responsibilities<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>app<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Application initialization, top-level navigation, and application composition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>core<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Retrofit, OkHttp, API configuration, and networking utilities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>core<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Room database, DAOs, and local persistence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>core<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Domain models, repository contracts, and use cases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>feature modules<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feature-specific UI, ViewModels, and feature dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt supports multi-module projects, but the application module must have access to the required Hilt modules and injected classes through the dependency graph.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For dynamic feature modules, additional integration patterns may be required because Hilt does not directly process dynamic feature modules in the same way as ordinary Android modules.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">33. Debugging Hilt Dependency Graph Errors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt validates dependencies during compilation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Error: Missing Binding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Dagger\/MissingBinding]\nDashboardRepository cannot be provided<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Possible Causes<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Missing @Inject constructor<\/li>\n\n\n\n<li>Missing @Binds module<\/li>\n\n\n\n<li>Missing @Provides function<\/li>\n\n\n\n<li>Incorrect qualifier<\/li>\n\n\n\n<li>Binding installed in an inaccessible component<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Common Error: Duplicate Binding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Dagger\/DuplicateBindings]\nRetrofit is bound multiple times<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use qualifiers or remove duplicate bindings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Error: Scope Mismatch<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A scoped binding is installed in an incompatible component.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Solution<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure the scope matches the component where the binding is installed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Debugging Checklist<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Check the full compiler error.<\/li>\n\n\n\n<li>Identify the requested dependency.<\/li>\n\n\n\n<li>Verify its constructor or provider.<\/li>\n\n\n\n<li>Verify qualifiers.<\/li>\n\n\n\n<li>Verify component installation.<\/li>\n\n\n\n<li>Verify scope compatibility.<\/li>\n\n\n\n<li>Verify Gradle module visibility.<\/li>\n\n\n\n<li>Verify Kotlin, KSP, and Hilt compatibility.<\/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\">34. Performance Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt generates dependency wiring at compile time, but object construction still has runtime cost.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recommendations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Avoid unnecessary eager initialization.<\/li>\n\n\n\n<li>Use scopes only when needed.<\/li>\n\n\n\n<li>Share expensive resources appropriately.<\/li>\n\n\n\n<li>Avoid heavy constructors.<\/li>\n\n\n\n<li>Use Lazy for genuinely optional expensive dependencies.<\/li>\n\n\n\n<li>Keep dependency graphs understandable.<\/li>\n\n\n\n<li>Measure startup performance.<\/li>\n\n\n\n<li>Avoid unnecessary singleton state.<\/li>\n\n\n\n<li>Do not inject Activity Context into application-scoped objects.<\/li>\n\n\n\n<li>Use background dispatchers for blocking operations.<\/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 improves dependency management, but it does not automatically improve runtime performance.<\/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 Advanced Hilt Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Using @Singleton Everywhere<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Scope only dependencies that need shared identity or lifetime.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Incorrect Qualifiers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Qualifiers must match between provider and injection point.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Mixing Activity Context and Singleton Scope<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This can introduce memory leaks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Assuming Provider Always Creates New Objects<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Scoped dependencies may be reused.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Assuming Lazy Makes Work Asynchronous<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Lazy delays construction but does not change execution threads.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Overcomplicated Modules<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Organize modules by responsibility.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Incorrect ViewModel Scoping<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use @ViewModelScoped only for dependencies owned by a particular ViewModel component.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 8: Missing Test Bindings<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Replace all required production bindings when configuring test modules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 9: Ignoring Multi-Module Visibility<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure the application module&#8217;s dependency graph can access Hilt bindings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 10: Overengineering Dependency Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use advanced features only when they solve actual application requirements.<\/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>Use @Binds for interface bindings.<\/li>\n\n\n\n<li>Use @Provides for custom object creation.<\/li>\n\n\n\n<li>Apply qualifiers consistently.<\/li>\n\n\n\n<li>Scope dependencies intentionally.<\/li>\n\n\n\n<li>Keep Context lifetimes safe.<\/li>\n\n\n\n<li>Use ViewModelScoped for ViewModel-owned collaborators.<\/li>\n\n\n\n<li>Share expensive network and database resources.<\/li>\n\n\n\n<li>Avoid heavy initialization in constructors.<\/li>\n\n\n\n<li>Use Lazy and Provider appropriately.<\/li>\n\n\n\n<li>Keep modules focused.<\/li>\n\n\n\n<li>Test with fake implementations.<\/li>\n\n\n\n<li>Use @TestInstallIn for reusable test replacements.<\/li>\n\n\n\n<li>Validate dependency graphs during CI builds.<\/li>\n\n\n\n<li>Maintain clear multi-module boundaries.<\/li>\n\n\n\n<li>Profile startup and memory behavior.<\/li>\n\n\n\n<li>Keep Hilt and build tools compatible.<\/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. Advanced Hilt 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>SingletonComponent<\/td><td>Application-level dependency graph<\/td><\/tr><tr><td>ActivityRetainedComponent<\/td><td>Dependencies retained across Activity configuration changes<\/td><\/tr><tr><td>ViewModelComponent<\/td><td>Dependencies associated with a ViewModel<\/td><\/tr><tr><td>@Singleton<\/td><td>SingletonComponent-scoped dependency<\/td><\/tr><tr><td>@ActivityRetainedScoped<\/td><td>Retained Activity-scoped dependency<\/td><\/tr><tr><td>@ViewModelScoped<\/td><td>ViewModel-scoped dependency<\/td><\/tr><tr><td>@ActivityScoped<\/td><td>Activity-scoped dependency<\/td><\/tr><tr><td>@Qualifier<\/td><td>Distinguishes bindings of the same type<\/td><\/tr><tr><td>@Provides<\/td><td>Custom dependency construction<\/td><\/tr><tr><td>@Binds<\/td><td>Interface-to-implementation binding<\/td><\/tr><tr><td>Lazy<\/td><td>Deferred dependency access with caching<\/td><\/tr><tr><td>Provider<\/td><td>On-demand dependency access<\/td><\/tr><tr><td>@AssistedInject<\/td><td>Combines injected and runtime dependencies<\/td><\/tr><tr><td>@TestInstallIn<\/td><td>Replaces production modules in tests<\/td><\/tr><tr><td>@UninstallModules<\/td><td>Removes modules for a test<\/td><\/tr><tr><td>@BindValue<\/td><td>Supplies test dependencies through fields<\/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: Custom Qualifiers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create two qualified Retrofit instances.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 2: Multiple API Services<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Inject separate UserApi and PostApi services.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 3: ViewModelScoped Dependency<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a ViewModel-specific SearchSession.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 4: ActivityRetainedScoped Dependency<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a retained session manager and test configuration changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 5: Lazy Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Delay the construction of an analytics dependency.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 6: Provider Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Request formatter instances through Provider.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 7: Multi-API Dashboard<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build the complete dashboard application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 8: Fake Repository<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Replace network requests with fake data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 9: Hilt Testing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use @TestInstallIn to replace production bindings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 10: Multi-Module Architecture<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Move networking and repository dependencies into separate Gradle modules.<\/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 advanced Hilt Dependency Injection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Advanced Hilt covers dependency scopes, qualifiers, component lifetimes, multiple implementations, assisted injection, testing, and modularization.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between @Singleton and @ViewModelScoped?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">@Singleton shares an instance within SingletonComponent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@ViewModelScoped shares an instance within a particular ViewModelComponent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is @ActivityRetainedScoped?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It scopes dependencies to an Activity lifecycle that survives configuration changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why are qualifiers needed?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Qualifiers distinguish multiple bindings of the same type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can Hilt provide multiple Retrofit instances?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Use qualifiers to identify different Retrofit configurations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between Lazy and Provider?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Lazy caches its first resolved dependency per Lazy instance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Provider requests a dependency on each get() call, subject to the dependency&#8217;s scope.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Hilt support assisted injection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Assisted injection can combine runtime parameters with injected dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I replace dependencies during testing?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use Hilt testing mechanisms such as @TestInstallIn, @UninstallModules, and @BindValue.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Hilt work with multi-module applications?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, provided the dependency graph and module visibility are configured correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should every dependency be scoped?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Scope dependencies only when sharing or lifetime management is needed.<\/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>Hilt component hierarchy<\/li>\n\n\n\n<li>Dependency scopes<\/li>\n\n\n\n<li>SingletonComponent<\/li>\n\n\n\n<li>ActivityRetainedComponent<\/li>\n\n\n\n<li>ViewModelComponent<\/li>\n\n\n\n<li>ActivityScoped dependencies<\/li>\n\n\n\n<li>Custom qualifiers<\/li>\n\n\n\n<li>Multiple Retrofit instances<\/li>\n\n\n\n<li>Repository injection<\/li>\n\n\n\n<li>Practical Multi-API Dashboard<\/li>\n\n\n\n<li>Lazy injection<\/li>\n\n\n\n<li>Provider injection<\/li>\n\n\n\n<li>Assisted injection<\/li>\n\n\n\n<li>Hilt testing<\/li>\n\n\n\n<li>@TestInstallIn<\/li>\n\n\n\n<li>@UninstallModules<\/li>\n\n\n\n<li>@BindValue<\/li>\n\n\n\n<li>Multi-module architecture<\/li>\n\n\n\n<li>Dependency graph debugging<\/li>\n\n\n\n<li>Performance considerations<\/li>\n\n\n\n<li>Production best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Advanced Hilt concepts help developers manage complex dependency graphs in production Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The key principle is simple:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Choose the correct dependency lifetime, use qualifiers when multiple bindings exist, and keep dependency ownership explicit.<\/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 46 \u2014 Retrofit and OkHttp in Android with Kotlin: REST API Integration, Interceptors &amp; Error Handling<\/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>Retrofit fundamentals<\/li>\n\n\n\n<li>REST APIs and HTTP methods<\/li>\n\n\n\n<li>OkHttpClient configuration<\/li>\n\n\n\n<li>Gson vs Kotlin Serialization<\/li>\n\n\n\n<li>API service interfaces<\/li>\n\n\n\n<li>GET, POST, PUT, PATCH, and DELETE<\/li>\n\n\n\n<li>Query parameters and headers<\/li>\n\n\n\n<li>OkHttp interceptors<\/li>\n\n\n\n<li>Authentication headers<\/li>\n\n\n\n<li>Network timeouts<\/li>\n\n\n\n<li>API error handling<\/li>\n\n\n\n<li>Repository integration<\/li>\n\n\n\n<li>Coroutines and StateFlow<\/li>\n\n\n\n<li>Jetpack Compose UI integration<\/li>\n\n\n\n<li>Practical API mini-project<\/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<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","protected":false},"excerpt":{"rendered":"<p>Learn advanced Hilt Dependency Injection in Android using Kotlin and Jetpack Compose. Explore component lifecycles, custom qualifiers, multiple Retrofit instances, Lazy and Provider injection, modularization, and testing.<\/p>\n","protected":false},"author":1,"featured_media":197,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[10,3,333],"tags":[],"class_list":["post-196","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hilt","category-android-development","category-android-tutorials"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/196","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=196"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":198,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/196\/revisions\/198"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/197"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}