Android Kotlin中的Dagger 2作用域和依赖项
我希望将Dagger 2添加到现有的应用程序中,一旦我降低了2个级别,就会遇到组件依赖性和范围问题。我的思维过程是拥有一个Android Kotlin中的Dagger 2作用域和依赖项,android,dependency-injection,dagger-2,dagger,Android,Dependency Injection,Dagger 2,Dagger,我希望将Dagger 2添加到现有的应用程序中,一旦我降低了2个级别,就会遇到组件依赖性和范围问题。我的思维过程是拥有一个AppComponent、LandingActivityComponent和LandingFragmentComponent(现在不是很好的名字,但我们还很早)分别具有作用域@Singleton、@ActivityScope和@FragmentScope。如果我停止活动,我所做的一切都会起作用,但一旦我添加了片段级别,我就会出现以下错误: e: /Users/me/git/m
AppComponent
、LandingActivityComponent
和LandingFragmentComponent
(现在不是很好的名字,但我们还很早)分别具有作用域@Singleton
、@ActivityScope
和@FragmentScope
。如果我停止活动,我所做的一切都会起作用,但一旦我添加了片段级别,我就会出现以下错误:
e: /Users/me/git/myapp-android/myapp/build/tmp/kapt3/stubs/regularDebug/tv/myapp/android/app/core/dagger/LandingActivityComponent.java:17: error: com.myapp.android.app.core.LandingActivity cannot be provided without an @Inject constructor or from an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
public abstract com.myapp.android.app.core.LandingActivity landingActivity();
^
com.myapp.android.app.core.LandingActivity is provided at
com.myapp.android.app.core.dagger.LandingActivityComponent.landingActivity()
e: /Users/me/git/myapp-android/myapp/build/tmp/kapt3/stubs/regularDebug/tv/myapp/android/app/core/dagger/LandingFragmentComponent.java:13: error: com.myapp.android.app.settings.QuickSettingsPresenter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
public abstract void inject(@org.jetbrains.annotations.NotNull()
^
com.myapp.android.app.settings.QuickSettingsPresenter is injected at
com.myapp.android.app.profile.ProfileViewPagerFragment.mQuickSettingsPresenter
com.myapp.android.app.profile.ProfileViewPagerFragment is injected at
com.myapp.android.app.core.dagger.LandingFragmentComponent.inject(profileViewPagerFragment)
应用程序
@Module
class AppModule(private val app: MyApplication) {
@Provides @Singleton
fun provideApp(): MyApplication = app
@Provides @Singleton
fun provideContext(): Context = app.applicationContext
@Provides @Singleton
fun provideAccountManager(): AccountManager = AccountManager(app)
}
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
fun inject(application: MyApplication)
// Allow dependents to see the properties provided below
fun accountManager(): AccountManager
}
活动
@Scope
@Qualifier
@Retention(RUNTIME)
annotation class ActivityScope
@Module
class LandingActivityModule(private val landingActivity: LandingActivity) {
@Provides @ActivityScope
fun provideLandingActivity(): LandingActivity = landingActivity
}
@ActivityScope
@Component(dependencies = [AppComponent::class], modules = [LandingActivityModule::class])
interface LandingActivityComponent {
fun inject(landingActivity: LandingActivity)
// Allow dependents to see the properties provided below
fun landingActivity(): LandingActivity
fun accountManager(): AccountManager
}
片段
@Scope
@Qualifier
@Retention(RUNTIME)
annotation class FragmentScope
@Module
class LandingFragmentModule(private val landingFragment: LandingFragment) {
@Provides @FragmentScope
fun provideFragment(): LandingFragment = landingFragment
@Provides @FragmentScope
fun provideQuickSettings(activity: LandingActivity): QuickSettingsPresenter =
QuickSettingsPresenter.create(activity)
}
@FragmentScope
@Component(dependencies = [LandingActivityComponent::class], modules = [LandingFragmentModule::class])
interface LandingFragmentComponent {
fun inject(profileViewPagerFragment: ProfileViewPagerFragment)
}
我觉得我可能遗漏了一些基本的东西,或者需要有点不同的结构,但我认为这突出了我的目标
有趣的是,如果我删除活动和片段模块中的@提供的@范围注释,一切都很好。正如我所提到的,如果我删掉片段模块,我可以确定活动的范围,没有问题
这里的最终目标是让这些组件与演示者一起工作,但我会一步一步地进行。我不熟悉Dagger中的非单片组件,还没有找到让我点击的指南或帖子。(代表问题作者发布)
我知道发生了什么事。在这过程中的某个地方,我读到一些东西,让我相信我需要@Qualifier
作为我的作用域的一部分,但事实并非如此。移除它似乎解决了我的问题。将此作为一种基于依赖关系的方法留在这里,供任何人查看