Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger2: ViewModel cannot be provided without an @Provides-annotated method

I am following the architecture used by GithubBrowserSample by Google. But facing troubles while building the project.

I have migrated the project to AndroidX. I have tried many answers available on StackOverflow without success. Here is the build exception I am facing while building:

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
}

Any help would be much appreciated.

like image 547
Ankit Mehta Avatar asked Jan 28 '19 10:01

Ankit Mehta


1 Answers

abstract fun bindAuthViewModel(authViewModel: AuthViewModel): AuthViewModel

should be returning a ViewModel not AuthViewModel

like image 154
Blackbelt Avatar answered Sep 20 '22 23:09

Blackbelt