我正在遵循Google的GithubBrowserSample使用的体系结构。但是在构建项目时面临麻烦。

我已将项目迁移到AndroidX。我尝试了许多StackOverflow上可用的答案,但均未成功。
这是我在构建时面临的构建异常:

e:/windows/Apps/Verifi/VerifiApp/app/build/tmp/kapt3/stubs/debug/com/verifi/di/component/AppComponent.java:11: error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.verifi.VerifiApp> {
                ^
      java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
          com.verifi.viewmodel.AppViewModelFactory.<init>(creators)
      com.verifi.viewmodel.AppViewModelFactory is injected at
          com.verifi.ui.auth.AuthActivity.appViewModelFactory
      com.verifi.ui.auth.AuthActivity is injected at
          dagger.android.AndroidInjector.inject(T)
  component path: com.verifi.di.component.AppComponent → com.verifi.di.module.ActivityBindingModule_AuthActivity.AuthActivitySubcomponent

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.

VerifiApp.kt
class VerifiApp: Application(), HasActivityInjector {

    @Inject
    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

    override fun onCreate() {
        super.onCreate()

        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
        }

        AppInjector.init(this)
    }

    override fun activityInjector() = dispatchingAndroidInjector
}

AppComponent.kt :
@Singleton
@Component(
    modules = [AndroidSupportInjectionModule::class,
        AppModule::class,
        ActivityBindingModule::class])
interface AppComponent: AndroidInjector<VerifiApp> {
    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder

        fun build(): AppComponent
    }

    override fun inject(verifiApp: VerifiApp)
}

AppModule.kt:
@Module(includes = [ViewModelBindingModule::class])
class AppModule {

    @Provides
    @Singleton
    internal fun provideContext(application: Application): Context = application

    @Provides
    @Singleton
    internal fun getRemoteSource(remoteDataManager: RemoteDataManager): IRemoteSource = remoteDataManager

    @Provides
    @Singleton
    internal fun provideDataManger(appDataManager: AppDataManager): IAppDataSource = appDataManager

    @Singleton
    @Provides
    fun provideVerifiService(): VerifiService {
        return Retrofit.Builder()
            .baseUrl("https://api.github.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(LiveDataCallAdapterFactory())
            .build()
            .create(VerifiService::class.java)
    }
}

ViewModelBindingModule::
@Suppress("unused")
@Module(includes = [AuthViewModelsBindingModule::class])
abstract class ViewModelBindingModule {

    @Binds
    abstract fun bindViewModelFactory(factory: AppViewModelFactory): ViewModelProvider.Factory

    @Binds
    @IntoMap
    @ViewModelKey(AuthViewModel::class)
    abstract fun bindAuthViewModel(authViewModel: AuthViewModel): AuthViewModel
}

任何帮助将非常感激。

最佳答案

abstract fun bindAuthViewModel(authViewModel: AuthViewModel): AuthViewModel
应该返回一个ViewModel而不是AuthViewModel

关于android - Dagger2:如果没有@Provides注释的方法,则无法提供ViewModel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54399582/

10-09 00:01