square / anvil

A Kotlin compiler plugin to make dependency injection with Dagger 2 easier.
Apache License 2.0
1.31k stars 82 forks source link

Anvil seems incompatible when using with Dagger KSP #747

Closed fathonyfath closed 1 year ago

fathonyfath commented 1 year ago

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.

fathonyfath commented 1 year ago

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

aitorvs commented 1 year ago

@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