Search code examples
androidkotlindagger-2

Dagger 2 @Component.Builder dependencies missing setters error


I am trying to instantiate Dagger using some external parameters (I am writing a library) but the compiler keeps giving me the following error:

error: @Component.Builder is missing setters for required modules or components: [com.example.domain.LibraryClient.Dependency]

Dependency is a simple interface which I use to get the Application in order to create room.

   interface Dependency {
        fun getApplication(): Application
    }

This is my library component:

@Singleton
@Component(
        modules = [AndroidInjectionModule::class, AndroidSupportInjectionModule::class, LibraryModule::class],
        dependencies = [LibraryClient.Dependency::class])
    interface LibraryComponent {

        @Component.Builder
        interface Builder {
            @BindsInstance
            fun client(client: LibraryClientImpl): Builder

            fun build(): LibraryComponent
        }

        fun inject(target: LibraryClientImpl)
    }

and this is the injector:

object LibraryInjector {

    private var deps: LibraryClient.Dependency? = null

    fun setDependency(dependency: LibraryClient.Dependency) {
        this.deps = dependency
    }

    fun init(client: LibraryClientImpl): LibraryComponent {

    val appComponent = DaggerLibraryComponent.builder()
            .dependency(deps)
            .client(client)
            .build()

        appComponent.inject(client)

        return appComponent
    }
}

I have seen some examples that they use the dependencies option from @Component and it seems right. If I delete dependencies = [LibraryClient.Dependency::class] it passes the build. What am I missing?


Solution

  • Since you have dependencies = [LibraryClient.Dependency::class] as @Component dependency you have to add a corresponding @Component.Builder method (setter) too.

    interface Builder {
       ...
       fun libraryClient(libraryClient: LibraryClient.Dependency): Builder
       ...
    }