I am currently writing a ksp processor which gener...
# dagger
k
I am currently writing a ksp processor which generates
FooImpl
for an interface
Foo
when annotated. Now I want dagger to know binding
Foo
with
FooImpl
from my generated code. Adding simple
@Inject
to the constructor of generated class doesn’t work, I guess because the code is not available at compile time. How can I achieve this?
Was curious if I could use anvil for this? cc: @ralf
p
Add it in a separate module. Then it will be compiled when dagger processes it
As a second step you can use anvil to generate the factories
m
You should create a module that binds the interface to the implementation.
Copy code
@Module
interface FooModule {
  @Binds
  fun FooImpl.bindFoo(): Foo
}
You may or may not want to generate the module code too. After that, you need to connect the module to the dagger
Component
. If you use
Anvil
you can generate
FooImpl
with
@ContributesBinding
instead, and Anvil will generate the module for you. If you use
Hilt
you can use
InstallIn
in the module to automatically connect to a given
Component
.