square/anvil

Anvil seems incompatible when using with Dagger KSP

fathonyfath opened this issue · 2 comments

Running Anvil with Dagger KSP throws the following error on :app:kspDebugKotlin tasks:

e: [ksp] .../di/ApplicationComponent.kt:19: @Component.Factory types must be nested within a @Component

I have the following Dagger configuration:

// Client.kt
class Client

// AnvilApplicationScope.kt
abstract class AnvilApplicationScope private constructor()

// ApplicationComponent.kt
@Singleton
@MergeComponent(AnvilApplicationScope::class)
interface ApplicationComponent {
    fun inject(activity: MyApplication)
    @Component.Factory
    interface Factory {
        fun create(@BindsInstance application: MyApplication): ApplicationComponent
    }
}

// ApplicationModule.kt
@ContributesTo(AnvilApplicationScope::class)
@Module
object ApplicationModule {
    @Singleton
    @Provides
    fun provideClient(): Client = Client()
}

// MyApplication.kt
class MyApplication : Application() {
    @Inject lateinit var client: Client
    override fun onCreate() {
        super.onCreate()
        DaggerApplicationComponent.factory().create(this).inject(this)
    }
}

and the following gradle build script:

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("com.google.devtools.ksp")
    id("com.squareup.anvil")
}

dependencies {
    implementation("com.google.dagger:dagger:2.48")
    ksp("com.google.dagger:dagger-compiler:2.48")
}

Switching to Dagger KAPT solves the issue.

Sorry, I thought I've searched the issue tracker before posting this issue.

@vRallev related to this issue, is there a way to use component builder/factories with dagger ksp? I get this same error when using dagger ksp and anvil