Error: cannot find symbol variable DaggerAppComponent

后端 未结 8 1677
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-19 08:50

While trying to integrate latest Dagger 2 version, I am facing problem of Dagger auto generation. Dagger is not auto generating DaggerAppComponent in spite of several Rebuilds a

相关标签:
8条回答
  • 2021-02-19 09:19

    Adding the below dependency fixed the issue for me for Java project

    annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
    

    and for Kotlin project (after adding kotlin-kapt):

    kapt "com.google.dagger:dagger-compiler:2.27"
    
    0 讨论(0)
  • 2021-02-19 09:20

    I had the same problem...What solved my problem was actually adding The view model to the ViewmodelModulle then add the annotation @Inject to the constructor of my view model. It could be a different issue for you but in my situation, this really helped. My code compiled without any problem

    @Inject <----- This was Missing in the constructor.

    public MessageViewModel(Application application) {
        super(application);
        mApp = application;
    
    0 讨论(0)
  • 2021-02-19 09:23

    You need these two lines

    implementation 'com.google.dagger:dagger:2.16'
    kapt 'com.google.dagger:dagger-compiler:2.16'
    

    Use kapt instead of annotationProcessor when using kotlin. Generated class like Dagger+AppComponentClass, eg:DaggerAppComponent

    0 讨论(0)
  • 2021-02-19 09:26

    1.Clean the project 2.Rebuild 3.File-->Invalidate and Restart

    0 讨论(0)
  • 2021-02-19 09:33

    Chenge code to this,

     private void initAppComponent()
    {
    /*    DaggerAppComponent.builder()
                        .appModule(new AppModule(this))
                        .build();*/
            appComponent = DaggerAppComponent.builder()
                    .appModule(new AppModule(this))
                    .build();
            appComponent .inject(this)
        }
    

    Other things are

    @Singleton
    @Component(modules = AppModule.class)
    public interface AppComponent
    {
        void inject(BaseApplication application);
    }
    

    Why you need to inject same class where a component is built you can easily get context and application on Application class. Dagger can help you to find a dependent class.

    0 讨论(0)
  • 2021-02-19 09:36

    Try, go to File and

    Invalidate and Restart

    0 讨论(0)
提交回复
热议问题