14

I'm trying to get my head around the new Dagger2 APIs and support for Android. I'm using Dagger2 version 2.15:

implementation 'com.google.dagger:dagger:2.15'
implementation 'com.google.dagger:dagger-android:2.15'
implementation 'com.google.dagger:dagger-android-support:2.15'
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.15'

Now in this version there are some classes like DaggerApplication and DaggerAppCompatActivity but I'm not sure how to get them to work.

This is what I've done so far:

My Application class which I added in the manifest:

class BaseApplication : DaggerApplication() {
    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        return DaggerAppComponent.builder().create(this)
    }
}

My AppComponent:

@Singleton
@Component(modules = [
    AndroidSupportInjectionModule::class
])
interface AppComponent : AndroidInjector<BaseApplication> {
    @Component.Builder
    abstract class Builder : AndroidInjector.Builder<BaseApplication>()
}

And my base Activity class that I extend in any other activity that I create:

abstract class BaseActivity : DaggerAppCompatActivity() {
}

The problem is that when I try to make or build the project it fails and Dagger doesn't generate DaggerAppComponent for me. What do I miss?

Reza Bigdeli
  • 1,142
  • 1
  • 12
  • 25

1 Answers1

24

Need more info but try this AppComponent

@Singleton
@Component(modules = [AndroidSupportInjectionModule::class])
interface ApplicationComponent : AndroidInjector<YourApplication> {
    override fun inject(application: YourApplication)

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: YourApplication): Builder

        fun build(): ApplicationComponent
    }
}

And from your application class

class YourApplication : DaggerApplication() {
    private val applicationInjector = DaggerApplicationComponent.builder()
        .application(this)
        .build()

    override fun applicationInjector() = applicationInjector
}

Also use kapt instead of annotationProcessor from your build.gradle :

apply plugin: 'kotlin-kapt'
...
kapt 'com.google.dagger:dagger-compiler:2.15'
kapt 'com.google.dagger:dagger-android-processor:2.15'
Samuel Eminet
  • 4,647
  • 2
  • 18
  • 32
  • Did that, didn't work! I get the same error. What info you need? – Reza Bigdeli Mar 31 '18 at 15:54
  • You need to build project at least one time to get your DaggerAppComponent class generated, then you should be able to add the import from your appication class – Samuel Eminet Mar 31 '18 at 15:58
  • And is BaseApplication an abstract class or a concrete one? because you have to use a croncrete one from dagger – Samuel Eminet Mar 31 '18 at 16:01
  • It is actually a concrete one. I'm also commenting the `DaggerAppComponent` related codes in the `BaseApplication` to build the project successfully but even after the build the `DaggerAppComponent` is not generated! – Reza Bigdeli Mar 31 '18 at 16:07
  • Try the kapt thing i just added to my answer – Samuel Eminet Mar 31 '18 at 16:10
  • 4
    God damn it.. It was the `kapt`... Thank you so much – Reza Bigdeli Mar 31 '18 at 16:15
  • Be careful leaking `this` from the constructor (`init`), if your modules have a provide method that use the Application or Context, it'll use an uninitialized object, and even worse an unattached Context. You should only create the component after it has been `super.onCreate()`. The example above works only for trivial Dagger graphs by luck. – TWiStErRob Sep 08 '20 at 08:23