Error: Cannot find symbolic variable DaggerAppComponent

While trying to integrate the latest version of Dagger 2, I ran into a dagger generation issue. Dagger does not automatically generate DaggerAppComponent, despite multiple Rebuilds and Make Module App processes.

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;
    }
}

      

Used dependency:

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 would be much appreciated.

+10


source to share


7 replies


It looks 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 .android dagger.



Correct dependencies:

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

      

+23


source


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. A generated class like Dagger + AppComponentClass, for example: DaggerAppComponent

+3


source


Adding the below dependency fixed the problem for me:

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

      

+2


source


Try it, go to File and

Cancel and restart

+1


source


I had the same problem ... What solved my problem was adding the ViewModel to the ViewmodelModulle and then adding the @Inject annotation to the constructor of my ViewModel. This might be a different problem for you, but it really helped in my situation. My code compiled with no problem

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

public MessageViewModel(Application application) {
    super(application);
    mApp = application;

      

0


source


1. Clean the project 2.Rebuild 3. File -> invalidate and restart

0


source


Enter the Chengue code,

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

      

Other things

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

      

Why you need to inject the same class in which the component is created, you can easily get the context and application in the Application class. The dagger can help you find the dependent class.

-1


source







All Articles