Interview frequency: …
Short answer
@Binds is a quick abstract function for binding an interface to its direct implementation; @Provides is a regular function used when building the object requires logic or when the object comes from an external library.
Explanation
@Binds is more efficient because Dagger generates no executable code for it in the bytecode; it only links the two types. @Provides is used when you do not have access to the object's constructor (such as Retrofit, RoomDatabase, OkHttpClient) or when conditional initialization steps are needed before returning the object.
Code example
@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
// @Binds To efficiently connect the interface to its implementation:
@Binds
abstract fun bindAuthRepo(impl: AuthRepositoryImpl): AuthRepository
}
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
// @Provides To build an object from an external library:
@Provides
fun provideRetrofit(): Retrofit = Retrofit.Builder().baseUrl("https://api.com/").build()
}Common mistakes
- Using @Provides instead of @Binds to bind simple internal app classes, which generates unnecessary intermediary code and slows compile time.