3

I am using dagger 2.11

Module

@Module
class MyModule {
    @Provides
    fun provideString() : String = "yo"
    @Provides @Named("injector")
    fun provideInzectorString() : String = "named_injection"

    @Singleton @Provides //The error goes away if I remove @Singleton
    fun provideRepository() = Repository(Interceptor(),"")
}

Activity binding module

@Module
abstract class ActivityBindingModule {
    @ContributesAndroidInjector(modules = [MyModule::class])
     abstract fun suggestionActivity() : SuggestionsActivity

    @ContributesAndroidInjector(modules = [MyModule::class])
    abstract fun editSubscriptionActivity() : SubscribeActivity
}

AppComponent

@Singleton
@Component(modules = {
        AndroidInjectionModule.class,
        MyModule.class
})
interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(MyApplication application);

        AppComponent build();
    }

    void inject(MyApplication app);
}

I get this error on compilation

SubscribeActivitySubcomponent (unscoped) may not reference scoped bindings:
      @Singleton @Provides @org.jetbrains.annotations.NotNull

I have seen these solutions 1 and 2. Both ask you to annotate your appcomponent with @Singleton which I am already doing. What is wrong with my code?

Ashwin
  • 12,691
  • 31
  • 118
  • 190

1 Answers1

8

The problem is MyModule's scope(Application or singleton) is bigger than an activity scope.

 @ContributesAndroidInjector(modules = [MyModule::class])
 abstract fun suggestionActivity() : SuggestionsActivity

Remove the two (modules = [MyModule::class])s or define activity specific modules.

You don't need MyModule here. It's redundant because it's already included in the AppComponent.

Dewey Reed
  • 4,353
  • 2
  • 27
  • 41