0

I tried to use dagger 2 for the first time in one of my apps. Im getting error at dagger component class as the class did not get generated. please help me to get it work.

Link to my app https://github.com/kantigaricharan/Zolo

This the Error

    02:45:55.663 [ERROR] [system.err] /Users/vamsikrishna/Downloads/Zolo/app/src/main/java/com/example/saicharan/zolo/dagger/component/AppComponent.java:17: error: com.example.saicharan.zolo.dashboard.DashboardInteractorImpl cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
02:45:55.663 [ERROR] [system.err]     void inject(MyApp myApp);
02:45:55.663 [ERROR] [system.err]          ^
02:45:55.663 [ERROR] [system.err]       com.example.saicharan.zolo.dashboard.DashboardInteractorImpl is injected at
02:45:55.663 [ERROR] [system.err]           com.example.saicharan.zolo.MyApp.dashboardInteractor
02:45:55.664 [ERROR] [system.err]       com.example.saicharan.zolo.MyApp is injected at
02:45:55.664 [ERROR] [system.err]           com.example.saicharan.zolo.dagger.component.AppComponent.inject(myApp)
02:45:55.722 [ERROR] [system.err] 1 error

This is my Component class in dagger

   @Singleton @Component(modules = AppModule.class)
public interface AppComponent {
    void inject(MyApp myApp);
    void inject(DashboardInteractorImpl dashboardInteractorImpl);
}

This is module

    @Module
public class AppModule {
    private final MyApp myApp;
    public AppModule(MyApp myApp){this.myApp=myApp;}

    @Provides @Singleton
    Context providesApplicationContext(){
        return myApp;
    }
    @Provides @Singleton
    SessionManagement getSession(Context context){
        return  new SessionManagement(myApp);
    }
    @Provides @Singleton
    DatabaseHelper getDhelper(Context context){
        return new DatabaseHelper(myApp);
    }
}

Application class

    public class MyApp extends Application {

    protected AppComponent appComponent;
    private static MyApp instance;
    @Inject
    DashboardInteractorImpl dashboardInteractor;

    public static MyApp getInstance() {
        return instance;
    }

    public static Context getContext(){
       // return (MyApp) context.getApplicationContext();
          return instance.getApplicationContext();
    }

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
        appComponent = DaggerAppComponent
                .builder()
                .appModule(new AppModule(this))
                .build();
        appComponent.inject(this);
    }
    public AppComponent getAppComponent(){
        return appComponent;
    }
}

DasboardInteractorImpl

   public class DashboardInteractorImpl implements DashboardInteractor {

    private final DatabaseHelper dHelper;
    private final SessionManagement sManager;
    DashboardPresenterImpl mDashboardPresenter;

    public DashboardInteractorImpl(DashboardPresenterImpl mDashboardPresenter){
        this.mDashboardPresenter=mDashboardPresenter;

        dHelper = new DatabaseHelper(MyApp.getContext());
        sManager= new SessionManagement(MyApp.getContext());
    }
     //SOME LOGIC HERE..
}

Can i know what went wrong with my app?

charan reddy
  • 38
  • 1
  • 8

4 Answers4

2

When you want Dagger 2 to provide a class for you, make sure you annotate the constructor with an @Inject annotation:

@Inject
public DashboardInteractorImpl(DashboardPresenterImpl mDashboardPresenter){
    this.mDashboardPresenter=mDashboardPresenter;

    dHelper = new DatabaseHelper(MyApp.getContext());
    sManager= new SessionManagement(MyApp.getContext());
}

Also, get rid of the line for injecting in your app:

@Singleton @Component(modules = AppModule.class)
public interface AppComponent {
   void inject(MyApp myApp);

   //delete the line below:
   //void inject(DashboardInteractorImpl dashboardInteractorImpl);
}

You will also need to make sure that Dagger 2 can provide DashboardPresenterImpl either by annotating the constructor for the presenter with @Inject or by writing a @Provides method inside a module.

