Dagger 2 component not generated

前端 未结 12 1552
走了就别回头了
走了就别回头了 2021-01-31 07:21

In my module, in my base Application class

component = DaggerCompClassComponent.builder()
                .classModule(new ModuleClass()).build();
相关标签:
12条回答
  • 2021-01-31 07:56

    Maybe you forgot to annotate ModuleClass with @Module ?

    0 讨论(0)
  • 2021-01-31 07:57

    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'
    
    0 讨论(0)
  • 2021-01-31 08:04
    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'
    
    0 讨论(0)
  • 2021-01-31 08:06

    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
    
    0 讨论(0)
  • 2021-01-31 08:09

    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.

    0 讨论(0)
  • 2021-01-31 08:09
    //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'
    
    0 讨论(0)
提交回复
热议问题