如何使用dagger.android @Inject一个抽象的BaseActivity及其子类?

时间:2018-01-30 13:36:30

标签: android dependency-injection kotlin dagger-2

我正在使用Dagger 2中新的dagger.android软件包在我的项目中注入Android依赖项。

  • 我需要将所有活动都作为抽象BaseActivity
  • 的子类
  • 在我的BaseActivity中,我要注入成员变量。这样:

    abstract class BaseActivity : AppCompatActivity() {
        @Inject
        lateinit var prefs: MyPreferenceDataStore
        ...// more @Injected members
    }
    
  • 我这样做是因为我希望BaseActiviy的子类可以访问BaseActivity的注入成员:

    class SubClassActivity : BaseActivity() {
        override fun onCreate(savedInstanceState: Bundle) {                
            val x = prefs.getXXX //use prefs variable from parent class
        }
    }
    
  • 这是我的ApplicationComponent:

    @Singleton @Component(modules = arrayOf(
                    ApplicationModule::class,
                    ActivityBindingModule::class,
                    AndroidSupportInjectionModule::class
                ))
    interface ApplicationComponent {
        @Component.Builder interface Builder {
            @BindsInstance
            fun application(application: Application): Builder
            fun build(): ApplicationComponent
        }
        fun inject(app: AndroidApplication)
    }
    
  • ApplicationModule类具有简单的@Provides注释方法:

    @Module
    class ApplicationModule {
        @Singleton @Provides
        fun providesMyPreferenceDataStore(context: Context): MyPreferenceDataStore {
            return MyPreferenceDataStoreImpl(context)
        }
        // more @Provides annotated methods
    }
    

    我认为问题出在我的ActivityBindingModule

    @Module
    abstract class ActivityBindingModule {
    
        @PerActivity
        @ContributesAndroidInjector(
            modules = arrayOf(BaseActivityModule::class
        ))
        abstract fun bindBaseActivity(): BaseActivity
    
        @PerActivity
        @ContributesAndroidInjector(
            modules = arrayOf(
                BaseActivityModule::class
        ))
        abstract fun bindSubClassActivity(): SubClassActivity
    }
    

这是我到目前为止所尝试的:

  • 使bindSubClassActivity()方法不依赖BaseActivityModule::class,不起作用。

  • providesMyPreferenceDataStoreApplicationModule移至BaseActivityModule,以便该课程为:

    @Module
    class BaseActivityModule {
        @PerActivity @Provides
        fun providesMyPreferenceDataStore(context: Context): MyPreferenceDataStore {
            return MyPreferenceDataStoreImpl(context)
        }
    }
    

这就是我得到的错误:

Error: [dagger.android.AndroidInjector.inject(T)] com.example.BaseActivity cannot
be provided without an @Provides-annotated method.
This type supports members injection but cannot
be implicitly provided.

1 个答案:

答案 0 :(得分:1)

我并不完全明白你尝试做什么,但这个解决方案基于我的理解

AppComponent应该看起来像这样

@Singleton
@Component(modules = [
    AndroidSupportInjectionModule::class,
    AppModule::class,
    ActivityModule::class])
interface AppComponent : AndroidInjector<DaggerApplication> {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder

        fun build(): AppComponent
    }
}

您的基本活动将注入所有对象

abstract class BaseActivity : DaggerAppCompatActivity() {

    @Inject
    lateinit var prefs: SharedPreferences

    //other objects to inject

}

将从中继承的活动,例如:MainActivity

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        prefs.getBoolean("s", true)
    }

}

活动模块

@Module
abstract class ActivityModule {

    @ContributesAndroidInjector
    abstract fun bindMainActivity(): MainActivity

    @ContributesAndroidInjector
    abstract fun bindBaseActivity():BaseActivity

}

的AppModule

@Module
class AppModule {

    @Singleton
    @Provides
    fun providesMyPreferenceDataStore(application: Application): SharedPreferences {
        return application.getSharedPreferences("test", Context.MODE_PRIVATE)
    }

}