Error: cannot find symbol variable DaggerAppComponent

孤者浪人 提交于 2019-12-05 20:08:05

问题


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 and Make Module App process.

Application class:

public class BaseApplication extends Application
{
    private AppComponent appComponent;

    @Override
    public void onCreate()
    {
        super.onCreate();
        initAppComponent();
    }

    private void initAppComponent()
    {
        DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }

    public AppComponent getAppComponent()
    {
        return appComponent;
    }
}

AppComponent

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

AppModule:

@Module
public class AppModule
{
    private BaseApplication application;

    public AppModule(BaseApplication app)
    {
        application = app;
    }

    @Provides
    @Singleton
    Context provideContext()
    {
        return application;
    }

    @Provides
    Application provideApplication()
    {
        return application;
    }
}

Dependency used:

compile 'com.google.dagger:dagger-android:2.11'
compile 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
androidTestCompile 'com.google.code.findbugs:jsr305:3.0.1'

Any help in this regard will be highly appreciated.


回答1:


Seems like I was using the wrong dependencies:

compile 'com.google.dagger:dagger-android:2.x'
compile 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'

The above dependencies should be used if you are using classes in dagger.android.

The correct dependencies are:

compile 'com.google.dagger:dagger:2.x'
annotationProcessor 'com.google.dagger:dagger-compiler:2.x'



回答2:


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




回答3:


Adding the below dependency fixed the issue for me:

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



回答4:


Try, go to File and

Invalidate and Restart




回答5:


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;



回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/45251222/error-cannot-find-symbol-variable-daggerappcomponent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!