Dagger AutoModule is an annotation processor which allows to generate a default implementation for Dagger modules that are meant to bind interfaces with implementations. And yes, Hilt is supported too!
Stop writing bindings boilerplate code!
This project is distributed using JitPack.
Please make sure to add its repository to your project's build.gradle
.
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Then, you need to import AutoModule's annotations and annotation processor packages.
In your module's build.gradle
add the following imports:
implementation "com.github.damianogiusti.dagger-automodule:automodule-annotations:<version>"
kapt "com.github.damianogiusti.dagger-automodule:automodule-processor:<version>"
AutoModule generates a Dagger module containing bindings for interfaces with their implementors.
First, you need to create an empty placeholder module, that will be used as a target for bindings.
@Module
interface SampleModule {
}
Then, you need to annotate a class that implements an interface with the @AutoModule
annotation.
interface GreetingsProvider {
fun greet(): String
}
@AutoModule(SampleModule::class)
class ProgrammerGreetingsProvider @Inject constructor() : Greeter {
override fun greet(): String = "Hello, world!"
}
This will generate a new Dagger module called AutoModuleSampleModule
that will contain
the binding for the class and the parent interface:
// GENERATED
import dagger.Binds
import dagger.Module
@Module
interface AutoModuleSampleModule {
@Binds
fun bindsProgrammerGreetingsProvider(impl: ProgrammerGreetingsProvider): GreetingsProvider
}
⚠️ AutoModule only supports classes with a single parent interface.
Last, you need to use the generated module as parent interface of your target module, like so:
@Module
interface SampleModule : AutoModuleSampleModule {
}
Add your module to your desired Component, or use the @InstallIn
annotation provided by Hilt.
Damiano Giusti - damianogiusti.com