匕首2:错误:[Dagger / MissingBinding]无法解决此问题

时间:2018-12-04 15:11:10

标签: android dependency-injection dagger-2

我遇到一个[Dagger/MissingBinding]错误,我不知道为什么会出现此错误。

这是完整的错误堆栈:

  

错误:[Dagger / MissingBinding]在没有以下情况下无法提供java.io.File:   @Inject构造函数或@Provides注释的方法。 java.io.File   在service.KeyStoreService(keyStoreFile)注入   service.KeyStoreService被注入在   di.Module.WalletRepositoryModule.getWalletRepository(…,   keyStoreService)存储库。WalletRepositoryInterface在以下位置提供   di.component.ApplicationComponent.getWalletRepository()

     

以下   其他入口点也取决于它:   dagger.android.AndroidInjector.inject(T)   [di.component.ApplicationComponent吗?   di.Module.BindModule_BindStartModule.StartActivitySubcomponent]   dagger.android.AndroidInjector.inject(T)   [di.component.ApplicationComponent吗?   di.Module.BindModule_BindAddWalletActivity.AddWalletActivitySubcomponent]

KeyStoreService类:

public class KeyStoreService implements KeyStoreServiceInterface {

    private final KeyStore keyStore;

    @Inject
    public KeyStoreService(File keyStoreFile) {
        keyStore = new KeyStore(keyStoreFile.getAbsolutePath(), Geth.LightScryptN, Geth.LightScryptP);
    }
}

WalletRepositoryModule类:

@Module
public class WalletRepositoryModule {

    @Provides
    @ApplicationScope
    WalletRepositoryInterface getWalletRepository(SharedPreferencesHelper sharedPreferencesHelper, KeyStoreService keyStoreService){
        return new WalletRepository(sharedPreferencesHelper, keyStoreService);
    }

}

ApplicationComponent类:

@ApplicationScope
@Component(modules = {ApplicationContextModule.class,
        SharedPreferencesModule.class,
        KeyStoreModule.class,
        SharedPreferenceHelperModule.class,
        AndroidInjectionModule.class,
        AndroidsupportInjectionModule.class,
        WalletRepositoryModule.class})
public interface ApplicationComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(MyApplication myApplication);
        ApplicationComponent build();
    }

    void inject(MyApplication myApplication);

    @ApplicationContext
    Context getApplicationContext();

    SharedPreferences getSharedPreferences();

    KeyStoreServiceInterface getKeyStoreService();

    SharedPreferencesHelper getSharedPreferencesHelper();

    WalletRepositoryInterface getWalletRepository();

}

所有其他模块正在/正在工作。只有在添加WalletRepositoryModule之后,我才收到此错误。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您必须告诉Dagger如何解决File。我建议您使用一种@Provides @Named注释的方法。 EG

  @Provides
  @Named("KEY_STORE_FILE") 
  public File provideKeyStoreFile() {
     return new File(path/to/keystore)
  }

并更改

@Inject
public KeyStoreService(File keyStoreFile) {

@Inject
public KeyStoreService(@Named("KEY_STORE_FILE") File keyStoreFile) {