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'
Advertisement
Answer
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'