Skip to content

Scopes and Components

Table of Contents

Auto-detection (Default)

By default, @AutoBinds(installIn=...) is set to HiltComponent.Unspecified. The processor auto-detects the correct component from the scope annotation on the class. If the class has no scope annotation, SingletonComponent is used as the fallback.

// No scope annotation -> SingletonComponent
@AutoBinds
class UserRepository @Inject constructor() : UserDataSource {
    // ...
}

// @ActivityScoped -> ActivityComponent
@ActivityScoped
@AutoBinds
class SearchRepository @Inject constructor(
    private val api: SearchApi,
) : SearchDataSource {
    // ...
}

Explicit Component

You can set the component explicitly via the installIn parameter:

@AutoBinds(installIn = HiltComponent.ViewModel)
class SearchRepository @Inject constructor(
    private val api: SearchApi,
) : SearchDataSource {
    // ...
}

This works with both @AutoBinds and @AutoBindsIntoSet.

Scope Validation

When both a scope annotation and an explicit installIn are present, the processor validates that they match. A mismatch produces a compile error:

// ERROR: @Singleton does not match installIn = Activity
@Singleton
@AutoBinds(installIn = HiltComponent.Activity)
class RepoImpl @Inject constructor() : Repo

This prevents accidental misconfiguration where the scope and the component would contradict each other.

Component and Scope Reference

HiltComponent values

HiltComponent value Dagger component
Unspecified (default) auto-detected from scope
Singleton SingletonComponent
ActivityRetained ActivityRetainedComponent
Activity ActivityComponent
ViewModel ViewModelComponent
Fragment FragmentComponent
View ViewComponent
ViewWithFragment ViewWithFragmentComponent
Service ServiceComponent

Scope auto-detection mapping

Scope annotation Resolved component
@Singleton SingletonComponent
@ActivityRetainedScoped ActivityRetainedComponent
@ActivityScoped ActivityComponent
@ViewModelScoped ViewModelComponent
@FragmentScoped FragmentComponent
@ViewScoped ViewComponent
@ServiceScoped ServiceComponent
(none) SingletonComponent

Note

@ViewScoped maps to ViewComponent by default, not ViewWithFragmentComponent. Use installIn = HiltComponent.ViewWithFragment explicitly if needed.