Search code examples
androiddependency-injectiondagger-2android-instrumentation

Unable to find the DaggerAppComponent and AppComponent classes in Android Dagger 2


I am developing an Android app using the Android Studio and Kotlin. I am adding instrumentation tests into my project. What I am trying to do now is I am trying to inject mock dependencies into the objects using Dagger 2 in my tests. I am following this tutorial, https://code.luasoftware.com/tutorials/android/setup-dagger2-for-android-kotlin/. I installed the required dependencies by adding this in my app gradle dependencies section.

implementation 'com.google.dagger:dagger:2.15'
    kapt 'com.google.dagger:dagger-compiler:2.15'
    provided 'javax.annotation:jsr250-api:1.0'
    kapt "com.google.dagger:dagger-android-processor:2.15"
    implementation "com.google.dagger:dagger-android:2.15"
    implementation "com.google.dagger:dagger-android-support:2.15"

Then I added the following snippet before the dependencies section

kapt {
    generateStubs = true
}

I also added this as well.

apply plugin: 'kotlin-kapt'

Then in my custom application class, I am trying to use the DaggerAppComponent class. Seems like the class does not come with the package.

enter image description here

What is missing in my installation and how can I fix it?


Solution

  • I use following gradle dependencies for Dagger :- In version.gradle file

        def dagger = [:]
    dagger.runtime = "com.google.dagger:dagger:2.24"
    dagger.android = "com.google.dagger:dagger-android::2.24"
    dagger.android_support = "com.google.dagger:dagger-android-support::2.24"
    dagger.compiler = "com.google.dagger:dagger-compiler::2.24"
    dagger.android_support_compiler = "com.google.dagger:dagger-android-processor::2.24"
    
    deps.dagger = dagger
    

    build.gradle of app module use following code :-

    implementation deps.dagger.runtime
    implementation deps.dagger.android
    implementation deps.dagger.android_support
    
    kapt deps.dagger.android_support_compiler
    kapt deps.dagger.compiler
    

    Please create a `MyApplicationAppInjector class

    class MyApplicationAppInjector : AppInjector()  {
    fun init(app: ApplicationController) {
        DaggerMyAppComponent.builder().application(app)
                .build().inject(app)
        app.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                handleActivity(activity as AppCompatActivity)
            }
    
            override fun onActivityStarted(activity: Activity) {
    
            }
    
            override fun onActivityResumed(activity: Activity) {
    
            }
    
            override fun onActivityPaused(activity: Activity) {
    
            }
    
            override fun onActivityStopped(activity: Activity) {
    
            }
    
            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {
    
            }
    
            override fun onActivityDestroyed(activity: Activity) {
    
            }
        })
    }
    }
    

    Create MyAppComponent interface as :-

     @SuppressWarnings("unchecked")
    @Singleton
    @Component(
            modules = [
                AndroidInjectionModule::class,
    )
    interface MyAppComponent {
        @Component.Builder
        interface Builder {
            @BindsInstance
            fun application(application: Application): Builder
            fun build(): MyAppComponent
        }
    
        fun inject(app: ApplicationController)
    }
    

    create a class

    open class AppInjector {
    fun handleActivity(activity: AppCompatActivity) {
        if (activity is HasAndroidInjector) {
            AndroidInjection.inject(activity)
        }
        activity.supportFragmentManager
                .registerFragmentLifecycleCallbacks(
                        object : FragmentManager.FragmentLifecycleCallbacks() {
                            override fun onFragmentCreated(
                                    fm: FragmentManager,
                                    f: Fragment,
                                    savedInstanceState: Bundle?
                            ) {
    
                                    AndroidSupportInjection.inject(f)
    
                            }
                        }, true
                )
    }
    

    }

    and in onCreate() method of your application class use this code :-

    `MyApplicationAppInjector().init(this)``
    

    I hope it will fix your problem