Skip to content

Hilt AutoBind

Maven Central License PR Check Coverage Publish

Auto-generate Dagger Hilt binding modules with a single annotation. Stop writing repetitive @Module / @Binds / @Provides boilerplate.

Quick Start

Add dependencies to your module's build.gradle.kts:

dependencies {
    implementation("com.uandcode:hilt-autobind:0.8.0")
    ksp("com.uandcode:hilt-autobind-compiler:0.8.0")
}

Requires Hilt and KSP. See Installation for full setup instructions.

Annotate your implementation class:

interface UserRepository {
    suspend fun getUser(id: Long): User
}

@AutoBinds
class UserRepositoryImpl @Inject constructor(
    private val api: UserApi,
    private val db: UserDao,
) : UserRepository {
    override suspend fun getUser(id: Long): User {
        return db.getUser(id) ?: api.fetchUser(id).also { db.insert(it) }
    }
}

The KSP processor generates a Hilt @Binds module for you:

@Module
@InstallIn(SingletonComponent::class)
internal interface UserRepositoryImplModule {
    @Binds
    fun bindToUserRepository(impl: UserRepositoryImpl): UserRepository
}

Manual modules are not needed anymore, and Hilt can now inject UserRepository anywhere.

Features

Feature Annotation / parameter Description
Basic binding @AutoBinds Generates @Binds modules for interface implementations
Object binding @AutoBinds Generates @Provides modules for Kotlin object declarations
Scopes & components installIn Auto-detects or explicitly sets the Hilt component
Custom components installInCustomComponent Installs bindings into custom Hilt components defined with @DefineComponent
Class factory @AutoBinds(factory = ...) Delegates instance creation to a ClassBindingFactory (e.g., Retrofit)
Delegate factory @AutoBinds(factory = ...) Provides a class and its sub-dependencies via DelegateBindingFactory (e.g., Room)
Set multibinding @AutoBindsIntoSet Contributes to a Dagger Set multibinding
Map multibinding @AutoBindsIntoMap Contributes to a Dagger Map multibinding with a @MapKey-annotated key
Qualifiers @Named / custom @Qualifier Distinguishes multiple bindings of the same type with JSR-330 qualifier annotations
Annotation aliases @AutoBinds as meta-annotation Define custom annotations that act as short aliases for @AutoBinds
Multi-Module Projects - Per-module setup in multi-module projects

Comparison with Similar Libraries

Feature Hilt AutoBind hilt-binder android-hilt
Basic @Binds generation
Scope / component auto-detection
Qualifiers
Supertype selection
Multibinding
Custom Hilt components
Kotlin object binding
Auto-binding to multiple types
Combine basic-/multi- binding
Binding by class (e.g. Retrofit)
Binding by delegate (e.g. Room) ⚠️ (@FactoryMethod)
Annotation aliases

Troubleshooting (KSP build failures)

If you encounter KSP errors when building a project that uses Hilt/Dagger or this library, try the following steps in order:

  1. Stop the Gradle daemon and rebuild:
./gradlew --stop

Then trigger a build again from Android Studio or the command line.

  1. If the error persists - clean and rebuild:

In Android Studio: Build -> Clean Project, then Build -> Rebuild Project.

Or from the command line:

./gradlew clean assembleDebug

Stale KSP caches and daemon state are a common cause of spurious build failures after dependency or annotation changes.

Requirements

Dependency Minimum version
Kotlin 2.0+
KSP 2.0+
Dagger Hilt 2.50+
Android Gradle Plugin 8.0+