anvil icon indicating copy to clipboard operation
anvil copied to clipboard

@Component.Factory types must be nested within a @Component

Open mustafatatarhan0 opened this issue 1 year ago • 4 comments

AppComponent.kt:

@SingleIn(AppScope::class)
@MergeComponent(AppScope::class)
interface AppComponent : NodeFactoriesBindings {

    @Component.Factory
    interface Factory {
        fun create(
            @ApplicationContext @BindsInstance
            context: Context,
        ): AppComponent
    }
}

When anvil generate AppComponent.java, error comes: error: @Component.Factory types must be nested within a @Component

What is the reason of this error ? How can I fix this ?

mustafatatarhan0 avatar Nov 21 '23 06:11 mustafatatarhan0

I too have the same problem, and with @Component.Builder

b-reetz avatar Feb 13 '24 10:02 b-reetz

I have the same issue building with ksp, @mustafatatarhan0 are you building with ksp as well?

kevinguitar avatar Apr 15 '24 07:04 kevinguitar

@kevinguitar I ran into this while trying to use Dagger KSP, too, yeah. I think in short, the deal is that Anvil doesn't currently support Dagger KSP.

However, this draft PR https://github.com/square/anvil/pull/1001 went up 2 days ago, which is directly about getting the @Component.Factory and @Component.Builder to work under an @MergeComponent. Very exciting stuff!

jamesonwilliams avatar May 16 '24 06:05 jamesonwilliams

Edit: I just found out that component merging will not work regardless of K2 KAPT or KSP and needs to use the workarounds described in the link

I got this error even with K2 Kapt enabled:

@Subcomponent.Factory types must be nested within a @Subcomponent
    public static abstract interface Factory {
                           ^

The component in question looks like this:

@SingleIn(UserScope::class)
@MergeSubcomponent(UserScope::class)
interface UserComponent {

    @Subcomponent.Factory
    interface Factory {
        fun create(
            @BindsInstance userSession: UserSession,
            @BindsInstance userCoroutineScope: UserCoroutineScope,
        ): UserComponent
    }

    fun inject(activity: MainActivity)
}

I am using 2.5.0-beta09 with Kotlin 2.0.0

itsandreramon avatar Jul 12 '24 18:07 itsandreramon