mars885/hilt-binder

Crash when installing a binding in a custom component

Closed this issue · 0 comments

For example:

// A custom component's scope annotation
@Scope
@Retention(value = AnnotationRetention.RUNTIME)
annotation class CustomScope

// Declaration of a custom component itself
@CustomScope
@DefineComponent(parent = SingletonComponent::class)
interface CustomComponent

interface ImageLoader
interface Logger

@CustomScope
@BindType
class PicassoImageLoader @Inject constructor(): ImageLoader

@BindType(
 installIn = BindType.Component.CUSTOM,
 customComponent = CustomComponent::class
)
class AndroidLogger @Inject constructor(): Logger

If the CustomScope annotation is declared in different package than the CustomComponent interface, the following error is thrown when building the project: Collection contains no element matching the predicate..

The above bug entails that having only custom scope annotation present on the binding type is not enough, since it's impossible to infer a class of the custom component from the scope annotation itself. This means that the installIn and the customComponent parameters should be set regardless if the type is scoped or unscoped. For example:

// A custom component's scope annotation
@Scope
@Retention(value = AnnotationRetention.RUNTIME)
annotation class CustomScope

// Declaration of a custom component itself
@CustomScope
@DefineComponent(parent = SingletonComponent::class)
interface CustomComponent

interface ImageLoader
interface Logger

// Binding unscoped type
@BindType(
 installIn = BindType.Component.CUSTOM,
 customComponent = CustomComponent::class
)
class PicassoImageLoader @Inject constructor(): ImageLoader

// Binding scoped type
@CustomScope
@BindType(
 installIn = BindType.Component.CUSTOM,
 customComponent = CustomComponent::class
)
class AndroidLogger @Inject constructor(): Logger

// Won't work, can't infer CustomComponent from CustomScope
// @CustomScope
// @BindType
// class AndroidLogger @Inject constructor(): Logger