Dagger 2注入不适用于@ContributesAndroidInjector

时间:2018-01-25 20:26:31

标签: android dagger

我有这个模块用于我的活动绑定:

@Module
abstract class ActivityBuilder {

    @ActivityScope
    @ContributesAndroidInjector(modules = [BaseActivityModule::class])
    abstract fun bindsActivity(): BaseActivity

    @ActivityScope
    @ContributesAndroidInjector(modules = [MainActivityModule::class])
    abstract fun bindMainActivity(): MainActivity

}

BaseActivityModule

@Module
abstract class BaseActivityModule {

    @Binds
    @ActivityScope
    abstract fun provideProgressDialogManager(manager: ProgressDialogManager): ProgressManager

}

ProgressDialogManager

class ProgressDialogManager : ProgressManager {

    private var activity: Activity;
    private var instance: AtomicReference<ProgressDialog> = AtomicReference<ProgressDialog>();

    @Inject
    constructor(activity: BaseActivity){
        this.activity = activity as Activity
        this.instance.set(getDialog())
    }

我的组件:

@Singleton
@Component(modules = [ApplicationModule::class, AndroidSupportInjectionModule::class, ActivityBuilder::class, ApplicationServiceModule::class, HTTPClientModule::class, ServicesModule::class])
public interface AceleraApplicationComponent : AndroidInjector<DaggerApplication> {

    fun inject(app: AceleraApplication)
    override fun inject(instance: DaggerApplication)

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder
        fun build(): AceleraApplicationComponent

    }

}

但如果我这样做:

abstract class BaseActivity : DaggerAppCompatActivity(), SalesforceActivityInterface, BaseView {

    private var logger: Logger = br.com.rede.acelera.util.Logger.create(javaClass)
    private var sf: SalesforceActivityDelegate = SalesforceActivityDelegate(this)

    @Inject
    lateinit var progress: ProgressManager

}

我有这个构建错误:

  

E:   /Users/rafael-iteris/Documents/afvc/acelera/app/build/tmp/kapt3/stubs/devProdDebug/br/com/rede/acelera/component/AceleraApplicationComponent.java:6:   错误:[dagger.android.AndroidInjector.inject(T)]   如果没有,则无法提供br.com.rede.acelera.util.ProgressManager   @ Provide-或@ Produces-annotated方法。 e:

     

e:公共抽象接口AceleraApplicationComponent扩展   dagger.android.AndroidInjector {e:   ^ e:br.com.rede.acelera.util.ProgressManager注入e:   br.com.rede.acelera.base.BaseActivity.progress e:
  br.com.rede.acelera.activity.main.MainActivity注入e:
  dagger.android.AndroidInjector.inject(arg0)e:与...绑定   组件中存在匹配键:   br.com.rede.acelera.module.ActivityBuilder_BindsActivity.BaseActivitySubcomponent   e:java.lang.IllegalStateException:无法分析:   org.jetbrains.kotlin.kapt3.diagnostic.KaptError:错误的同时   注释处理

任何想法为什么?

2 个答案:

答案 0 :(得分:1)

Dagger开始从子类注入依赖项,即使您的AndroidInjection.inject(this)在您的基类中(或者从DaggerAppCompatActivity延伸)。

然后在ActivityBuilder中查找子类定义。

因此,要注入抽象基类,需要在其每个子类中提供该依赖关系(通过ActivityBuilder中的Module)。

@Module
abstract class ActivityBuilder {

    @ActivityScope
    @ContributesAndroidInjector(modules = [MainActivityModule::class, BaseActivityModule::class])
    abstract fun bindMainActivity(): MainActivity

}

答案 1 :(得分:0)

尝试使用此BaseActivityModule,并从ProgressDialogManager构造函数中删除@Inject注释。

@Module
class BaseActivityModule {

    @Provide
    @ActivityScope
    fun provideProgressDialogManager(activity: BaseActivity): ProgressManager {
       return ProgressDialogManager(activity)
    }
}