android - 多模块项目 - Dagger

标签 android dagger-2 dagger

在我的 Android 项目中我有:

  • 核心库模块
  • ui_add_credit_card 图书馆模块
  • app 作为应用程序模块

核心中定义了一个AppComponent:

@Singleton
@Component(
        modules = {
                AppModule.class,
                NetworkModule.class,
                ViewModelFactoryModule.class,
        }
)
public interface AppComponent {

还有一个用于 ViewModules 的特殊模块,即绑定(bind)模块 ViewModelFactoryModule:

@Module
abstract class ViewModelFactoryModule {

    @Binds
    abstract fun bindViewModelFactory(viewModelFactory: DaggerViewModelFactory): ViewModelProvider.Factory

    @Binds
    @IntoMap
    @ViewModelKey(SplashViewModel::class)
    abstract fun bindSplashViewModel(viewModel: SplashViewModel): ViewModel
...

DaggerViewModelFactory 是一个特殊的 ViewModelProvider.Factory,它将处理 ViewModel 键的命名,因此注入(inject) Activity/fragment 不需要关心它:

class DaggerViewModelFactory @Inject constructor(
        private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val creator = creators[modelClass] ?: creators.entries.firstOrNull {
            modelClass.isAssignableFrom(it.key)
        }?.value ?: throw IllegalArgumentException("unknown model class $modelClass")
        try {
            @Suppress("UNCHECKED_CAST")
            return creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }    }
}

一切正常:)

问题是当我想为单独的项目模块 ui_add_credit_card 创建额外的 AddCreditCardComponentAddCreditCardViewModelModule 时。

@Component(
        dependencies = [AppComponent::class],
        modules = [
            AddCreditCardViewModelModule::class
        ])
interface AddCreditCardComponent {

    fun inject(creditCardRegisterActivity: CreditCardRegisterActivity)
}
@Module
abstract class AddCreditCardViewModelModule {

    @Binds
    @IntoMap
    @ViewModelKey(CreditCardViewModel::class)
    abstract fun bindCreditCardRegistrationViewModel(
            viewModel: CreditCardRegistrationViewModel
    ): ViewModel

}

我收到以下错误:

AddCreditCardComponent.java:8: error: [Dagger/MissingBinding] androidx.lifecycle.ViewModelProvider.Factory cannot be provided without an @Provides-annotated method.

有什么帮助吗?我假设 ViewModelProvider.Factory 应该由我的 ViewModelFactoryModule 提供,但它并没有发生:(

最佳答案

我设法通过以不同的方式创建模块 AddCreditCardViewModelModule 来解决我的问题(@Provide 而不是 @Binds),我必须创建专门的 ViewModel.Factory 来处理添加的 ViewModel 实例化。

@Module
object AddCreditCardViewModelModule {

    @JvmStatic @Provides
    fun provideAddCreditCardViewModelFactory(
            adyenApi: AdyenApi,
            paymentController: PaymentController
    ) = AddCreditCardViewModelFactory(adyenApi, paymentController)

}

另外(感谢 David Medenjak)确保来自 AppComponent 的依赖项(例如 AdyenApi)作为“getter”公开是至关重要的。否则 AddCreditCardComponent 将无法访问它。

干杯!

关于android - 多模块项目 - Dagger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58222449/

相关文章:

java - 如果没有@Provides 注释方法,则无法提供 Dagger 2 LocalDataSource

Android - 运行时使用 Dagger 创建对象

dagger-2 - dagger android 对 androidx.fragment 的支持

android - 如何在数据库 (SQLite) 中保存图像并将其加载到 View : Android

android - 在 kotlin android 中显示 Unresolved 引用

android - 服务可以被task killer杀死吗

android - 无法解析 DaggerAppComponent

android - 使用 Dagger,可以重新创建注入(inject)的对象吗?

android - 如何从 android 中的 url 解析多个 JSON 对象和数组?我在那个例子中使用了一个我想要的例子,

java - 找不到 gradle 包装器