Creating dependent component in dagger 2
我有一个带有
我添加了范围注释
1 2 3 | @Scope @Retention(AnnotationRetention.RUNTIME) annotation class MyTestScope |
然后我为其创建一些类和一个模块
1 2 3 4 5 6 7 8 9 10 11 12 | class A { fun get() = 1 } @Module class TestModule { @Provides @MyTestScope fun provideA(): A { return A() } } |
像这样添加从属组件后
1 2 3 4 5 6 7 8 9 10 11 12 13 | @MyTestScope @Component(dependencies = [AppComponent::class], modules = [TestModule::class]) interface DependentComponent { @Component.Builder interface Builder { @BindsInstance fun appComponent(component: AppComponent): Builder fun build(): DependentComponent } fun inject(application: Application) } |
如果我尝试构建它,则会看到下一个错误
这是我的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @Singleton @Component(modules = [ AndroidInjectionModule::class, ActivityModule::class, // etc .... ]) interface AppComponent { @Component.Builder interface Builder { @BindsInstance fun language(language: Language): Builder @BindsInstance fun appContext(appContext: Context): Builder fun build(): AppComponent } fun inject(application: Application) } |
有什么想法吗?
在为依赖组件创建构建器时,切勿使用
标记主要组件的设置器
1 2 3 4 5 6 7 8 9 10 11 12 | @MyTestScope @Component(dependencies = [AppComponent::class], modules = [TestModule::class]) interface DependentComponent { @Component.Builder interface Builder { fun appComponent(component: AppComponent): Builder fun build(): DependentComponent } fun inject(application: Application) } |