1

I have a Component to provide dependencies for Data Input:

@Component(modules = [DataInputModule::class])
interface DataInputComponent {
    fun inject(activity: DataInputActivity)

    fun factory(): Factory

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance activity: DataInputActivity, module: DataInputModule): DataInputComponent
    }
}

With the module:

@Module
class DataInputModule(private val activity: DataInputActivity) {
    @Provides
    fun provideDataInputViewModel(repo: MyRepository, timestampProvider: TimestampProvider) =
        DefaultDataInputViewModel(repo, timestampProvider)

    @Provides
    fun provideTimestampProvider(): TimestampProvider = DefaultTimestampProvider()

    @Provides
    fun provideDateTimeDialogFactory(): DateTimeDialogFactory = DateTimeDialogFactory(activity)
}

My application's component looks like so:

@Singleton
@Component(
    modules = [
        DataListModule::class
    ]
)
interface AppComponent : AndroidInjector<MyApplication> {
    @Component.Factory
    interface Factory {
        fun create(@BindsInstance app: MyApplication): AppComponent
    }
}

with the module to provide the trusty Room database and the repository.

When trying to build I get the following error:

D:\Applications\AndroidStudioProjects\MyApp\app\build\tmp\kapt3\stubs\debug\com\example\myapp\di\component\DataInputComponent.java:7: error: [Dagger/MissingBinding] com.example.myapp.di.component.DataInputComponent.Factory cannot be provided without an @Provides-annotated method. public abstract interface DataInputComponent { ^ com.example.myapp.di.component.DataInputComponent.Factory is provided at com.example.myapp.di.component.DataInputComponent.factory()

I cannot add a @Provides to the @Component because it is not a module. I looked for docs online but found no example of anyone "provides" annotating and providing a @Factory. So what could cause the issue here?

agiro
  • 2,018
  • 2
  • 30
  • 62
  • A `component` cannot provide a factory to build an instace of itself. If you already have a DataInputComponent what would you use a DataInputComponent.Factory for? Did you intend to make DataInputComponent a SubComponent of AppComponent and have a DataInputComponent.Factory provided by AppComponent? – al3c Feb 20 '20 at 16:09
  • The factory is defined within Component but is not accessed by having a `DataInputComponent` instance. See here: https://dagger.dev/api/2.22.1/dagger/Component.Factory.html I defined the component just like them. Now, the `factory()` method is indeed unnecessary in my case, just added that out of desparation. Without it the same problem persists. – agiro Feb 20 '20 at 17:48
  • 1
    In general you can't inject `Factory`s of `Components`. You should create the component yourself using `DaggerYourComponentName.Factory().create(...)` and then use the componet to inject/provide objects. Note that this is different for `Factory`s of `@SubComponent` those can be injected within the scope of their parent `Component`. – al3c Feb 21 '20 at 10:19
  • @al3c true, only seen it being used as you say so. But my problem is that my code doesn't even compile; I'm nowhere near actually using it. – agiro Feb 21 '20 at 20:08
  • Well if you remove the code where you try to inject `DataInputComponent.Factory` your code should, at least, give a different error. – al3c Feb 23 '20 at 18:18

0 Answers0