1 Comment

Three Methods for Solving Android Data Binding Errors

Trying to figure out what is causing data binding errors when you compile your Android project? This post will save you a lot of time and frustration.

There is nothing worse than the compiler telling you something’s wrong in your project but not telling you where. If you are getting cryptic errors like error: cannot find symbol import, the following methods may help you figure out what is wrong.

1. View All of the Build Output

Android Studio has two different ways to view the build output. The default tree view tries to condense the build output into an easy-to-view hierarchy.

tree view of build output

Although this method allows you to quickly get to your first build error, it doesn’t help if your first build error is a failed import because of problems elsewhere in your code. The underlying issue is obscured, and you should look at the entire build output. To see all of the build output, press the Toggle View button.

build output from android studio

Here, you can see the real reason for my compile error. I was missing a variable definition called viewModel in my layout file.

2. Fall Back to the Old Data Binding Compiler Temporarily

Starting in version 3.2.1 of Android Studio, the second version of the data binding compiler was turned on by default. Version 2 drastically improved compile times by incrementally creating the binding classes.

The incremental build may also be the source of the problem when you have cryptic error messages. I have found that rolling back to the first version of the data binding compiler can help reveal the real reason your code is not compiling. Once you figure out what your compile error is, switch back to Version 2 to restore the speed of your compile.

To take care of this, in the gradle.properties file, add the setting android.databinding.enableV2=false to revert to the old compiler. You could leave this setting there and just toggle the Boolean from false to true when you want to go back and forth. I chose to comment it out when I don’t need it, just in case a Version 3 compiler comes along someday. The default setting is true.

#Uncomment this line to revert to the old compiler
android.databinding.enableV2=false

3. Invalidate Caches

Sometimes, you may not even have an error in your code, but Android Studio incorrectly says that you do. Some people in online communities have found that if they tell Android Studio to Invalidate Caches and Restart, it can fix the erroneous error. This option is located in the File menu. I find this helps when the data binding compiler is not generating my data binding classes like it should be.

I hope these tips can help you conquer your Android data binding error challenges.