Search code examples
androidmvvmdagger-2android-databindingdagger

Dagger and databinding


I have a MVVM project where I have ViewModel classes extending BaseObservable. Now if put @Inject class in my ViewModel then compilation fails with many errors like: "error: package xxx.databinding does not exist"

Can I find the actual error that's causing this using some gradle technique? Also is @Inject really supported with databinding?

Edit:

Code is exactly the same as https://github.com/googlesamples/android-architecture/tree/todo-mvvm-databinding/

In that I have added dagger and I'm trying to @Inject a repository into a view model that extends BaseObservable. As soon as I add @Inject into the view model then I cant compile


Solution

  • The general approach to fixing this kind of problem is to find the errors that are not tied to databinding. Once those are fixed, your databinding errors will go away. Databinding just complains loudly because the build failed before it could do its thing. Unfortunately this often feels like finding the needle in the haystack.

    If you have a lot of errors you may need to increase the maximum error count displayed, as otherwise the error output may end before it prints the actual root cause. See here: https://stackoverflow.com/a/35707088/436417

    Dagger's @Inject is compatible with databinding in general.