简体   繁体   English

没有@Provides注释的方法就无法提供对象

[英]Objects cannot be provided without an @Provides-annotated method

I've started to setup Dagger 2 and faced a strange issue that looks like a bug to me. 我已经开始设置Dagger 2,并且遇到了一个奇怪的问题,对我来说似乎是个错误。

I have 1 main component and 1 subcomponent which I 'plus' in parent component. 我有1个主要组成部分和1个子组成部分,我在父组成部分中“加了”。

I use: 我用:

compile "com.google.dagger:dagger:2.4"
apt "com.google.dagger:dagger-compiler:2.4"
annotationProcessor "com.google.dagger:dagger-compiler:2.4"

Application component is prety easy. 应用程序组件非常简单。 It just 'plus' the subcomponent: 它只是“加上”子组件:

@Singleton @Component(modules = { AristocratsAppModule.class })
public interface AristocratsAppComponent {
  StreamComponent plus(StreamModule module);
}

Application module also quite basic. 应用模块也相当基本。 It provides application level dependancies: 它提供了应用程序级别的依赖关系:

@Module public class AristocratsAppModule {
  private static AristocratsApp app;

  public AristocratsAppModule(AristocratsApp app) {
    AristocratsAppModule.app = app;
  }

  @Provides @Singleton AristocratsApp providesApplication() {
    return app;
  }

  @Provides @Singleton GsonXml providesGsonXml() {
    return ...
  }

  @Provides @Singleton @Named("aristocrats") Retrofit providesAristocratsRetrofit() {
    return ...
  }

  @Provides @Singleton @Named("last_fm") Retrofit providesLastFmRetrofit() {
    return ...
  }
}

Subcomponent has two StreamServices (Retrofit services) and simple inject: 子组件具有两个StreamServices(改造服务)和简单注入:

@PerController @Subcomponent(modules = { StreamModule.class }) public interface StreamComponent {
  void inject(StreamsController streamsController);

  AristocratsStreamService providesAristocratsStreamService();

  LastFmStreamService providesLastFmStreamService();
}

Module of subcomponent provides presenter injection and two different injections of Retrofit services: 子组件模块提供演示者注入和两种不同的翻新服务注入:

@Module public class StreamModule {
  private StreamsController mView;

  public StreamModule(StreamsController view) {
    mView = view;
  }

  @Provides @PerController StreamMvp.Presenter providesPresenter(StreamCase streamCase,
      StreamModelMapper streamMapper) {
    return new StreamPresenter(mView, streamCase, streamMapper);
  }

  @Provides @PerController AristocratsStreamService providesAristocratsStreamService(
      @Named("aristocrats") Retrofit retrofit) {
    return retrofit.create(AristocratsStreamService.class);
  }

  @Provides @PerController LastFmStreamService providesLastFmStreamService(
      @Named("last_fm") Retrofit retrofit) {
    return retrofit.create(LastFmStreamService.class);
  }
}

Application injection that I call in onCreate() of my app class: 我在应用程序类的onCreate()中调用的应用程序注入:

mAppComponent = DaggerAristocratsAppComponent.builder()
        .aristocratsAppModule(new AristocratsAppModule(this))
        .build();

View injection that I call in my controller: 查看我在控制器中调用的注入:

getAppComponent().plus(new StreamModule(this)).inject(this);

Exception that I'm getting during build: 我在构建过程中遇到的异常:

