Dagger 2 can not resolve symbol 'DaggerAppComponent'
Asked Answered
B

4

12

I'm trying to create a simple app with retrofit 2, dagger 2 and MVP, but I struggle with dependencies, actualy, this is the error I get after i try to rebuild the project Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

java.lang.StackOverflowError

and also in App class where I provide AppComponent: can not resolve symbol 'DaggerAppComponent'

I'll try to show you what my project looks like so someone can see the problem, First one is my AppModule which includes PresentationModule.class

@Module(includes = PresentationModule.class)
public class AppModule {
    private App app;

    public AppModule(App app) {
        this.app = app;
    }

    @Provides
    @Singleton
    public App provideApp() {
        return app;
    }
}

Presentation Module looks like this:

@Module(includes = InteractorModule.class)
public class PresentationModule {

    @Provides
    JokePresenter providePresenter(JokeInteractor jokeInteractor) {
        return new JokePresenterImpl(jokeInteractor);
    }
}

And InteractorModule:

@Module(includes = {NetworkModule.class, PresentationModule.class})
public class InteractorModule {

    @Provides
    JokeInteractor provideJokeInteractor(RetrofitService service, JokePresenter presenter) {
        return new JokeInteractorImpl(service, presenter);
    }
}

This is JokePresenter that has a reference to view and interactor:

public class JokePresenterImpl implements JokePresenter {

    private JokeView mJokeView;
    private List<String> mData = new ArrayList<>();
    private String mJoke;
    private JokeInteractor jokeInteractor;

    public JokePresenterImpl(JokeInteractor jokeInteractor) {
        this.jokeInteractor = jokeInteractor;
    }

    @Override
    public void setView(JokeView view) {
        this.mJokeView = view;
    }

    @Override
    public void getRandomJoke() {
        mJokeView.showProgress();
        jokeInteractor.getRandomJoke();
    }
}

And JokeInteractor that has a RetrofitService and JokePresenter references:

public class JokeInteractorImpl implements JokeInteractor {

    private RetrofitService retrofitService;
    private JokePresenter presenter;

    public JokeInteractorImpl(RetrofitService service, JokePresenter presenter) {
        this.retrofitService = service;
        this.presenter = presenter;
    }

    @Override
    public void getRandomJoke() {
        retrofitService.getRandomJoke()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RandomJokeResponse>() {
                    @Override
                    public void onCompleted() {
                        presenter.onRandomJokeCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.onError(e.getMessage());
                    }

                    @Override
                    public void onNext(RandomJokeResponse randomJokeResponse) {
                        presenter.onNextRandomJoke(randomJokeResponse);
                    }
                });
    }

Gradle dependencies:

  apt 'com.google.dagger:dagger-compiler:2.7'
  compile 'com.google.dagger:dagger:2.7'
  provided 'javax.annotation:jsr250-api:1.0'

    //retrofit
  compile 'com.squareup.retrofit2:retrofit:2.1.0'
  compile 'com.squareup.okhttp:okhttp:2.5.0'
  compile 'com.squareup.retrofit2:converter-gson:2.1.0'
  compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

  //rx java
  compile 'io.reactivex:rxjava:1.1.6'
  compile 'io.reactivex:rxandroid:1.2.1'

 //dagger2
  compile 'com.google.dagger:dagger:2.0'
  provided 'org.glassfish:javax.annotation:10.0-b28'

Can someone see the problem here?

Braunstein answered 6/12, 2016 at 13:18 Comment(6)
Did you create a Component interface that handles the injection? If not, let me know and I am happy to provide an example.Prefer
You should show your gradle dependenciesEclosion
Yes, I have a component interface that has AppModule, and methods where presenter is injected void inject(App app); void inject(JokeActivity activity); void inject(JokeListActivity activity); void inject(JokeDialog dialog);Braunstein
@Eclosion I added my dependenciesBraunstein
Why do you have compile 'com.google.dagger:dagger:2.7' AND compile 'com.google.dagger:dagger:2.0' ?Eclosion
Take a look at this article: medium.com/me/stats and this sample project: github.com/mmirhoseini/marvel I hope they help.Pickwickian
I
8

Look at the implementation of:

@Module(includes = InteractorModule.class)
public class PresentationModule

and

@Module(includes = {NetworkModule.class, PresentationModule.class})
public class InteractorModule

You have a cyclic dependency. You need to rethink your design. I propose to decouple Interactor from Presenter.

A simple solution:

Change getRandomJoke() implementation to return Observable and subscribe to it inside Presenter. And remove presenter reference from Interactor.

Interactor:

public class JokeInteractorImpl implements JokeInteractor {

    private RetrofitService retrofitService;

    public JokeInteractorImpl(RetrofitService service) {
        this.retrofitService = service;
    }

    @Override
    public Observable getRandomJoke() {
        return retrofitService.getRandomJoke()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
    }
}

Presenter:

@Override
public void getRandomJoke() {
    mJokeView.showProgress();
    jokeInteractor.getRandomJoke()
          .subscribe(new Observer<RandomJokeResponse>() {
                @Override
                public void onCompleted() {
                    onRandomJokeCompleted();
                }

                @Override
                public void onError(Throwable e) {
                    onError(e.getMessage());
                }

                @Override
                public void onNext(RandomJokeResponse randomJokeResponse) {
                    onNextRandomJoke(randomJokeResponse);
                }
            });
}
Isomer answered 6/12, 2016 at 13:36 Comment(1)
This seems like a great solution, it fixed my problem! Ty, cheers!Braunstein
R
3

Make sure that dependencies added in your gradle

implementation 'com.google.dagger:dagger-android:2.15'
implementation 'com.google.dagger:dagger-android-support:2.13'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.16'
Ricotta answered 14/10, 2019 at 7:27 Comment(1)
Not having annotation processor was the exactly problem I have. Thank you so much man!Charlottetown
C
0

Make sure adding AndroidSupportInjectionModule::class inside your modules.

Like this:

@Component(
    modules = [
        AndroidSupportInjectionModule::class,
       ....//Other module classes
    ]
)
Ciccia answered 20/5, 2020 at 0:26 Comment(0)
M
0

This is what worked for me (Solution) :-

If you have any other build issues related to Dagger apart from DaggerAppComponent import issue, then make sure to fix them first, if not we will just end up wasting time.

If you fix the other Dagger issues, & then build your project. The DaggerAppComponent will get generated, & then all you have to do is import it.

Brief info regarding the issue that I faced :-

The mistake that I did was that, I had another Dagger build issue, but I ignored it & spent my time trying to fix 'DaggerAppComponent' import problem.

Wont be relevant to you but in my case, the DI module class annotated with @Module had a single abstract method, & hence the module class had to be made abstract as well. But later accidentally I added 2 other non-abstract methods within this same abstract class. So when I fixed this & re-build the project. DaggerAppComponent was available for import.

This is all what I have in my app level build.gradle file in relation to dagger :-

Within plugins block :

id 'kotlin-kapt'

Within dependencies block :

implementation "com.google.dagger:dagger:2.43.2"
kapt "com.google.dagger:dagger-compiler:2.43.2"

(Please note that "2.43.2" was the latest available version at the time of posting this comment)

My failed attempts (These answers didn't work for me) :-

  • Clean > Re-build > Invalidates caches & restart.
  • Switching to Project view, deleting build folder & re-building the project.
  • Trying out various dagger versions & extra dagger dependencies that I didnt require.
Menswear answered 21/8, 2022 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.