HasActivityInjector can not be resolved in android dagger 2

android dagger 2 tutorial for beginners
dagger-android deprecated
dagger-android library
dagger 2 maven
dagger 2 java example
daggerappcomponent not found
dagger android medium
dependency injection in android with dagger 2 free download

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

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. I was trying to implement HasActivityInjector in my android application but it's showing me HasActivityInjector cann't be resolved. Below dependencies and  This article is all about implementing the new Dagger 2 Android Injector in an Android application. In case, you are not familiar with Dagger 2, refer to the below article. Introduction to Dagger 2, Using Dependency Injection in Android. The new dagger 2 is released with the Android support module and the Android compiler.

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
       }        
}

AGP 3.4.2 Gradle 5.5.1 Kotlin 1.3.41 Dagger 2.24 The 2.24 release removes 2.24 cannot inject AndroidInjector<Any> into Application class #1576 public abstract interface MainApplicationComponent { ^ dagger.android. Browse other questions tagged android dependency-injection dagger-2 or ask your own question. The Overflow Blog The Overflow #20: Sharpen your skills

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.

After clean and recompile my Android project, it build failed and throws such error​: cannot find symbol class DaggerAppComponent. active oldest votes. 6. @ContributesAndroidInjector is in HEAD, added ten days ago. It is not yet in a numbered release in Maven, but I'd imagine it will be shortly. Until then, you can download or clone the repository directly from Github (click the green button).

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

/..../

Because android-dagger is all about annotations and I think It's learning curve is a bit hard. In my opinion, Dagger 2 and dependency injection should be understood before we use A class shouldn't know anything about how it is injected. That is why we implement HasActivityInjector interface. Dagger 2 uses code generation at compile time for dependency injection. In this, it is different from other dependency injection frameworks like Guice that inspect injection sites at runtime. In order for Dagger 2 to work, you must at some point specify the invariant of the injection site. Therefore it will never be possible to write something like:

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"

This article is regarding how to implement dagger 2 with new approach, However I don't compare between old and new dagger structure so I just have class SampleApplication : Application(), HasActivityInjector { @Inject I would like to exercise this Dagger 2 Vehicle Motor example. I made everything exact like in that tutorial, except for my gradel.build: compile 'com.google.dagger:dagger:2.4' apt 'com.google.dagger:dagger-compiler:2.4' compile 'javax.annotation:javax.annotation-api:1.2' but then I get an . error: cannot find symbol variable Dagger_VehicleComponent

Dagger 2 does not inject fields automatically. It can also not inject private fields. If you want to use field injection you have to define a method in  Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

The new dagger 2 is released with the Android support module and the In case​, you are not familiar with Dagger 2, refer to the below article. After that, you have to implement the HasActivityInjector interface in your  android-dagger2-example. This project implements the dagger 2 in android for dependency injection. The complete explanation is present in the following articles:

In this article I will talk about dagger-android and re-implement what we did in the last But Marcos, you've just said that subcomponents are not ideal in a Step 2. In our application we have to implement HasActivityInjector because We have solved the circular dependency issue and now our injected  Can be used on a constructor, a field, or a method. @Component: enable selected modules and used for performing dependency injection. Dagger 2 uses generated code to access the fields and not reflection. Therefore it is not allowed to use private fields for field injection. 2.2.

Comments
  • update your version to 2.17 and rebuild project.
  • @UmangBurman, I am already using the latest one dependency only.
  • @rekire, Of course I am Importing the mentioned class, by the way I am using Android stdio, and it's cool enough to import it self or easy by Alt+Enter
  • Thank boss. You Save my Day
  • As it is of any type. So, we can use the same to inject Worker as well. is it?
  • could you briefly explain which dependency for what?
  • @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).