Search code examples
androiddagger-2android-databinding

Error with DataDinding and Dagger2


I was developing a project using DataBinding but after to add Dagger2 and implement the Module, Component and graft I have faced this error:

Error:(8, 74) error: package com.anda.soft.app.databinding does not
exist
Error:(16, 13) error: cannot find symbol class ActivityMainBinding
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.IllegalArgumentException: not a valid component method: injectPresentationFragmentPresenter()

This is my Module:

@Module
public class Modul {

    private Context context;


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

    @Provides
    public MainActivity provideMainActivity(){
        return new MainActivity();
    }

    @Provides
    public PresentationFragmentPresenter providePresentationFragment(){
        return new PresentationFragmentPresenterImp(provideMainActivity());
    }

}

My Compoment:

@Component (modules = Modul.class )

public interface Compoment {
 void injectPresentationFragmentPresenter();
}

and Graph

public class App extends Application {

    private Compoment mCompoment;

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

        setUpGraph();
    }

    private void setUpGraph() {
        mCompoment = DaggerCompoment.builder()
                .modul(new Modul(this))
                .build();
    }


    public Compoment getCompoment(){
        return mCompoment;
    }
}

Finally my MainActivity

    private ActivityMainBinding mActivityMainBinding ;
    @Inject PresentationFragmentPresenter mView;
    private Toolbar mToolbar;


    @Override
    protected void bindView(int layoutResource) {
        mView = new PresentationFragmentPresenterImp(this);
        mActivityMainBinding = DataBindingUtil.setContentView(this,layoutResource);
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_main;
    }

What I am doing wrong ? Do you know if there is incompatibilities between Dagger and DataBinding?


Solution

  • A component has two ways of providing dependendencies:

    Provision methods that return an injected or provided type.

    Here's an example of a provision method. Note that it is a method that simply returns a dependency, in this case an OKHttpClient.

    OkHttpClient httpClient();
    

    Members-injection methods that inject dependencies into a particular type.

    Here's an example of a members-injection method. Note that it takes a single argument, which is the type that will be injected with its dependencies (in this case a MainActivity:

    void inject(MainActivity activity);
    

    You can read more about components at the Dagger 2 @Component javadocs.

    Your problem is that void injectPresentationFragmentPresenter(); is neither of these. It doesn't return anything, so it isn't a provision method. It also takes no argument, so it can't be a members-injection method. Based on the naming of the method and the rest of your post, I'm guessing what you want is to define the method like so:

    void injectPresentationFragmentPresenter(PresentationFragmentPresenter presenter);