Dagger 2 component not generated

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

In my module, in my base Application class

component = DaggerCompClassComponent.builder()
                .classModule(new ModuleClass()).build();
12条回答
  •  执念已碎
    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.

提交回复
热议问题