Error:(13, 8) error: [com.qsoft.streams.presentation.di.StreamComponent.inject(com.qsoft.streams.presentation.StreamsController)] com.qsoft.streams.data.network.AristocratsStreamService cannot be provided without an @Provides-annotated method.
com.qsoft.streams.data.network.AristocratsStreamService is injected at
com.qsoft.streams.data.network.StreamApi.<init>(…, streamServiceAristocrats, …)
com.qsoft.streams.data.network.StreamApi is injected at
com.qsoft.streams.data.repository.StreamRepository.<init>(streamApi, …)
com.qsoft.streams.data.repository.StreamRepository is injected at
com.qsoft.streams.domain.interactors.StreamCase.<init>(streamRepository)
com.qsoft.streams.domain.interactors.StreamCase is injected at
com.qsoft.streams.presentation.di.StreamModule.providesPresenter(streamCase, …)
com.qsoft.streams.presentation.StreamMvp.Presenter is injected at
com.qsoft.streams.presentation.StreamsController.mPresenter
com.qsoft.streams.presentation.StreamsController is injected at
com.qsoft.streams.presentation.di.StreamComponent.inject(streamsController)
A binding with matching key exists in component: com.qsoft.streams.presentation.di.StreamComponent

Error complaints about missing @Provides-annotated method but they are inside my child module. 关于缺少@Provides-annotated method错误投诉,但是它们在我的子模块中。 Seems that Dagger2 just does not see them. 似乎Dagger2只是看不到它们。 I was thinking that the problem could be in the missing declaration of the AristocratsStreamService in subcomponent, so I even added it there but nothing changed. 我以为问题可能出在子组件中缺少AristocratsStreamService声明中,所以我什至在这里添加了它,但没有任何改变。

This situation looks very strange and I would like to hear some inputs from more experienced Dagger2 developers. 这种情况看起来很奇怪,我想听听更有经验的Dagger2开发人员的一些意见。

Thank you in advance! 先感谢您!

I have noticed that you did not set subcomponents part in AristrocratsModule. 我注意到您没有在AristrocratsModule中设置子组件部分。

This: 这个:

@Module public class AristocratsAppModule {

into this: 到这个:

@Module(subcomponents = {StreamComponent.class})
public class AristocratsAppModule {

Try to make the following changes. 尝试进行以下更改。 In your StreamComponent create a Builder. 在您的StreamComponent中创建一个Builder。

@Subcomponent.Builder
interface Builder {
    Builder streamModule(StreamModule module);

    StreamComponent build();
}

Eventually you will be injecting everything via StreamComponent, and it will inject everything in AristocratsAppComponent as well. 最终,您将通过StreamComponent注入所有内容,并且还将在AristocratsAppComponent中注入所有内容。

public StreamComponent getStreamComponent(Controller controller) {
    return getAppComponent().streamBuilder().streamModule(new StreamModule(controller)).build();
}

Plus thing can be removed after this. 再加上可以在此之后删除的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有@Provides注释的方法无法提供 - Cannot be Provided Without an @Provides-annotated Method 如果没有@Provides 注释的方法 Dagger/MissingBinding,则无法提供 - cannot be provided without an @Provides-annotated method Dagger/MissingBinding 没有@Annex-annotated方法就无法提供上下文,但它是? - Context cannot be provided without an @Provides-annotated method, but it is? Android匕首2:如果没有@Provides注释的方法,则无法提供依赖项 - Android dagger 2: dependency cannot be provided without an @Provides-annotated method 不能在没有 @Provides 注释的方法的情况下提供(Android Kotlin 中的 Dagger 2) - Cannot be provided without an @Provides-annotated method (Dagger 2 in Android Kotlin) Dagger 2错误:依赖项“如果没有@Provides注释的方法就无法提供” - Dagger 2 error: dependency “cannot be provided without an @Provides-annotated method" 错误:如果没有 @Provides-annotated 方法就无法提供 - Error: cannot be provided without an @Provides-annotated method 匕首柄:如果没有@Provides-annotated 方法,则无法提供 - Dagger Hilt: cannot be provided without an @Provides-annotated method 没有@Provides注释的方法,无法提供该类 - The class cannot be provided without an @Provides-annotated method 不能在没有 @Inject 构造函数或从 @Provides-annotated 方法的情况下提供 - cannot be provided without an @Inject constructor or from an @Provides-annotated method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM