In my module, in my base Application class
component = DaggerCompClassComponent.builder()
.classModule(new ModuleClass()).build();
Maybe you forgot to annotate ModuleClass with @Module ?
If you have several modules in your AndroidStudio (modules in terms of Android Studio, not Dagger), another possible reason of fail is that you've forgot to put annotation processors into the all modules' build.gradle
.
We've divided our app into several modules, updated dependencies from using implementation
to using api
but forgot to handle annotation processors accordingly.
So, you can have this lines only in a root module:
api 'com.google.dagger:dagger-android:2.16'
// if you use the support libraries
api 'com.google.dagger:dagger-android-support:2.16'
But this ones should be specified in all modules dependencies:
annotationProcessor 'com.google.dagger:dagger-compiler:2.16'
// if you use injections to Android classes
annotationProcessor 'com.google.dagger:dagger-android-processor:2.16'
api 'com.google.dagger:dagger-android:2.28.3'
api 'com.google.dagger:dagger-android-support:2.28.3'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.28.3'
Replace above with below dependencies in app level dependencies, if you have used above
api 'com.google.dagger:dagger:2.28.3'
annotationProcessor 'com.google.dagger:dagger-compiler:2.28.3'
If you are using Kotlin, make sure to add kapt
(Kotlin annotation processor) Gradle plugin to your build script and use kapt
Gradle dependency type instead of annotationProcessor
for Dagger Compiler.
apply plugin: 'kotlin-kapt
kapt deps.daggercompiler
implementation deps.dagger
There are some minor misconceptions/faults in your code above, here's a working implementation:
Application.java:
component = DaggerComponentClass.builder().classModule(new ModuleClass()).build();
The generated class will be named DaggerComponentClass
, not DaggerCompClassComponent
. If you can't run your app in Android Studio to get it built, try Build->Clean project and Build->Rebuild project in the menu. If everything is OK Dagger will have compiled DaggerComponentClass
which will be located in the same package as ComponentClass
.
ComponentClass.java:
@Component(modules = ModuleClass.class)
public interface ComponentClass {
void inject(AClassThatShouldGetInstancesInjected instance);
}
A Component in Dagger2 has methods named inject
that receive the instance to get instances injected into it, not the other way around. In the code above the class AClassThatShouldGetInstancesInjected
will typically call componentClass.inject(this);
to get instances injected into itself.
ModuleClass.java:
@Module
public class ModuleClass {
@Provides
@Singleton
public Interceptor provideInterceptor() {/*code*/}
//Your Providers...
}
The Module is correct in your code, make sure its annotated.
//add all the dependencies otherwise component class will not be generated.
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
implementation 'com.google.dagger:dagger:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'