I have a question and maybe someone with experience can make things a little more clear for me. I have written Hilt
modules both as interfaces or as abstract classes. Is there an actual difference between those two? For example, I have changed the following module in Hilt
on my app and it still working.
@Module
@InstallIn(ViewModelComponent::class)
abstract class LocalModules {
@Binds
@ViewModelScoped
abstract fun bindTemporaryImageFileFactory(factory: TemporaryImageFileFactoryImpl): TemporaryImageFileFactory
}
or
@Module
@InstallIn(ViewModelComponent::class)
interface LocalModules {
@Binds
@ViewModelScoped
fun bindTemporaryImageFileFactory(factory: TemporaryImageFileFactoryImpl): TemporaryImageFileFactory
}
There is no difference between using interface
or abstract class
when @Binds
is used. These @Module
classes' @Binds
methods are used by Dagger only to understand which implementation class (TemporaryImageFileFactoryImpl
) should be linked to the interface (TemporaryImageFileFactory
).
Using this information, Dagger generates its own code to link the interface with the implementation. After successful build, in IDE you can see that no matter what you use, the @Binds
method will show as unused.