Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2: error: [Dagger/MissingBinding] cannot solve this one

I get a [Dagger/MissingBinding] error and I can not figure out why on this error.

Here is the full error stack:

error: [Dagger/MissingBinding] java.io.File cannot be provided without an @Inject constructor or an @Provides-annotated method. java.io.File is injected at service.KeyStoreService(keyStoreFile) service.KeyStoreService is injected at di.Module.WalletRepositoryModule.getWalletRepository(…, keyStoreService) repository.WalletRepositoryInterface is provided at di.component.ApplicationComponent.getWalletRepository()

The following other entry points also depend on it: 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 class:

public class KeyStoreService implements KeyStoreServiceInterface {

    private final KeyStore keyStore;

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

WalletRepositoryModule class:

@Module
public class WalletRepositoryModule {

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

}

ApplicationComponent class:

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

}

All the other modules are/was working. It's only after adding WalletRepositoryModule I got this error. Any suggestions?

like image 820
Carlton Avatar asked Nov 06 '22 23:11

Carlton


1 Answers

you have to tell Dagger how to resolve File. I would suggest you a @Provides @Named annotated method. EG

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

and change

@Inject
public KeyStoreService(File keyStoreFile) {

in

@Inject
public KeyStoreService(@Named("KEY_STORE_FILE") File keyStoreFile) {
like image 95
Blackbelt Avatar answered Nov 13 '22 14:11

Blackbelt