title: Dagger2: no injector factory bound for fragment
body: <p>I'm trying to convert a project that I'm building to use the dagger-android API for the DI framework, but I'm running into a dead end with an IllegalArgumentException when trying to inject a fragment using @ContributesAnroidInjector.</p>
  
  <p>The relevant modules and components are included below:</p>
  
  <p><strong>ApplicationComponent.java</strong></p>
  
  <pre><code>@Singleton
  @Component(modules = {AndroidSupportInjectionModule.class,
      ApplicationModule.class,
      ActivityBindingModule.class,
      DataManagerModule.class})
  public interface ApplicationComponent extends AndroidInjector&lt;MyApplication&gt; {
  
  DataManagerContract getDataManager();
  
  void inject(MyApplication application);
  
  @Component.Builder
  interface Builder {
  
      @BindsInstance
      ApplicationComponent.Builder application(Application application);
  
      ApplicationComponent build();
      }
  }
  </code></pre>
  
  <p>my <strong>ActivityBindingModule.java</strong>:</p>
  
  <pre><code>@Module
  public abstract class ActivityBindingModule {
  
      @ActivityScope
      @ContributesAndroidInjector(modules = MainActivityModule.class)
      abstract MainActivity bindMainActivity();
  
      @ActivityScope
      @ContributesAndroidInjector(modules = SplashActivityModule.class)
      abstract SplashActivity bindSplashActivity();
  
      @ActivityScope
      @ContributesAndroidInjector(modules = LoginActivityModule.class)
      abstract LoginActivity bindLoginActivity();
  }
  </code></pre>
  
  <p><strong>MainActivityModule.java</strong></p>
  
  <pre><code>@Module
  public abstract class MainActivityModule {
  
      @ActivityScope
      @Binds
      abstract MainActivityContract.Presenter provideMainActivityPresenter(MainActivityPresenter presenter);
  
      @FragmentScope
      @ContributesAndroidInjector
      abstract HomeFragment provideHomeFragment();
  
      @FragmentScope
      @Binds
      abstract HomeFragmentContract.Presenter provideHomeFragmentPresenter(HomeFragmentPresenter presenter);
  
      // Inject other fragments and presenters
  }
  </code></pre>
  
  <p>SplashActivity and LoginActivity only depend on their respective presenters, and dagger works fine in these. But my MainActivity can contain numerous fragments and causes a crash when trying to inject one of those fragments using:</p>
  
  <p><strong>HomeFragment.java</strong></p>
  
  <pre><code>public class HomeFragment extends Fragment {
      ....
      @Override
      public void onAttach(Context context) {
          AndroidSupportInjection.inject(this);
          super.onAttach(context);
      }
      ....
  }
  </code></pre>
  
  <p>Here is my logcat for this crash: </p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.main.MainActivity}: java.lang.IllegalArgumentException: No injector factory bound for Class&lt;com.myapp.ui.main.Home.HomeFragment&gt;
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
  at android.app.ActivityThread.-wrap12(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6119)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  Caused by: java.lang.IllegalArgumentException: No injector factory bound for Class&lt;com.myapp.ui.main.Home.HomeFragment&gt;
          at dagger.android.DispatchingAndroidInjector.inject(DispatchingAndroidInjector.java:104)
          at dagger.android.support.AndroidSupportInjection.inject(AndroidSupportInjection.java:74)
          at com.myapp.ui.main.Home.HomeFragment.onAttach(HomeFragment.java:65)
          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1363)
          at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1109)
          at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:996)
          at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:99)
          at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2364)
          at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
          at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3221)
          at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3171)
          at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
          at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:560)
          at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
          at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
          at android.app.Activity.performStart(Activity.java:6696)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:154) 
          at android.app.ActivityThread.main(ActivityThread.java:6119) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  </code></pre>
  
  <p>I'm not sure where the problem in the code is. If I move the bindings for HomeFragment to the ActivityBindingModule, the app runs fine, but the crash comes back if I bring those bindings back into the MainActivityModule. What am I doing wrong here?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>public class MyApp extends DaggerApplication {
  
      @Override
      protected AndroidInjector&lt;? extends DaggerApplication&gt; applicationInjector() {
          return DaggerApplicationComponent.builder().application(this).build();
      }
  }
  </code></pre>
  
  <p>and my main activity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity
      implements MainActivityContract.View,
      NavigationView.OnNavigationItemSelectedListener {
  
  @Inject
  MainActivityContract.Presenter mPresenter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      AndroidInjection.inject(this);
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // Open home fragment on first start
      if (savedInstanceState == null) {
          // Create new instance of HomeFragment
          HomeFragment homeFragment = HomeFragment.newInstance();
  
          FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
          fragmentTransaction.replace(R.id.content_main, homeFragment)
                  .commit();
      }
  
  // Other logic
  }
  </code></pre>