我正在使用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
,不起作用。
将providesMyPreferenceDataStore
从ApplicationModule
移至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.
答案 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)
}
}