1

I'm trying to implement a dagger to my Android Project with MVVM structure. I've succeeded in injecting retrofit API service to my ViewModel using Module and Component with a field injection. But I keep getting an error when I try to inject my RoomDatabase with provision method (DaggerComponent.Builder).

I'm still completely unfamiliar with the dagger, is there something wrong with my code?

DatabaseModule.java

import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;

@Module
public class DatabaseModule {

   private AppDatabase database;

   public DatabaseModule(Application application) {
       database = Room.databaseBuilder(application, AppDatabase.class, 
                  "pendingDb").build();
   }

   @Singleton
   @Provides
   AppDatabase provideDatabase(){
      return database;
   }

   @Singleton
   @Provides
   PendingTodoListDao providePendingDao(AppDatabase database){
      return database.pendingTodoListDao();
   }
}

Component

import javax.inject.Singleton;
import dagger.Component;

@Singleton
@Component(modules = {ApiModule.class, ContextModule.class, 
                      DatabaseModule.class})
public interface ApiComponent {

    void inject(ApiService service);

    void inject(DeliveredViewModel viewModel);

    void inject(ToDoViewModel viewModel);

    void inject(DeliveredDetailsViewModel viewModel);

    void inject(PendingViewModel viewModel);

    AppDatabase appDatabase();
}

PendingViewModel.java

public class PendingViewModel extends AndroidViewModel {

    @Inject
    AppDatabase db;

    public PendingViewModel(@NonNull Application application) {
        super(application);
        DaggerApiComponent.builder().databaseModule(
                 new DatabaseModule(application)).build();
    }
}
Shubham Jain
  • 2,365
  • 2
  • 17
  • 29
  • 1
    As far as I know, Dagger creates `create()` method only when none of your `Module` takes constructor parameter. The moment you add `DatabaseModule` in your graph, dagger will ask you to `build()` the component yourself as DatebaseModule expects `Application` parameter in constructor. – Sandip Fichadiya Sep 13 '19 at 13:50
  • @SandipSoni So if i have one constructor with parameter i have to change all of the create() method with build() instead? – Muhammad Faisal Sep 16 '19 at 01:59

1 Answers1

0

As @Sandip Fichadiya correctly suggested, you can use

DaggerApiComponent.create()

only when none of your modules takes constructor parameter. If even only one of them has parameters, you must use

AppComponent appComponent = DaggerAppComponent.builder()
        .appModule(new AppModule(this))
        .build();

where AppModule is

@Module
public class AppModule {
    private final Context context;

    public AppModule(Context context) {
        this.context = context;
    }

    @Provides
    public Context provideContext() {
        return this.context;
    }
}

in case, for example, one or more of your modules need a Context

Alessandro Muzzi
  • 808
  • 1
  • 12
  • 26