Skip to content

@OptionallyProvided Usage

Denotes that the annotated type might not be provided to the dependency graph.

This will generate a @BindsOptionalOf binding for the annotated type.

This is useful when the type will only be provided under certain circumstances. For example an Android app might have some debug settings that are only available in debuggable builds.

@OptionallyProvided
interface DebugApi

// This generates
@BindsOptionalOf
abstract fun bindsOptionalDebugApi(): DebugApi

// Then in your implementation module
@AutoBind
@Singleton
class RealDebugApi @Inject constructor() : DebugApi

Component

AutoDagger will try to infer the component to install the binding in using the scope. The following mapping is used:

Scope Component
No scope SingletonComponent
Singleton SingletonComponent
Reusable SingletonComponent
ActivityRetainedScoped ActivityRetainedComponent
ActivityScoped ActivityComponent
FragmentScoped FragmentComponent
ServiceScoped ServiceComponent
ViewScoped ViewComponent
ViewModelScoped ViewModelComponent
ViewWithFragmentScoped ViewWithFragmentComponent

If you are are using custom scopes or want to change which component the binding is installed in, you can use the inComponent property:

@OptionallyProvided(inComponent = SomeComponent::class)
interface DebugApi

Qualifiers

Any Qualifiers on the annotated type will be carried over to the binding:

@Named("Prod")
@OptionallyProvided
interface Authenticator

// Generates the equivalent to:
@BindsOptionalOf
@Named("Prod")
abstract fun bindsOptionalAuthenticator(): Authenticator