title: Injecting ViewModels with an Activity context dependency
body: <p>I'm using dagger.android and want to inject ViewModels. On the other hand, I have an <code>ApiModule</code> that depends on activity context, </p>
  
  <p>Here is the <code>AppComponent</code></p>
  
  <pre><code>@Singleton
  @Component(modules = {
          AndroidSupportInjectionModule.class,
          AppModule.class,
          ActivityBindingModule.class,
          ViewModelModule.class
  })
  public interface AppComponent extends AndroidInjector&lt;MyApp&gt; {
      @Component.Builder
      abstract class Builder extends AndroidInjector.Builder&lt;MyApp&gt; {}
  }
  </code></pre>
  
  <p>The <code>ViewModelModule</code> provides ViewModelProvider.Factory</p>
  
  <pre><code>@Module
  abstract class ViewModelModule {
      @Binds
      abstract ViewModelProvider.Factory bindViewModelFactory(MyAppViewModelFactory factory);
  }
  </code></pre>
  
  <p>Here is the <code>ActivityBindingModule</code>:</p>
  
  <pre><code>@Module
  abstract class ActivityBindingModule {
      //MainActivity
      @ActivityScoped
      @ContributesAndroidInjector(modules = {
              MainActivityModule.class,
              ApiModule.class
      })
      abstract MainActivity contributeMainActivityInjector();
  
      @Module
      abstract class MainActivityModule {
          @ActivityScoped
          @Binds
          abstract Activity bindMainActivity(MainActivity activity);
  
          @Binds
          @IntoMap
          @ViewModelKey(UserViewModel.class)
          abstract ViewModel bindUserViewModel(UserViewModel userViewModel);
      }
  
      //SecondActivity
      @ActivityScoped
      @ContributesAndroidInjector(modules = {
              SecondActivityModule.class,
              ApiModule.class
      })
      abstract SecondActivity contributeSecondActivityInjector();
  
      @Module
      abstract class SecondActivityModule {
          @ActivityScoped
          @Binds
          abstract Activity bindSecondActivity(SecondActivity activity);
  
          @Binds
          @IntoMap
          @ViewModelKey(DetailViewModel.class)
          abstract ViewModel bindDetailViewModel(DetailViewModel detailViewModel);
      }
  }
  </code></pre>
  
  <p>I have these injection hierarchies:</p>
  
  <ul>
  <li><p><code>MainActivity --inject--&gt; ViewModelProvider.Factory --inject--&gt; MyAppViewModelFactory --inject--&gt; UserViewModel --inject--&gt; UserRepository --inject--&gt; ApiService --inject--&gt; Activity context</code></p></li>
  <li><p><code>SecondActivity --inject--&gt; ViewModelProvider.Factory --inject--&gt; MyAppViewModelFactory --inject--&gt; DetailViewModel --inject--&gt; DetailRepository --inject--&gt; ApiService --inject--&gt; Activity context</code></p></li>
  </ul>
  
  <p>We provide:</p>
  
  <ul>
  <li><code>Activity</code> context on <code>MainActivityModule</code> (and <code>SecondActivityModule</code>)</li>
  <li><code>ApiService</code> on <code>ApiModule</code> (added to modules of each activity on <code>ActivityBindingModule</code>)</li>
  <li><code>UserRepository</code> and <code>DetailRepository</code> are <code>@Singleton</code></li>
  <li><code>UserViewModel</code> on <code>MainActivityModule</code> and <code>DetailViewModel</code> on <code>SecondActivityModule</code></li>
  <li><code>MyAppViewModelFactory</code> is @Singleton</li>
  </ul>
  
  <p>Here is <code>MyAppViewModelFactory</code>:</p>
  
  <pre><code>@Singleton
  public class MyAppViewModelFactoryimplements ViewModelProvider.Factory {
      private final Map&lt;Class&lt;? extends ViewModel&gt;, Provider&lt;ViewModel&gt;&gt; creators;
  
      @Inject
      MyAppViewModelFactory(Map&lt;Class&lt;? extends ViewModel&gt;, Provider&lt;ViewModel&gt;&gt; creators) {
          this.creators = creators;
      }
  
      @SuppressWarnings("unchecked")
      @Override
      @NonNull
      public &lt;T extends ViewModel&gt; T create(@NonNull Class&lt;T&gt; modelClass) {
          Provider&lt;? extends ViewModel&gt; creator = creators.get(modelClass);
          if (creator == null) {
              for (Map.Entry&lt;Class&lt;? extends ViewModel&gt;, Provider&lt;ViewModel&gt;&gt; entry : creators.entrySet()) {
                  if (modelClass.isAssignableFrom(entry.getKey())) {
                      creator = entry.getValue();
                      break;
                  }
              }
          }
          if (creator == null) {
              throw new IllegalArgumentException("unknown model class " + modelClass);
          }
          try {
              return (T) creator.get();
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }
  }
  </code></pre>
  
  <p>I got this error:</p>
  
  <pre><code>error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] java.util.Map&lt;java.lang.Class&lt;? extends android.arch.lifecycle.ViewModel&gt;,javax.inject.Provider&lt;android.arch.lifecycle.ViewModel&gt;&gt; cannot be provided without an @Provides-annotated method.
  java.util.Map&lt;java.lang.Class&lt;? extends android.arch.lifecycle.ViewModel&gt;,javax.inject.Provider&lt;android.arch.lifecycle.ViewModel&gt;&gt; is injected at
  com.example.viewmodels.MyAppViewModelFactory.&lt;init&gt;(creators)
  com.example.viewmodels.MyAppViewModelFactory is injected at
  com.example.di.ViewModelModule.bindViewModelFactory(factory)
  android.arch.lifecycle.ViewModelProvider.Factory is injected at
  com.example.activities.MainActivity.viewModelFactory
  com.example.activities.MainActivity is injected at
  dagger.android.AndroidInjector.inject(T)
  component path: com.example.di.AppComponent ? com.example.di.ActivityBindingModule_ContributeMainActivityInjector.MainActivitySubcomponent
  </code></pre>
  
  <p>I tried to move <code>bindUserViewModel</code> (from <code>MainActivityModule</code>) and <code>bindDetailViewModel</code> (from <code>SecondActivityModule</code>) to <code>ViewModelModule</code> and I got this error:</p>
  
  <pre><code>error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] tk.medlynk.medlynk.http.ApiService cannot be provided without an @Provides-annotated method.
  com.example.http.ApiService is injected at
  com.example.repositories.UserRepository.&lt;init&gt;(apiService)
  com.example.repositories.UserRepository is injected at
  com.example.viewmodels.UserViewModel.&lt;init&gt;(userRepo)
  com.example.viewmodels.UserViewModel is injected at
  com.example.di.ViewModelModule.bindUserViewModel(userViewModel)
  java.util.Map&lt;java.lang.Class&lt;? extends android.arch.lifecycle.ViewModel&gt;,javax.inject.Provider&lt;android.arch.lifecycle.ViewModel&gt;&gt; is injected at
  com.example.viewmodels.MyAppViewModelFactory.&lt;init&gt;(creators)
  com.example.viewmodels.MyAppViewModelFactory is injected at
  com.example.di.ViewModelModule.bindViewModelFactory(factory)
  android.arch.lifecycle.ViewModelProvider.Factory is injected at
  com.example.activities.MainActivity.viewModelFactory
  com.example.activities.MainActivity is injected at
  dagger.android.AndroidInjector.inject(T)
  component path: com.example.di.AppComponent ? com.example.di.ActivityBindingModule_ContributeMainActivityInjector.MainActivitySubcomponent
  </code></pre>
  
  <p>What am I missing here? the <a href="https://github.com/google/iosched/blob/master/mobile/src/main/java/com/google/samples/apps/iosched/di/ActivityBindingModule.kt" rel="nofollow noreferrer">iosched</a> project does the same thing without problem</p>