java.lang.IllegalArgumentException: No injector factory bound for Class<MyActivity_>

How to deal with Dagger 2 error: "Injector factories were bound for supertypes of ... Did you mean to bind an injector factory for the subtype?"

  1. Suppose we do have some BaseActivity:

    open class BaseActivity : MvpAppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        }
    }
    

We do also have an ActivityBuilder Module (which provides the BaseActivity itself) and we have a Module which provides the dependencies needed for the successful running of the BaseActivity.

@Module
abstract class ActivityBuilder {
    @ContributesAndroidInjector(modules = [BaseModule::class])
    abstract fun bindBaseActivity(): BaseActivity
}
  1. If we will add a ChildActivity like that

    class ChildActivity : BaseActivity() { }

thinking: "Hey, Dagger 2 will satisfy the dependencies for the BaseActivity and since we are extending it, we will get a ChildActivity up and running, right?". Wrong. We will get an exception "Injector factories were bound for supertypes of ... Did you mean to bind an injector factory for the subtype?"

  1. What we should do? We should make Dagger 2 know explicitly about our ChildActivity.

a) Add AndroidInjection.inject(this) to the ChildActivity onCreate() method:

class ChildActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
    }
}

b) add all subclasses of the BaseActivity to the ActivityBuilder module (which provides activities). Note, that the BaseModule, satisfying dependencies for the BaseActivity, should also be included to the @ContributesAndroidInjector of the child class (ChildActivity)

@Module
abstract class ActivityBuilder {
    @ContributesAndroidInjector(modules = [BaseModule::class])
    abstract fun bindBaseActivity(): BaseActivity

    //Adding subclasses
    @ContributesAndroidInjector(modules = [BaseModule::class])
    abstract fun bindChildActivity(): ChildActivity
}

Just an informed guess: It probably happens because your Class is actually called different when you use AndroidAnnotations (they add the underscore somewhere). Then you have to define the binding also like so (not sure where the underscore goes):

@ContributesAndroidInjector(modules = LoginActivityModule_.class)
@LoginActivityScope
abstract LoginActivity bindLoginActivity();