2

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

Prasanth
  • 577
  • 1
  • 9
  • 24

3 Answers3

1

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.

Uli
  • 2,828
  • 20
  • 23
  • I already know what you were talking about I just wanted a way to see what the error was. Thanks for the link. Too bad google does not provide these as default for the gradle template. Turns out it was the simplest of the errors "Dagger does not support injection into private fields". Thanks a lot for the help. – Prasanth Jul 20 '17 at 06:18
0

Dagger works with data binding, you have something wrong in your setup.

When you get error: package xxx.databinding does not exist it means that code generation failed, and since both data binding and dagger use code generation problem might be in the setup of both components. Based on your description it looks like you have not configured dagger properly, i.e. not set up how it should provide the object you are injecting.

Make sure that you did the actions under "satisfying dependencies" and "building the graph" from here https://google.github.io/dagger//users-guide.html

TpoM6oH
  • 8,385
  • 3
  • 40
  • 72
0

Like Uli mentioned, this is due to the number of displayed errors being limited by the compiler.

Do this:

1. Increase the displayed error limit by doing the following

Add this snippet in your submodule gradle file inside the android block.

kapt {
    javacOptions {
        // Increase the max count of errors from annotation processors.
        // Default is 100.
        option("-Xmaxerrs", 1000)
    }
}

2. Find the errors which are not binding related and fix them.

i.e (Fix the errors from app/src/.. folders and ignore the ones from app/build/generated/.. which are binding related)

Check this thread and this comment for more info.

Venkata Narayana
  • 1,657
  • 12
  • 25