David Rawson
  • 20,912
  • 7
  • 88
  • 124
  • i added @Inject to both presenter and interactor, its now giving error – charan reddy Aug 09 '17 at 22:06
  • DashboardView cannot be provided without an @Provides-annotated method. – charan reddy Aug 09 '17 at 22:06
  • Dashboard view is just an interface – charan reddy Aug 09 '17 at 22:13
  • let me do one thing i will update my code on github, so that u can view my whole code there. – charan reddy Aug 09 '17 at 22:14
  • Are you There?? – charan reddy Aug 09 '17 at 22:31
  • @charan reddy i have a meeting now sorry - put the code on github and i will look later – David Rawson Aug 09 '17 at 22:37
  • @charan Please carefully read https://stackoverflow.com/questions/44912080/how-do-i-fix-dagger-2-error-cannot-be-provided?noredirect=1&lq=1 in the meantime, I think if you read carefully you will be able to solve the issue yourself – David Rawson Aug 09 '17 at 22:52
  • @charanreddy i checked your Github project. You haven't written any modules besides the AppModule! You need to write modules for your dashboard etc. See the Google sample project todo-mvp-dagger [here](https://github.com/googlesamples/android-architecture) – David Rawson Aug 10 '17 at 10:24
  • Yes, i tried to understand it, but im not getting how to instantiate my presenter and interactor class. Can you help me in doing that? I would lilke to know what should i do to remove the hard dependencies from my app.. please do bear me as im implementing dagger for the first time – charan reddy Aug 10 '17 at 10:31
0

Try using following modification

// Component
@Singleton @Component(modules = AppModule.class)
public interface AppComponent {
    void inject(MyApp myApp);

    DashboardInteractorImpl getDashboardInteractorImpl();
}

// Module
@Module 
public class AppModule { 
    private final MyApp myApp;
    public AppModule(MyApp myApp){this.myApp=myApp;}

    @Provides @Singleton 
    Context providesApplicationContext(){
        return myApp;
    } 
    @Provides @Singleton 
    SessionManagement getSession(Context context){
        return  new SessionManagement(context);
    } 
    @Provides @Singleton 
    DatabaseHelper getDhelper(Context context){
        return new DatabaseHelper(context);
    } 
}

// Application
public class MyApp extends Application {

    protected AppComponent appComponent;
    private static MyApp instance;
    @Inject 
    DashboardInteractorImpl dashboardInteractor;

    public static MyApp getInstance() { 
        return instance;
    } 

    public static Context getContext(){
       // return (MyApp) context.getApplicationContext(); 
          return instance.getApplicationContext();
    } 

    @Override 
    public void onCreate() { 
        instance = this;
        super.onCreate(); 
        appComponent = DaggerAppComponent
                .builder() 
                .appModule(new AppModule(this))
                .build(); 
        appComponent.inject(this);
    } 
    public AppComponent getAppComponent(){ 
        return appComponent;
    } 
} 

// DashboardInteractorImpl
public class DashboardInteractorImpl implements DashboardInteractor {

    private final DatabaseHelper dHelper;
    private final SessionManagement sManager;
    private DashboardPresenterImpl mDashboardPresenter;

    @Inject 
    public DashboardInteractorImpl(DatabaseHelper databaseHelper,
                SessionManagement sessionManagement
                DashboardPresenterImpl dashboardPresenter){

        dHelper = databaseHelper;
        sManager = sessionManagement;
        mDashboardPresenter = dashboardPresenter;
    }
     //SOME LOGIC HERE..
}

// DashboardPresenterImpl
public class DashboardPresenterImpl implements DashboardPresenter {

    @Inject     
    public DashboardPresenterImpl(ABC abc){
        //make sure ABC is provided by the module or inject on constructed
    }
}
Janishar Ali
  • 376
  • 3
  • 8
  • i will share my project on github.. can you please have a look on it. And correct me please??.. its very important for me to understand the dagger 2 and im failing to understand it completely. – charan reddy Aug 10 '17 at 09:42
  • @charanreddy Have you read all the 3 of My articles on Dagger 2: https://blog.mindorks.com/introduction-to-dagger-2-using-dependency-injection-in-android-part-2-b55857911bcd – Janishar Ali Aug 10 '17 at 16:55
0

You can inject DashboardInteractorImpl with two ways.

Either you have to Declare DashboardInteractorImpl Dependency in App module.

Or

You can write @Inject constructor. I did not see any code that full fill this Dependency. so you can do two thing you can Provide this DashboardPresenterImpl, DashboardInteractorImpl in module or Create @Inject constructor for each of this class DashboardPresenterImpl, DashboardInteractorImpl.

Himeshgiri gosvami
  • 2,559
  • 4
  • 16
  • 28
-1

If you use or write code in Kotlin add this line code inside build.gradle

apply plugin: 'kotlin-kapt'
Madi
  • 1,805
  • 1
  • 15
  • 9