Dependency Injection with Hilt in Android app

NOVASOLUTIONS.TECHNOLOGY is engaged in the development, support and maintenance of iOS, Android, PWA mobile applications. We have extensive experience and expertise in publishing mobile applications in popular markets like Google Play, App Store, Amazon, AppGallery and others.
Development and support of all types of mobile applications:
Information and entertainment mobile applications
News apps, games, reference guides, online catalogs, weather apps, fitness and health apps, travel apps, educational apps, social networks and messengers, quizzes, blogs and podcasts, forums, aggregators
E-commerce mobile applications
Online stores, B2B apps, marketplaces, online exchanges, cashback services, exchanges, dropshipping platforms, loyalty programs, food and goods delivery, payment systems.
Business process management mobile applications
CRM systems, ERP systems, project management, sales team tools, financial management, production management, logistics and delivery management, HR management, data monitoring systems
Electronic services mobile applications
Classified ads platforms, online schools, online cinemas, electronic service platforms, cashback platforms, video hosting, thematic portals, online booking and scheduling platforms, online trading platforms

These are just some of the types of mobile applications we work with, and each of them may have its own specific features and functionality, tailored to the specific needs and goals of the client.

Showing 1 of 1 servicesAll 1735 services
Dependency Injection with Hilt in Android app
Medium
~2-3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_mobile-applications_feedme_467_0.webp
    Development of a mobile application for FEEDME
    756
  • image_mobile-applications_xoomer_471_0.webp
    Development of a mobile application for XOOMER
    624
  • image_mobile-applications_rhl_428_0.webp
    Development of a mobile application for RHL
    1052
  • image_mobile-applications_zippy_411_0.webp
    Development of a mobile application for ZIPPY
    947
  • image_mobile-applications_affhome_429_0.webp
    Development of a mobile application for Affhome
    862
  • image_mobile-applications_flavors_409_0.webp
    Development of a mobile application for the FLAVORS company
    445

Dependency Injection (Hilt) in Android App

Hilt—Google's official DI library for Android, built on top of Dagger 2. Removes 80% of Dagger boilerplate, providing ready-made components for standard Android classes. For most new projects—reasonable default choice.

Basic Setup

// build.gradle.kts (project)
plugins {
    id("com.google.dagger.hilt.android") version "2.51" apply false
}

// build.gradle.kts (app)
plugins {
    id("com.google.dagger.hilt.android")
    id("com.google.devtools.ksp")
}

dependencies {
    implementation("com.google.dagger:hilt-android:2.51")
    ksp("com.google.dagger:hilt-android-compiler:2.51")
}

@HiltAndroidApp on Application—entry point, without it Hilt doesn't initialize:

@HiltAndroidApp
class App : Application()

Injection in Android Classes

Hilt knows about Activity, Fragment, Service, BroadcastReceiver, ViewModel, and WorkManager. Each has its own scope:

@AndroidEntryPoint
class ProfileFragment : Fragment() {
    @Inject lateinit var userRepository: UserRepository
    private val viewModel: ProfileViewModel by viewModels()
}

@HiltViewModel
class ProfileViewModel @Inject constructor(
    private val userRepository: UserRepository,
    private val analyticsService: AnalyticsService
) : ViewModel()

@AndroidEntryPoint generates Dagger subcomponent for this class. @HiltViewModel integrates ViewModel with Hilt without manual ViewModelFactory.

Modules and Bindings

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

    @Provides
    @Singleton
    fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder()
        .addInterceptor(HttpLoggingInterceptor().apply {
            level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY
                    else HttpLoggingInterceptor.Level.NONE
        })
        .build()

    @Provides
    @Singleton
    fun provideApiService(client: OkHttpClient): ApiService =
        Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(ApiService::class.java)
}

@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
    @Binds
    abstract fun bindUserRepository(impl: UserRepositoryImpl): UserRepository
}

@InstallIn(SingletonComponent::class) binds module to Hilt's AppComponent. For Activity-level dependencies—ActivityComponent::class, for Fragment—FragmentComponent::class.

Qualifiers and Multiple Bindings

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AuthenticatedClient

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class UnauthenticatedClient

@Module
@InstallIn(SingletonComponent::class)
object HttpModule {
    @Provides @Singleton @AuthenticatedClient
    fun provideAuthenticatedClient(authInterceptor: AuthInterceptor): OkHttpClient =
        OkHttpClient.Builder().addInterceptor(authInterceptor).build()

    @Provides @Singleton @UnauthenticatedClient
    fun provideUnauthenticatedClient(): OkHttpClient = OkHttpClient.Builder().build()
}

Without qualifiers Hilt can't distinguish two OkHttpClient—compilation error [Dagger/DuplicateBindings].

Testing with Hilt

@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class ProfileFragmentTest {

    @get:Rule
    val hiltRule = HiltAndroidRule(this)

    @BindValue
    @JvmField
    val fakeRepository: UserRepository = FakeUserRepository()

    @Test
    fun displaysUserName() {
        // test
    }
}

@BindValue replaces real binding with fake directly in test, without creating test module. For unit tests of ViewModels, Hilt is not needed—dependencies passed to constructor directly.

Common Mistake: @Inject in non-Android Classes Without @AndroidEntryPoint

@Inject in Fragment without @AndroidEntryPoint gives NullPointerException when accessing injected field—field remains null. Hilt doesn't inject into classes without its annotation. Typical crash when copying Fragment from old code to new project.

Setting up Hilt from scratch in new project—1–2 days. Migration from manual DI or Dagger 2—3–7 days depending on project size. Cost is calculated individually.