我实现了自定义ViewModelFactory
,Dagger DI对ViewModel
的成员进行了自动注入,所有代码如下:
class ViewModelFactory @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)
}
}
}
DI:
@Suppress("unused")
@Module
abstract class ViewModelModule {
@Binds
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}
@Singleton
@Component(
modules = [
AndroidInjectionModule::class,
AppModule::class,
ViewModelModule::class,
ActivityModule::class
]
)
interface AppComponent: AndroidInjector<App> {
@Component.Builder
abstract class Builder: AndroidInjector.Builder<App>()
}
@Suppress("unused")
@Module
abstract class ActivityModule {
@ContributesAndroidInjector(
modules = [LoginFragmentBuilderModule::class, LoginViewModelModule::class]
)
abstract fun contributeLoginActivity(): LoginActivity
@ContributesAndroidInjector(modules = [])
abstract fun contributeMainActivity(): MainActivity
}
@Suppress("unused")
@Module
abstract class LoginViewModelModule {
@Binds
@IntoMap
@ViewModelKey(AccountViewModel::class)
abstract fun bindAccountViewModel(accountViewModel: AccountViewModel): ViewModel
}
@Suppress("unused")
@Module
abstract class LoginFragmentBuilderModule {
@ContributesAndroidInjector
abstract fun contributeAccountFragment(): AccountFragment
}
@Module
object AppModule {
@JvmStatic
@Provides
fun provideGitHubApi(retrofit: Retrofit): IGitHubApi {
return retrofit.create(IGitHubApi::class.java)
}
// A lot of other providers
}
AccountViewModel::
class AccountViewModel(app: Application) : AndroidViewModel(app) {
@Inject
lateinit var gitHubApi: IGitHubApi
}
我使用tutorial编写了它,但是编译时出现了错误:
e: /test/di/AppComponent.java:8: error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] test.ui.login.models.AccountViewModel cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.contedevel.juke.App> {
^
test.ui.login.models.AccountViewModel is injected at
test.di.LoginViewModelModule.bindAccountViewModel(accountViewModel)
java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
test.di.ViewModelFactory.<init>(creators)
test.di.ViewModelFactory is injected at
test.di.ViewModelModule.bindViewModelFactory(factory)
androidx.lifecycle.ViewModelProvider.Factory is injected at
test.ui.login.AccountFragment.viewModelFactory
test.ui.login.AccountFragment is injected at
dagger.android.AndroidInjector.inject(T)
component path: test.di.AppComponent → test.di.ActivityModule_ContributeLoginActivity.LoginActivitySubcomponent → test.di.LoginFragmentBuilderModule_ContributeAccountFragment.AccountFragmentSubcomponent
答案 0 :(得分:1)
从堆栈跟踪中:
test.ui.login.models.AccountViewModel cannot be provided without an @Inject constructor or an @Provides-annotated method
您的AccountViewModel
没有@Inject
构造函数。
尝试一下:
class AccountViewModel @Inject constructor(
private val gitHubApi: IGitHubApi,
app: Application) : AndroidViewModel(app) {
}
答案 1 :(得分:1)
首先,在这种情况下Dagger将如何工作。您正在通过AccountViewModel
中的参数绑定bindAccountViewModel
的实例。但是,现在匕首将创建AccountViewModel
的实例,并将其绑定到键为AccountViewModel::class
的Map。
根据堆栈跟踪,Dagger建议您可以使用构造函数注入,也可以在模块中使用Provides方法提供ViewModel
。从我们当前的实现中,第二个建议变得很普遍,因为我们没有创建AccountViewModel实例。因此,您需要在AccountViewModel
中将IGithubApi作为构造函数参数传递,这在这种情况下将是可行的解决方案。
class AccountViewModel @Inject constructor(private val api: IGithubAPI, app: Application) : AndroidViewModel(app)