30

I am using Android Studio for my Android projects. I faced an issue when builds crash with strange stacktrace, like this:

Error:Execution failed for task ':app:compileDevDebugJavaWithJavac'.
 java.lang.RuntimeException: failure, see logs for details.
  cannot generate view binders java.lang.NullPointerException
    at android.databinding.tool.reflection.ModelMethod.isBoxingConversion(ModelMethod.java:155)
    at android.databinding.tool.store.SetterStore.isBetterParameter(SetterStore.java:946)
    at android.databinding.tool.store.SetterStore.getBestSetter(SetterStore.java:838)

and it was seemed that the databinding became broken as whole.

I made refactoring before and moved classes between packages.

yennsarah
  • 5,467
  • 2
  • 27
  • 48
atlascoder
  • 2,746
  • 3
  • 26
  • 34
  • I'm running on the same problem. In my case, I think it's because there is an inner class, but I{m not sure. I made several changes, but there is no clue at all of what's going on in the logs – Chisko Feb 14 '17 at 04:03

12 Answers12

49

In my case, I relied on Android Studio when renaming and moving classes between packages. But it didn't proceed correction for XMLs of layouts where were references on refactored classes in the type attribute of variable element in data.

So my previous type's value pointed to non existing files and build crashed.

It's simple mistake but it may take more time to find the source. Hope this would help someone.

atlascoder
  • 2,746
  • 3
  • 26
  • 34
  • 8
    This. I wasted nearly an hour because of this. I wish Android studio would show the actual error on databinding issues. – muthuraj Sep 11 '17 at 06:41
  • 1
    Was there any way you can modify the compiler errors to tell you what file did this? Or was it a lot of guessing? – AdamMc331 Nov 26 '18 at 21:08
  • 2
    @AdamMc331 unfortunately, I didn’t try to do it. It’s easy to open layouts files and see that lint marked wrong tokens – atlascoder Nov 26 '18 at 22:32
12

For me, this started happening after updating Android Studio to version 3.5.2

To fix it, I downgraded Android Gradle Plugin

buildscript {

    repositories {
        //..........
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        //......
    }
}
hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
9

Make sure you import all classes you reference in xml bindings

I had code something like:

android:visible="@{obj instanceof A}"

I was getting that same error.

Turns out that class A was not imported on top. Adding <import type="com.company.A"> tag resolved the problem.

P.S. android:visible is a custom binding adapter I have.

Daniels Šatcs
  • 526
  • 6
  • 18
9

If this question is still relevant. For me I figured that in my xml layout file I was including a class (not an instance of one) as a variable, i.e I had this code

<variable
      name="Converters"
      type="com.example.flexapp.utils.Converters" />

and it should have been

<import type="com.example.flexapp.utils.Converters"/>

since this was a class and not an object.

Steven
  • 93
  • 2
  • 5
6

in my case my problem was in a LongClickListener that the method for this listener must have returned a boolean but my method returned void so when I added the correct return statement it worked just fine.

keep in mind if you use a data binding method you must return the correct value or else the error it throws is not helpful at all.

Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36
  • 1
    Damn, it took me a full evening tryna get my moved classes and packages back to where they were before the error ...n then boom, I saw this n corrected my code n viola!! It worked – Lefty Jul 28 '19 at 12:00
2

You may have stumbled upon this question after migrating to Android X. If that's the case, and you're positive your XML files are okay, but it's still not working, you should start looking at libraries that generate code.

Libraries that generate code cannot be easily converted by the Jettifyer. Some info here - https://blog.danlew.net/2018/11/14/the-reality-of-migrating-to-androidx/

In my case, I updated the obvious ones, but I still was missing something. So, what I did was go into my develop branch (that wasn't Android X), ran a build, and dug into the build folder to see all of the libraries that generated code. Once I did that, I was able to look at the suspects one by one until I found the dependency that was causing this issue. You could either update it, or, in my case, remove it, and this error was resolved. :)

AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • 1
    this is the same for me, after finding the contents generated code/builds still caching the support library. By running `clean`, `invalidate+restart`, delete `.gradle` cache and build directory solved my issue, also by providing a `jetifier.blacklist` if we don't want to upgrade to AndroidX for that particular libs, [check here](https://github.com/bumptech/glide/issues/3080#issuecomment-495430590) – mochadwi Apr 07 '20 at 08:55
1

The getter function for any instance that you refer from your xml file might not have been defined or the getter function might not have public access specifier.

senthil kumar
  • 91
  • 1
  • 6
1

Downgrading Android Gradle Plugin to 3.5.1 fixed the issue for me

  • 1
    Hi, you could add a more specific answer, like how to downgrade or a link to why this happen on 3.5.2 Thank you your participation. – heat Mar 07 '20 at 13:13
0

I face this problem when I define variable like this (Android Studio don't warning anything)

<data>
    <variable
        name="onGlobalLayoutA"
        type="ViewTreeObserver.OnGlobalLayoutListener"/>

    <import type="android.view.ViewTreeObserver"/>
</data>

And I solve it by

<data>
    <variable
        name="onGlobalLayoutA"
        type="android.view.ViewTreeObserver.OnGlobalLayoutListener"/>
</data>

Hope this would help someone.

Linh
  • 57,942
  • 23
  • 262
  • 279
0

In my case, the reason was that I used a type for a variable which is another module and that module is added as 'implementation ' in build script. Changing to 'api ' resolved the issue.

Clearly not the same issue as the one posted by atlascoder but I mention regardless, maybe someone gits this article with the same problem as I had.

zsolt világos
  • 316
  • 3
  • 4
0

As @hiddeneyes02 mentioned, this started happen when upgrading from Android Gradle Plugin from 3.5.1 to 3.5.2, this seems to be the bug: https://issuetracker.google.com/issues/143778134

When I built the project with the command tool I got the same errors as in this post: When building Android project with Android Gradle Plugin v. 3.5.2 my builds fail

kotlinski
  • 331
  • 2
  • 6
0

In my case I wrote wrong the name of my view model variable inside the XML file. I had it named VProgressBar instead of vProgressBar:

android:visibility="@{fatherViewModel.vProgressBar}"
Ramiro G.M.
  • 357
  • 4
  • 7