How kotlin-inject-anvil works
mainThe project extends kotlin-inject by allowing you to contribute component interfaces and bindings from anywhere in your codebase without needing to explicitly reference them in a central component.
It uses Scopes (marker classes) to connect contributions to a specific merged component. When you use @MergeComponent(Scope::class), the plugin automatically gathers all interfaces marked with @ContributesTo(Scope::class) and all bindings marked with @ContributesBinding(Scope::class) and merges them into the final component.
@ContributesTo(AppScope::class)
interface AppIdComponent {
@Provides
fun provideAppId(): String = "demo app"
}
@Inject
@SingleIn(AppScope::class)
@ContributesBinding(AppScope::class)
class RealAuthenticator : Authenticator
// The final kotlin-inject component.
@MergeComponent(AppScope::class)
@SingleIn(AppScope::class)
interface AppComponent
// Instantiate the component at runtime.
val component = AppComponent::class.create()