title: Error [Dagger/MissingBinding] androidx.lifecycle.ViewModelProvider.Factory cannot be provided without an @Provides-annotated method
body: <p>I am facing an issue while implementing MultiBinding using dagger 2.2. I am using dagger with MVVM architecture. I have injected the <code>ViewModelProvideFactory</code> constructor and binds the dependency from module.</p>
  
  <p>I have followed the tutorial of Mitch from youtube </p>
  
  <p><a href="https://www.youtube.com/watch?v=DToD1W9WdsE&amp;list=PLgCYzUzKIBE8AOAspC3DHoBNZIBHbIOsC&amp;index=13" rel="noreferrer">https://www.youtube.com/watch?v=DToD1W9WdsE&amp;list=PLgCYzUzKIBE8AOAspC3DHoBNZIBHbIOsC&amp;index=13</a></p>
  
  <p>I have searched on these links for the solutions but still facing the same issue. </p>
  
  <p><a href="https://stackoverflow.com/questions/54399582/dagger2-viewmodel-cannot-be-provided-without-an-provides-annotated-method">Dagger2: ViewModel cannot be provided without an @Provides-annotated method</a></p>
  
  <p><a href="https://stackoverflow.com/questions/55669810/dagger-missingbinding-java-util-mapjava-lang-class-extends-viewmodel-provide">Dagger/MissingBinding java.util.Map&lt;java.lang.Class&lt;? extends ViewModel&gt;,Provider&lt;ViewModel&gt;&gt; cannot be provided without an @Provides-annotated method</a></p>
  
  <p><a href="https://github.com/google/dagger/issues/1478" rel="noreferrer">https://github.com/google/dagger/issues/1478</a></p>
  
  <p><strong>Code Snippet</strong></p>
  
  <p><strong>ViewModelKey</strong></p>
  
  <pre><code>@MapKey
  @Documented
  @Target({ElementType.METHOD})
  @Retention(RetentionPolicy.RUNTIME)
  public @interface ViewModelKey {
      Class&lt;? extends ViewModel&gt; value();
  }
  </code></pre>
  
  <p><strong>ViewModelFactoryModule</strong></p>
  
  <pre><code>   /**
   * ViewModelFactoryModule responsible for providing [ViewModelProviderFactory]
   *
   * Annotated with Module to tell dagger it is a module to provide [ViewModelProviderFactory]
   *
   * Annotated with bind annotation to efficiently provide dependencies similar to provides annotation
   */
  @Module
  abstract class ViewModelFactoryModule {
  
      @Binds
      abstract fun bindViewModelFactory(viewModelFactory: ViewModelProviderFactory) : ViewModelProvider.Factory
  }
  </code></pre>
  
  <p><strong>ViewModelProviderFactory</strong></p>
  
  <pre><code>@Singleton
  class ViewModelProviderFactory @Inject
  constructor(private val creators: Map&lt;Class&lt;out ViewModel&gt;, @JvmSuppressWildcards Provider&lt;ViewModel&gt;&gt;) :
      ViewModelProvider.Factory {
  override fun &lt;T : ViewModel&gt; create(modelClass: Class&lt;T&gt;): T {
      var creator: Provider&lt;out ViewModel&gt;? = creators[modelClass]
      if (creator == null) { // if the viewmodel has not been created
  
          // loop through the allowable keys (aka allowed classes with the @ViewModelKey)
          for ((key, value) in creators) {
  
              // if it's allowed, set the Provider&lt;ViewModel&gt;
              if (modelClass.isAssignableFrom(key)) {
                  creator = value
                  break
              }
          }
      }
  
      // if this is not one of the allowed keys, throw exception
      if (creator == null) {
          throw IllegalArgumentException("unknown model class $modelClass")
      }
  
      // return the Provider
      try {
          return creator.get() as T
      } catch (e: Exception) {
          throw RuntimeException(e)
      }
  
  }
  
      companion object {
  
          private val TAG = "ViewModelProviderFactor"
      }
  }
  </code></pre>
  
  <p><strong>StackTrace</strong></p>
  
  <pre><code>&gt; Task :app:kaptDebugKotlin FAILED
  e: /Users/fazal/Documents/fazal/demo/AdvanceDagger2/app/build/tmp/kapt3/stubs/debug/com/example/advancedagger2/di/AppComponent.java:22: error: [Dagger/MissingBinding] java.util.Map&lt;java.lang.Class&lt;? extends androidx.lifecycle.ViewModel&gt;,javax.inject.Provider&lt;androidx.lifecycle.ViewModel&gt;&gt; cannot be provided without an @Provides-annotated method.
  public abstract interface AppComponent extends dagger.android.AndroidInjector&lt;com.example.advancedagger2.BaseApplication&gt; {
                  ^
        java.util.Map&lt;java.lang.Class&lt;? extends androidx.lifecycle.ViewModel&gt;,javax.inject.Provider&lt;androidx.lifecycle.ViewModel&gt;&gt; is injected at
            com.example.advancedagger2.viewmodel.ViewModelProviderFactory(viewModelsMap)
        com.example.advancedagger2.viewmodel.ViewModelProviderFactory is injected at
            com.example.advancedagger2.ui.AuthActivity.viewModelFactory
        com.example.advancedagger2.ui.AuthActivity is injected at
            dagger.android.AndroidInjector.inject(T) [com.example.advancedagger2.di.AppComponent → com.example.advancedagger2.di.ActivityBuilderModule_ContributeAuthActivity.AuthActivitySubcomponent]
  </code></pre>
  
  <p>I have downgraded the Kotlin version but still facing the same issue. Tell me what, I am doing wrong? </p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>I am also providing ViewModel through AuthViewModel which is on the scope of activity. When activity destroys its component and dependencies also destroy.</p>
  
  <p><strong>AuthViewModelModule</strong></p>
  
  <pre><code>@Module
  abstract class AuthViewModelModule {
  
      /**
       * Binds the auth view model dependency with [ViewModelKey] to group similar [ViewModel]
       *
       * Under the hood it is providing [com.example.advancedagger2.viewmodel.AuthViewModel]
       */
      @Binds
      @IntoMap
      @ViewModelKey(AuthViewModel::class)
      abstract fun bindAuthViewModel(authViewModel: AuthViewModel) : ViewModel
  }
  </code></pre>
  
  <p><strong>ActivityBuilderModule</strong></p>
  
  <pre><code>/**
   * This Class {@linkplain ActivityBuilderModule} is responsible for for android injection
   * for the activity with in the application to avoid the seprate injection in each activity
   *
   * {@linkplain dagger.android.AndroidInjection#inject(Activity)}
   *
   * {@link com.example.advancedagger2.viewmodel.AuthViewModel} can be access from Auth Activity
   * only so it is the concept of sub-modules
   *
   */
  @Module
  public abstract class ActivityBuilderModule {
  
      @ContributesAndroidInjector(
              modules = AuthViewModelModule.class
      )
      abstract AuthActivity contributeAuthActivity();
  }
  </code></pre>