简体   繁体   English

在Android上从Dagger 2.7升级到Dagger 2.21时的Dagger 2构建错误

[英]Dagger 2 build error when upgrading from Dagger 2.7 to Dagger 2.21 on Android

I am attempting to upgrade from Dagger 2.7 to Dagger 2.21 in an Android app. 我正在尝试在Android应用程序中从Dagger 2.7升级到Dagger 2.21。 So far, that has mostly involved adding new scopes to subcomponents, as this is enforced in Dagger 2.8+, but wasn't in Dagger 2.7. 到目前为止,这主要涉及向子组件添加新的作用域,因为这在Dagger 2.8+中得到了实施,但在Dagger 2.7中却没有。 I have been stuck for a few hours on an error that I was hoping someone could help me get past. 我因一个错误被困了几个小时,我希望有人可以帮助我过去。 The error is this: 错误是这样的:

error: [Dagger/MissingBinding] com.experticity.android.member.model.card.survey.SurveyCampaign cannot be provided without an @Inject constructor or an @Provides-annotated method.

I have an @Provides method in a module though: 我在模块中有一个@Provides方法:

@Module
class SurveyPlayerModule(private val surveyCampaign: SurveyCampaign) {

    @Provides
    @FragmentScope
    fun provideSurveyCampaign(): SurveyCampaign {
        return surveyCampaign
    }
}

And I provide that surveyCampaign from my fragment: 我从片段中提供了SurveyCampaign:

getComponent().surveyPlayerFragmentComponent(
                    new SurveyPlayerModule(mSurveyCampaign), new FragmentModule(this));

The class that doesn't seem to be able to get the survey campaign is the SurveyTracker and the constructor looks like this: 似乎无法获得调查活动的类是SurveyTracker ,构造函数如下所示:

@Inject
public SurveyTracker(UserRepository userRepository, CampaignRepository campaignRepository, SurveyCampaign surveyCampaign) {// Set all of the fields from the constructor parameters}

And yes, our project is a mix of Kotlin and Java, moving gradually towards more and more Kotlin. 是的,我们的项目是Kotlin和Java的结合,并逐渐向越来越多的Kotlin发展。

And the rest of the error message, with package names removed for brevity: 错误消息的其余部分,为简洁起见,已删除软件包名称:

SurveyCampaign is injected at SurveyTracker(…, surveyCampaign)

SurveyTracker is provided at SurveyPlayerFragmentComponent.surveyTracker() [ApplicationComponent → SessionComponent → RepositoryComponent → activity.ActivityComponent → activity.SurveyPlayerFragmentComponent]

  The following other entry points also depend on it:
      SurveyPlayerFragmentComponent.inject(SurveyPlayerViewModel) [ApplicationComponent → SessionComponent → RepositoryComponent → activity.ActivityComponent → activity.SurveyPlayerFragmentComponent]

      SurveyPlayerFragmentComponent.inject(SurveyQuestionViewModel) [ApplicationComponent → SessionComponent → RepositoryComponent → activity.ActivityComponent → activity.SurveyPlayerFragmentComponent]

      SurveyPlayerFragmentComponent.inject(SurveyCompletedViewModel) [ApplicationComponent → SessionComponent → RepositoryComponent → activity.ActivityComponent → activity.SurveyPlayerFragmentComponent]

The full, un-edited error stack: 完整的,未编辑的错误堆栈:

ApplicationComponent.java:11: error: [Dagger/MissingBinding] com.experticity.android.member.model.card.survey.SurveyCampaign cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract interface ApplicationComponent {
                ^
      com.experticity.android.member.model.card.survey.SurveyCampaign is injected at
          com.experticity.android.member.domain.SurveyTracker(…, surveyCampaign)
      com.experticity.android.member.domain.SurveyTracker is injected at
          com.experticity.android.member.ui.viewmodel.SurveyPlayerViewModel.mSurveyTracker
      com.experticity.android.member.ui.viewmodel.SurveyPlayerViewModel is injected at
          com.experticity.android.member.injection.component.activity.SurveyPlayerFragmentComponent.inject(com.experticity.android.member.ui.viewmodel.SurveyPlayerViewModel) [com.experticity.android.member.injection.component.ApplicationComponent → com.experticity.android.member.injection.component.SessionComponent → com.experticity.android.member.injection.component.RepositoryComponent → com.experticity.android.member.injection.component.activity.ActivityComponent → com.experticity.android.member.injection.component.activity.SurveyPlayerFragmentComponent]
  The following other entry points also depend on it:
      com.experticity.android.member.injection.component.activity.SurveyPlayerFragmentComponent.inject(com.experticity.android.member.ui.viewmodel.SurveyQuestionViewModel) [com.experticity.android.member.injection.component.ApplicationComponent → com.experticity.android.member.injection.component.SessionComponent → com.experticity.android.member.injection.component.RepositoryComponent → com.experticity.android.member.injection.component.activity.ActivityComponent → com.experticity.android.member.injection.component.activity.SurveyPlayerFragmentComponent]
      com.experticity.android.member.injection.component.activity.SurveyPlayerFragmentComponent.inject(com.experticity.android.member.ui.viewmodel.SurveyCompletedViewModel) [com.experticity.android.member.injection.component.ApplicationComponent → com.experticity.android.member.injection.component.SessionComponent → com.experticity.android.member.injection.component.RepositoryComponent → com.experticity.android.member.injection.component.activity.ActivityComponent → com.experticity.android.member.injection.component.activity.SurveyPlayerFragmentComponent]

And here is the component: 这是组件:

@FragmentScope
@Subcomponent(modules = [SurveyPlayerModule::class, FragmentModule::class])
interface SurveyPlayerFragmentComponent {

    @ChildFragmentManager
    fun childFragmentManager(): FragmentManager

    fun surveyTracker(): SurveyTracker

    //Fragments

    fun inject(fragment: SurveyPlayerFragment)

    // ViewModels

    fun inject(viewModel: SurveyPlayerViewModel)

    fun inject(viewModel: SurveyQuestionViewModel)

    fun inject(viewModel: SurveyCompletedViewModel)

    // Adapters

    fun inject(adapter: SurveyPlayerAdapter)
}

I finally figured it out. 我终于弄明白了。 I kept on thinking, "It feels like SurveyTracker and SurveyPlayerFragment (which actually creates the module and passes the surveyCampaign to the module) are not in the same scope." 我一直在想:“感觉SurveyTrackerSurveyPlayerFragment (实际上创建了模块并将surveyCampaign传递给模块)不在同一范围内。” I was thinking about this from a non-Dagger perspective, but it turns out that the SurveyTracker class was annotated with the @ActivityScope scope instead of @FragmentScope like everything else in the component and module. 我从非Dagger的角度考虑这个问题,但事实证明SurveyTracker类使用@ActivityScope范围而不是@FragmentScope进行了注释,就像组件和模块中的其他所有内容一样。 Changing it to @FragmentScope fixed it. 将其更改为@FragmentScope修复它。 For some reason I had not thought of checking for annotations on the SurveyTracker , because I didn't think that was something you would do. 由于某些原因,我没有想到要在SurveyTracker上检查注解,因为我不认为那是您可以做的。 Thank you for your help, David. 大卫,谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM