HasActivityInjector can not be resolved in android

2020-07-02 09:40发布

问题:

I was trying to implement HasActivityInjector in my android application but it's showing me HasActivityInjector cann't be resolved. Below dependencies and plugin I have used in my project-

apply plugin: 'kotlin-kapt'

and

implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'

Here is the code I am trying -

class RanoBoilerplateApplication : Application(), HasActivityInjector{
    @Inject
    lateinit var activityDispatchingAndroidInjector:
            DispatchingAndroidInjector<Activity>

    override fun onCreate() {
        super.onCreate()

        DaggerAppComponent.builder()
                .application(this)
                .build()
                .inject(this)
    }

    override fun activityInjector(): AndroidInjector<Activity> =
            activityDispatchingAndroidInjector
}

I don't know what other dependencies I must have to include to work that, help me if anyone have done this before.

Find the latest Dagger 2 version here.

Happy coding :-)

回答1:

In case anyone comes here after updating to 2.24, this was removed: https://github.com/google/dagger/commit/3bd8f707cb28fd0c5f3abb4f87658566f8b52c10.

You can use HasAndroidInjector instead.



回答2:

It's pretty late to answer but it might be useful for someone who is new to dagger world!

Its been replaced with HasAndroidInjector in order to avoid boilerplate of implementing multiple Dagger Interfaces like HasActivityInjector, HasServiceInjector etc, in your Application class. Now you only need to implement HasAndroidInjector like below:

class DaggerExampleApplication : Application(), HasAndroidInjector{

       @Inject lateinit var androidInjector : DispatchingAndroidInjector<Any>

       override fun androidInjector(): AndroidInjector<Any> = androidInjector
       override fun onCreate() {
         super.onCreate()
         //Your code
       }        
}


回答3:

My dependencies looks like this:

//Dagger
implementation "com.google.dagger:dagger:${libs.dagger}"
implementation "com.google.dagger:dagger-android:${libs.dagger}"
implementation "com.google.dagger:dagger-android-support:${libs.dagger}"
kapt "com.google.dagger:dagger-compiler:${libs.dagger}"
kapt "com.google.dagger:dagger-android-processor:${libs.dagger}"

The support one is needed if you're using appcompat. And the ${libs.dagger} refers to the needed dagger version (e.g. 2.16). Read more about dependencies here.



回答4:

In addition to @mbonnin and @Subhan's answer, starting version 2.24, Has{Activity,Fragment,Service,ContentProvider,BroadcastReceiver} are removed. If you still want to support this old implementation, use version 2.23 which supports both HasAndroidInjector and Has{Activity,Fragment,Service,ContentProvider,BroadcastReceiver}.

here's what it should look like on version 2.24

Application

class ExampleApp: Application(), HasAndroidInjector {

    @Inject
    lateinit var androidInjector: DispatchingAndroidInjector<Any>

    override fun androidInjector(): AndroidInjector<Any> = androidInjector

/..../

Activity

class ExampleActivity: AppCompatActivity(), HasAndroidInjector {

    @Inject
    lateinit var androidInjector: DispatchingAndroidInjector<Any>

    override fun androidInjector(): AndroidInjector<Any> = androidInjector

/..../

Fragment

class MoreFragment: Fragment(), HasAndroidInjector {

    @Inject
    lateinit var androidInjector: DispatchingAndroidInjector<Any>

    override fun androidInjector(): AndroidInjector<Any> = androidInjector

/..../


回答5:

HasActivityInjector was introduced in new dagger android support module.Include following dependency in your build.gradle file.

kapt "com.google.dagger:dagger-android-processor:2.18"
implementation "com.google.dagger:dagger-android-support:2.18"