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 :-)
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.
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.
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.
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.
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.
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With