Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HasActivityInjector can not be resolved in android dagger 2

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

like image 796
Bajrang Hudda Avatar asked Oct 23 '18 12:10

Bajrang Hudda


People also ask

What is HasActivityInjector?

HasActivityInjector is a part of dagger. android . You can but not have to use it. You can easily live without this thing and still use everything that dagger offers. That's why it is in some guides but not all of them.

What is DispatchingAndroidInjector?

Class DispatchingAndroidInjector<T>Performs members-injection on instances of core Android types (e.g. Activity , Fragment ) that are constructed by the Android framework and not by Dagger. This class relies on an injected mapping from each concrete class to an AndroidInjector.

What is Android dagger2?

Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.

What is dagger in Android Mindorks?

Dagger 2 is a dependency injection for Android. Dagger 2 is a dependency injection (DI) framework. And, Dependency Injection in build upon the concept of Inversion of Control which says that a class should get its dependencies from outside.


2 Answers

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.

like image 159
mbonnin Avatar answered Sep 20 '22 13:09

mbonnin


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        }         } 
like image 32
Subhan Ali Avatar answered Sep 18 '22 13:09

Subhan Ali