question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ContributesAndroidInjector in a Subcomponent

See original GitHub issue

Is there a way to add ContributesAndroidInjector for activity in a Subcomponent? I am trying to create a scope that works across a few activities.

What I have is this:

AppComponent

@Component(modules = [
    AndroidInjectionModule::class,
    AndroidSupportInjectionModule::class
])
interface AppComponent: AndroidInjector<DaggerApplication> {
    override fun inject(instance: DaggerApplication)
    fun chatComponent(): ChatComponent.Builder
    fun inject(needle: Needle)

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): AppComponent.Builder
        fun build(): AppComponent
    }
}

ChatScope

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ChatScope

ChatComponent

@Subcomponent(modules = [ChatBindingModule::class])
@ChatScope
interface ChatComponent {

    @Subcomponent.Builder
    interface Builder {
        fun chatModule(chatModule: ChatModule): Builder
        fun build(): ChatComponent
    }

}

ChatBindingModule

@Module
abstract class ChatBindingModule {
    @ActivityScoped
    @ContributesAndroidInjector(modules = [])
    abstract fun chatListActivity(): ChatListActivity

    @ActivityScoped
    @ContributesAndroidInjector(modules = [])
    abstract fun chatDetailActivity(): ChatDetailActivity
}

With this code dagger fails to find injector for both activities. I guess I do know why that is happening but no idea how to solve this problem

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
ronshapirocommented, Aug 30, 2018

You’d need to provide custom code in your Application instance that would know how to access ChatComponent. Something like this:

class MyApplication : DaggerApplication {
  override fun activityInjector(): AndroidInjector<Activity> {
      return object : AndroidInjector<Activity> {
        override fun inject(activity: Activity) {
            if (activity.isChatActivity()) { 
              component.chatComponent().activityInjector().inject(activity)
            } else {
              component.activityInjector().inject(activity)
         }
     }
  }
}
0reactions
mladen1nfcommented, Feb 9, 2021

It seems there is no elegant way of doing this, so at the end I implemented it with standard Dagger approach

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dagger subcomponent with contributor - Stack Overflow
@ContributesAndroidInjector.modules is a way for talking to the generated subcomponent. So the relationship between Activity and FeatureFragment ...
Read more >
Dagger 2 with ContributesAndroidInjector - Is it easy? - Medium
The injector is implemented with a dagger. Subcomponent and will be a child of the dagger. Module's component.
Read more >
ContributesAndroidInjector - Dagger
Generates an AndroidInjector for the return type of this method. The injector is implemented with a Subcomponent and will be a child of...
Read more >
Dagger 2 Annotations: @Binds & @ContributesAndroidInjector
Dagger Android introduced an annotation which can reduce the Component Binds IntoSet Subcomponent ActivityKey FragmentKey etc. boilerplate for ...
Read more >
Dagger by Tutorials, Chapter 16: Dagger & Android | Kodeco ...
When to use @Component s or @Subcomponent s to manage dependencies between objects with different @Scope s. ... How to use @ContributesAndroidInjector ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found