gpt4 book ai didi

android - Dagger 2 无法解析符号 'DaggerAppComponent'

转载 作者:太空宇宙 更新时间:2023-11-03 12:28:38 26 4
gpt4 key购买 nike

我正在尝试使用 retrofit 2、dagger 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,它引用了 View 和交互器:

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'

有人能看出这里的问题吗?

最佳答案

查看以下实现:

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

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

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

一个简单的解决方案:

更改 getRandomJoke() 实现以返回 Observable 并在 Presenter 中订阅它。并从 Interactor 中删除 presenter 引用。

交互者:

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

关于android - Dagger 2 无法解析符号 'DaggerAppComponent',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40996365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com