Dagger 2无法解析符号' DaggerAppComponent'

时间:2016-12-06 13:18:13

标签: android dependency-injection mvp dagger-2

我尝试使用改装2,匕首2和MVP创建一个简单的应用程序,但是我很难依赖,实际上,这是我尝试重建项目后得到的错误错误:任务执行失败':应用程序:compileDebugJavaWithJavac'

  

java.lang.StackOverflowError的

以及在我提供AppComponent的App类中:无法解析符号' DaggerAppComponent'

我会试着告诉你我的项目是什么样的,这样有人可以看到问题,首先是我的AppModule,其中包括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;
    }
}

演示模块如下所示:

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

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

和InteractorModule:

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

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

这是JokePresenter,它具有对视图和交互器的引用:

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

具有RetrofitService和JokePresenter引用的JokeInteractor:

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依赖项:

  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'

有人可以在这看到问题吗?

3 个答案:

答案 0 :(得分:6)

看看实施:

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

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

你有一个循环依赖。你需要重新考虑你的设计。我建议将InteractorPresenter分开。

一个简单的解决方案:

getRandomJoke()实施更改为返回Observable并在Presenter内订阅。并从presenter删除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());
    }
}

主讲人:

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

答案 1 :(得分:0)

确保依赖项已添加到您的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'

答案 2 :(得分:0)

确保在模块内添加AndroidSupportInjectionModule::class

像这样:

@Component(
    modules = [
        AndroidSupportInjectionModule::class,
       ....//Other module classes
    ]
)