简体   繁体   English

错误:尝试构建项目时出现[Dagger / MissingBinding]

[英]error: [Dagger/MissingBinding] when trying building the project

I have a service class and I want to provide it via Dagger. 我有一个服务类,我想通过Dagger提供它。 But I get this error below: 但是我在下面得到这个错误:

error: [Dagger/MissingBinding] service.KeyStoreService cannot be provided without an @Inject constructor or an @Provides-annotated method. 错误:[Dagger / MissingBinding] service.KeyStoreService如果没有@Inject构造函数或@Provides注释的方法,将无法提供。 service.KeyStoreService is provided at di.component.ApplicationComponent.getKeyStoreService() 在di.component.ApplicationComponent.getKeyStoreService()处提供了service.KeyStoreService

Here is my component class: 这是我的组件类:

@ApplicationScope
@Component(modules = {ApplicationContextModule.class, KeyStoreModule.class})
public interface ApplicationComponent {

    @ApplicationContext
    Context getApplicationContext();

    KeyStoreService getKeyStoreService();

}

Here is my KeyStoreModule: 这是我的KeyStoreModule:

@Module(includes = {ApplicationContextModule.class})
public class KeyStoreModule {

    @Provides
    @ApplicationScope
    KeyStoreServiceInterface getKeyStoreService(@ApplicationScope Context context){
        File file = new File(context.getFilesDir(), "keystore/keystore");
        return new KeyStoreService(file);
    }
}

KeyStoreService implements KeyStoreServiceInterface. KeyStoreService实现KeyStoreServiceInterface。

Here is how I start Dagger2: 这是我启动Dagger2的方法:

public class MyApplication extends Application {

    private ApplicationComponent applicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        applicationComponent = DaggerApplicationComponent.builder()
                .applicationContextModule(new ApplicationContextModule(this))
                .build();

    }

}

Anyone see where it could have gone wrong? 有人看到它可能出问题了吗? I looked at similar questions on Stackoverflow but did not find anything that helped me. 我在Stackoverflow上查看了类似的问题,但没有发现任何对我有帮助的东西。

Here is a thing: Dagger provides instances based on specific type . 这件事:Dagger提供基于特定类型的实例。 Here are a couple of ways to deal with this problem 以下是解决此问题的几种方法

  • Change the return type of getKeyStoreService method from KeyStoreServiceInterface to KeyStoreService in KeyStoreModule getKeyStoreService方法的返回类型从KeyStoreServiceInterface更改为KeyStoreService中的KeyStoreModule
  • Change the return type of getKeyStoreService method from KeyStoreService to KeyStoreServiceInterface in ApplicationComponent getKeyStoreService方法的返回类型从KeyStoreService更改为ApplicationComponent KeyStoreServiceInterface
  • Create an abstract method with @Binds annotation which will receive KeyStoreService and which return type will be KeyStoreServiceInterface (in order to do this the whole module should be abstract - so it is possible to either create separate abstract module with @Binds annotations or to make KeyStoreModule abstract and modify getKeyStoreService method to be static) 创建带有@Binds批注的抽象方法,该方法将接收KeyStoreService ,返回类型为KeyStoreServiceInterface (为此,整个模块应该是抽象的-因此可以使用@Binds批注创建单独的抽象模块,也可以制作KeyStoreModulegetKeyStoreService方法抽象并修改为静态)
  • Again, using @Binds annotation to map KeyStoreService instance to the provided KeyStoreServiceInterface , but applying @Inject annotation on KeyStoreService constructor and provide keystore File through Dagger 同样,使用@Binds批注将KeyStoreService实例映射到提供的KeyStoreServiceInterface ,但在KeyStoreService构造函数上应用@Inject批注并通过Dagger提供密钥库File
  • Not using @Binds annotation, but applying @Inject annotation on KeyStoreService constructor and provide keystore File through Dagger 不使用@Binds批注,而是在KeyStoreService构造函数上应用@Inject批注,并通过Dagger提供密钥库File

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM