Dagger2: ViewModel не может быть предоставлен без аннотированного метода @ Provides - PullRequest
0 голосов
/ 28 января 2019

Я следую архитектуре, используемой GithubBrowserSample Google.Но столкнулись с проблемами при построении проекта.

Я перенес проект в 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
}

Любая помощь будет принята с благодарностью.

1 Ответ

0 голосов
/ 28 января 2019

abstract fun bindAuthViewModel(authViewModel: AuthViewModel): AuthViewModel

должен возвращать ViewModel, а не AuthViewModel

...