Hilt AutoBind¶
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.6.0")
ksp("com.uandcode:hilt-autobind-compiler:0.6.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 |
| Scopes & components | installIn |
Auto-detects or explicitly sets the Hilt component |
| 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 | ✅ | ✅ | ❌ |
| Auto-binding to multiple types | ✅ | ❌ | ❌ |
| Combine basic-/multi- binding | ✅ | ❌ | ❌ |
| Binding by class (e.g. Retrofit) | ✅ | ❌ | ❌ |
| Binding by delegate (e.g. Room) | ✅ | ❌ | ⚠️ (@FactoryMethod) |
| Annotation aliases | ✅ | ❌ | ❌ |
| Custom Hilt components | ❌ (Issue #18) | ✅ | ✅ |
Requirements¶
| Dependency | Minimum version |
|---|---|
| Kotlin | 2.0+ |
| KSP | 2.0+ |
| Dagger Hilt | 2.50+ |
| Android Gradle Plugin | 8.0+ |