1

I am learning Dagger 2 from this site https://developer.android.com/training/dependency-injection/dagger-android. I am creating like this,

Appcomponent.java

@Singleton
@Component(modules = {AppModule.class, AuthModule.class})
public interface AppComponent {
    AuthComponent.Factory authComponent();
}

AppModule.java

@Module
public class AppModule
{}

AuthComponent.java

@FeatureScope
@Subcomponent
public interface AuthComponent {

    @Subcomponent.Factory
    interface Factory{
        AuthComponent create();
    }


}

AuthModule.java


@Module(subcomponents = AuthComponent.class)
public class AuthModule {

}

But I got the error,AuthComponent doesn't have a @Subcomponent.Builder, which is required when used with @Module.subcomponents @Module(subcomponents = AuthComponent.class)I am doing as the documentation. Please help me explain with my error.

Edit This is my dagger version.


 //di
    api 'com.google.dagger:dagger-android:2.35.1'
    api 'com.google.dagger:dagger:2.35.1'
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.20'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.20'

pak
  • 55
  • 5
  • The code you posted looks correct; I built this based on the files you posted with Dagger 2.41, and was unable to reproduce your error message. Can you please edit your question to include your build or gradle file? `@Subcomponent.Factory` is [new in Dagger 2.22 from April 2019](https://github.com/google/dagger/releases/tag/dagger-2.22); if you can refer to it without a compile error, you're likely using a newer version of the _implementation library_, but if the _annotation processor library_ is older it might emit that message. – Jeff Bowman Mar 12 '22 at 21:40
  • Thank you for adding those two `api` lines. Can you please edit in your [`annotationProcessor lines`](https://github.com/google/dagger#gradle), specifically `dagger-compiler` and `dagger-android-processor`? I suspect that those two are the ones that might be mismatched. (Also see the note in the link about favoring `implementation` over `api`.) – Jeff Bowman Mar 13 '22 at 16:02
  • I am creating a Multi Module project. I don' want to add Dagger in each module over and over again. So I use `api` in my core module. – pak Mar 14 '22 at 07:00

1 Answers1

1

As expected, your code generator is at version 2.20, which is before @Subcomponent.Factory was introduced in 2.22. This causes the annotation processor step to fail because it was written before @Subcomponent.Factory existed, even though in your project you can compile references to it since you're including the api for Dagger 2.35.1. Newer versions of the annotation processor properly know to check for either a Factory or a Builder.

In any case, you should never use a different version of the annotationProcessor lines compared to the api or implementation lines: this has caused other difficult-to-diagnose errors before.

The placeholder version 2.x no longer seems to be available, so instead change all dagger lines to the same value (2.35.1) or the latest value (2.41 as of March 2022).

api 'com.google.dagger:dagger-android:2.35.1'
api 'com.google.dagger:dagger:2.35.1'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.35.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.35.1'
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251