1

There are many answers in SO regarding the above question, however my case is different, for me the setters for component is missing.

I have ApplicationComponent which depends on some other component [TestComponent] coming from a library.

@AppScope
@Component(
    dependencies = [TestComponent::class],
    modules = [
        AppModule::class,
        AndroidInjectionModule::class
    ]
)
interface AppComponent : AndroidInjector<MyApplication> {

    override fun inject(application: MyApplication)

    @ContextIO
    fun getIOCoroutineContext(): CoroutineContext

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder

        @BindsInstance
        fun testComponent(testComponent: TestComponent): Builder

        fun build(): AppComponent
    }
}

And the application class

class MyApplication : DaggerApplication() {

  val appComponent by lazy {
    DaggerAppComponent.builder()
        .application(this)
        .testComponent(SdkInjectors.testComponent())
        .build()
  }
}


@Singleton
@Component(
    modules = {VideoModule.class, AnalyticsModule.class}
)
public interface TestComponent {

  // ...
}
AndroidDev
  • 1,485
  • 2
  • 18
  • 33

1 Answers1

3

Stumbled upon on some open source project and found this, I was not suppose to use @BindsInstance when creating dependent component.

@AppScope
@Component(
    dependencies = [TestComponent::class],
    modules = [
        AppModule::class,
        AndroidInjectionModule::class
    ]
)
interface AppComponent : AndroidInjector<MyApplication> {

    override fun inject(application: MyApplication)

    @ContextIO
    fun getIOCoroutineContext(): CoroutineContext

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder
      
        fun testComponent(testComponent: TestComponent): Builder

        fun build(): AppComponent
    }
}

Removing @BindsInstance worked, however I still don't know the reason, will update if I get to know.

AndroidDev
  • 1,485
  • 2
  • 18
  • 33