无法使用 Dagger2 将 AppModule 中提供的对象注入动态功能模块中的 Activity
Cannot inject object provided in AppModule to Activity in a dynamic feature module with Dagger2
使用下面的设置,我无法将 Singleton 对象注入动态功能模块内的 Activity。我可以注入子组件 MainActivity,但不能注入动态功能模块中的 Activity。
@Component(modules = [AppModule::class])
interface AppComponent {
fun inject(application: Application)
@Component.Factory
interface Factory {
fun create(@BindsInstance application: Application): AppComponent
}
// Types that can be retrieved from the graph
fun mainActivityComponentFactory(): MainActivitySubComponent.Factory
}
我的 AppModule
@Module(includes = [AppProviderModule::class])
abstract class AppModule {
@Binds
abstract fun bindContext(application: Application): Context
}
@Module
object AppProviderModule {
@Provides
@Singleton
fun provideSharedPreferences(application: Application): SharedPreferences {
return application.getSharedPreferences("PrefName", Context.MODE_PRIVATE)
}
}
动态功能模块 GalleryComponent
@GalleryScope
@Component(
dependencies = [AppComponent::class],
modules = [GalleryModule::class])
interface GalleryComponent {
fun inject(galleryActivity: GalleryActivity)
}
和我的应用程序
open class MyApplication : Application() {
// Instance of the AppComponent that will be used by all the Activities in the project
val appComponent: AppComponent by lazy {
initializeComponent()
}
open fun initializeComponent(): AppComponent {
// Creates an instance of AppComponent using its Factory constructor
// We pass the applicationContext that will be used as Application
return DaggerAppComponent.factory().create(this).apply {
inject(this@MyApplication)
}
}
}
Activity 在动态特征模块中,当只注入 GalleryViewer
和 DummyDependency
从 GalleryModule
注入时它工作正常
class GalleryActivity : AppCompatActivity() {
@Inject
lateinit var sharedPreferences: SharedPreferences
@Inject
lateinit var galleryViewer: GalleryViewer
@Inject
lateinit var dummyDependency: DummyDependency
override fun onCreate(savedInstanceState: Bundle?) {
DaggerGalleryComponent.builder()
.appComponent((application as MyApplication).appComponent)
.build()
.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_gallery)
}
当我尝试从 AppModule
注入 SharedPreferences 或任何不依赖于任何参数(如上下文或应用程序)的依赖项时,出现错误
error: [Dagger/MissingBinding] android.content.SharedPreferences cannot be provided without an @Provides-annotated method.
这里的错误是不包括 提供方法 用于 AppComponent
中的依赖项并且没有正确构建动态功能组件。
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
/**
* This method is required to get this object from a class that uses this component
* as dependent component
*/
fun provideSharedPreferences(): SharedPreferences
fun inject(application: Application)
@Component.Factory
interface Factory {
fun create(@BindsInstance application: Application): AppComponent
}
// Types that can be retrieved from the graph
fun mainActivityComponentFactory(): MainActivitySubComponent.Factory
}
在动态功能中
@GalleryScope
@Component(
dependencies = [AppComponent::class],
modules = [GalleryModule::class])
interface GalleryComponent {
fun inject(galleryActivity: GalleryActivity)
// Alternative1 With Builder
@Component.Builder
interface Builder {
fun build(): GalleryComponent
@BindsInstance
fun application(application: Application): Builder
fun galleryModule(module: GalleryModule): Builder
fun appComponent(appComponent: AppComponent): Builder
}
// Alternative2 With Factory
@Component.Factory
interface Factory {
fun create(appComponent: AppComponent,
galleryModule: GalleryModule,
@BindsInstance application: Application): GalleryComponent
}
}
您必须使用 Builder
或 Factory
,将来可能需要 none,但它还不支持动态功能,我更喜欢工厂模式因为他们弃用了 Builder 模式。
里面ActivityonCreate
初始化注入
private fun initInjections() {
// Alternative1 With Builder
DaggerGalleryComponent.builder()
.appComponent((application as MyApplication).appComponent)
.application(application)
.galleryModule(GalleryModule())
.build()
.inject(this)
// Alternative2 With Factory
DaggerGalleryComponent
.factory()
.create((application as MyApplication).appComponent, GalleryModule(), application)
.inject(this)
}
您应该选择在功能组件中使用的相同模式。
使用下面的设置,我无法将 Singleton 对象注入动态功能模块内的 Activity。我可以注入子组件 MainActivity,但不能注入动态功能模块中的 Activity。
@Component(modules = [AppModule::class])
interface AppComponent {
fun inject(application: Application)
@Component.Factory
interface Factory {
fun create(@BindsInstance application: Application): AppComponent
}
// Types that can be retrieved from the graph
fun mainActivityComponentFactory(): MainActivitySubComponent.Factory
}
我的 AppModule
@Module(includes = [AppProviderModule::class])
abstract class AppModule {
@Binds
abstract fun bindContext(application: Application): Context
}
@Module
object AppProviderModule {
@Provides
@Singleton
fun provideSharedPreferences(application: Application): SharedPreferences {
return application.getSharedPreferences("PrefName", Context.MODE_PRIVATE)
}
}
动态功能模块 GalleryComponent
@GalleryScope
@Component(
dependencies = [AppComponent::class],
modules = [GalleryModule::class])
interface GalleryComponent {
fun inject(galleryActivity: GalleryActivity)
}
和我的应用程序
open class MyApplication : Application() {
// Instance of the AppComponent that will be used by all the Activities in the project
val appComponent: AppComponent by lazy {
initializeComponent()
}
open fun initializeComponent(): AppComponent {
// Creates an instance of AppComponent using its Factory constructor
// We pass the applicationContext that will be used as Application
return DaggerAppComponent.factory().create(this).apply {
inject(this@MyApplication)
}
}
}
Activity 在动态特征模块中,当只注入 GalleryViewer
和 DummyDependency
从 GalleryModule
注入时它工作正常
class GalleryActivity : AppCompatActivity() {
@Inject
lateinit var sharedPreferences: SharedPreferences
@Inject
lateinit var galleryViewer: GalleryViewer
@Inject
lateinit var dummyDependency: DummyDependency
override fun onCreate(savedInstanceState: Bundle?) {
DaggerGalleryComponent.builder()
.appComponent((application as MyApplication).appComponent)
.build()
.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_gallery)
}
当我尝试从 AppModule
注入 SharedPreferences 或任何不依赖于任何参数(如上下文或应用程序)的依赖项时,出现错误
error: [Dagger/MissingBinding] android.content.SharedPreferences cannot be provided without an @Provides-annotated method.
这里的错误是不包括 提供方法 用于 AppComponent
中的依赖项并且没有正确构建动态功能组件。
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
/**
* This method is required to get this object from a class that uses this component
* as dependent component
*/
fun provideSharedPreferences(): SharedPreferences
fun inject(application: Application)
@Component.Factory
interface Factory {
fun create(@BindsInstance application: Application): AppComponent
}
// Types that can be retrieved from the graph
fun mainActivityComponentFactory(): MainActivitySubComponent.Factory
}
在动态功能中
@GalleryScope
@Component(
dependencies = [AppComponent::class],
modules = [GalleryModule::class])
interface GalleryComponent {
fun inject(galleryActivity: GalleryActivity)
// Alternative1 With Builder
@Component.Builder
interface Builder {
fun build(): GalleryComponent
@BindsInstance
fun application(application: Application): Builder
fun galleryModule(module: GalleryModule): Builder
fun appComponent(appComponent: AppComponent): Builder
}
// Alternative2 With Factory
@Component.Factory
interface Factory {
fun create(appComponent: AppComponent,
galleryModule: GalleryModule,
@BindsInstance application: Application): GalleryComponent
}
}
您必须使用 Builder
或 Factory
,将来可能需要 none,但它还不支持动态功能,我更喜欢工厂模式因为他们弃用了 Builder 模式。
里面ActivityonCreate
初始化注入
private fun initInjections() {
// Alternative1 With Builder
DaggerGalleryComponent.builder()
.appComponent((application as MyApplication).appComponent)
.application(application)
.galleryModule(GalleryModule())
.build()
.inject(this)
// Alternative2 With Factory
DaggerGalleryComponent
.factory()
.create((application as MyApplication).appComponent, GalleryModule(), application)
.inject(this)
}
您应该选择在功能组件中使用的相同模式。