Dependency Injection with Hilt

June 02, 2026 1 min read

As apps grow, manually creating objects everywhere becomes messy. Hilt (built on Dagger) creates and supplies dependencies for you — improving testability and structure.

Set up

@HiltAndroidApp
class MyApp : Application()

@AndroidEntryPoint
class MainActivity : AppCompatActivity() { /* ... */ }

Provide a dependency

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
    @Provides @Singleton
    fun api(): ApiService = Retrofit.Builder()
        .baseUrl("https://api.example.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .build().create(ApiService::class.java)
}

Inject it

@HiltViewModel
class UserViewModel @Inject constructor(
    private val api: ApiService
) : ViewModel() { /* use api */ }
Tip: Start small: inject your repository and API. You don't need to inject everything on day one.

Summary

Hilt wires up dependencies for you. Annotate the app, provide objects in modules, and inject them where needed.