41

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 :-)

Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63

6 Answers6

89

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.

mbonnin
  • 6,893
  • 3
  • 39
  • 55
47

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
       }        
}
Subhan Ali
  • 1,370
  • 13
  • 17
13

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

/..../
Kurt Capatan
  • 421
  • 1
  • 5
  • 8
6

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.

Demigod
  • 5,073
  • 3
  • 31
  • 49
  • could you briefly explain which dependency for what? – Bajrang Hudda Oct 23 '18 at 12:45
  • @BajrangHudda, briefly - the 1st and 4th is min required for dagger to work (dagger classes + annotation processor), the 2nd and 5th are the one needed to work with `Android` (and the 3rd one if you have appcompat). – Demigod Oct 23 '18 at 13:45
2

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"
Vipul
  • 27,808
  • 7
  • 60
  • 75
1

Could have been late late answer but new Dagger2 dependencies carries DaggerAppCompatActivity and DaggerFragment classes which are useful for injecting activities and fragments by itself. So, we no longer need HasActivityInjector, autoInjection methods or something like them.

Amrah Aziz
  • 81
  • 6
  • We still might depend on some our legacy project or external library which force us to extend their own ```Fragment``` and ```Activity``` classes. Interfaces bring more flexibility to this. – Joe Dow Dec 29 '22 at 14:03