Hilt basics

Architecture and dependency injection Mid-level Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Fill the blank

Bind the interface to its implementation without writing a factory.

@Module
@InstallIn(SingletonComponent::class)
abstract class DataModule {
    @____
    abstract fun bindArticles(impl: OfflineArticles): Articles
}
Options
Hint 1 of 1

The function is abstract.

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Binds

Correct. @Binds maps the interface to the implementation Hilt already knows how to build.

Why not the others?
  • B Provides. @Provides needs a function body that builds the instance.
  • C Inject. @Inject goes on constructors or fields.
  • D Singleton. That's a scope, not a binding.

30-second answer

Annotate the Application with @HiltAndroidApp, Android entry points with @AndroidEntryPoint, and ViewModels with @HiltViewModel. @Inject constructor tells Hilt how to build your own classes. For types you don't own, like Retrofit, or for interfaces, write a @Module installed in a component with @Provides functions or @Binds for interface-to-implementation.

Practise saying it · 35 s. No recording, just a timer

Explain Hilt setup.

Say your answer out loud, then check which of these points you covered:

  • @HiltAndroidApp, @AndroidEntryPoint, @HiltViewModel
  • @Inject constructor
  • Modules with @Provides and @Binds
  • @ApplicationContext

Deep dive

@Binds is an abstract function in an abstract module; it's cheaper than @Provides for interfaces.

@InstallIn(SingletonComponent::class) makes bindings available app-wide.

Use @ApplicationContext to inject the application Context safely.

Hilt uses KSP or kapt to generate the Dagger code at build time.

A Hilt ViewModel Compile-checked · Kotlin 2.4.21
interface Articles { fun titles(): List<String> }

class OfflineArticles @Inject constructor() : Articles {
    override fun titles() = listOf("Hello")
}

@Module
@InstallIn(SingletonComponent::class)
abstract class ArticlesModule {
    @Binds abstract fun bind(impl: OfflineArticles): Articles
}

@HiltViewModel
class ArticlesViewModel @Inject constructor(
    articles: Articles,
    savedStateHandle: SavedStateHandle,
) : ViewModel() {
    val titles = articles.titles()
}

Common mistakes

  • Forgetting @AndroidEntryPoint on the Activity hosting a Hilt Fragment.
  • Using @Provides for every interface instead of @Binds.
  • Injecting an Activity Context into a singleton.

Senior take & follow-ups

Show the four annotations and one module; then mention @Binds.

Follow-up questions

Think of your answer first, then open the hint.

  1. @Binds vs @Provides?

    Show hint

    @Binds maps an interface to an implementation; @Provides runs code to build an instance.

  2. How do you get a Hilt ViewModel in Compose?

    Show hint

    hiltViewModel() from androidx.hilt:hilt-lifecycle-viewmodel-compose (moved there from hilt-navigation-compose in Hilt 1.3.0).

Suggest a fix if something here is wrong or out of date.