Kotlin-android: unresolved reference databinding
DataBinding classes cannot be recognized due to an “unresolved reference databinding” error? Let's rectify it:
- Kickstart DataBinding within
build.gradle (app)
:
- DataBinding magic needs the
<layout>
spell. Cast it around your XML views:
- Finally, regenerate the DataBinding classes:
Voila! The DataBinding classes should now be recognized, and the error should have vanished. 🎉
Understanding your Gradle configurations
Let's dive a little deeper to enshrine DataBinding in your Kotlin-Android project:
Apply correct build.gradle
configuration
You're a mighty builder, but every builder needs the right tools:
- Add the magic wand:
kotlin-kapt
plugin:
- Make sure DataBinding gets along with Kotlin and Android plugin. Add correct versions in
build.gradle (project)
:
- Add kapt dependencies and enable
generateStubs
, if required:
And of course, make sure all these settings live in the android block of your build.gradle (app)
.
XML files layout check
DataBinding won't work if the <layout> tags are missing in your XML files:
Syntax verification
Any Gradle or XML syntax errors can interfere with DataBinding. A keen eye on typos and version mismatch can save your day!
Align your gears!
Ensure your Android Studio, Gradle, plugins are up-to-date, dancing the same version tango. Rusted gears (old versions) can grind your project to a halt.
Mastering DataBinding
Want to take DataBinding to the next level? Here's a deeper dive:
Configure dataBinding and viewBinding together
If your Android Studio version is equal or above Arctic Fox 2020.3.1, then you can enable DataBinding and ViewBinding together, giving you the best of both worlds!
Keep versions close and updated
Keep an eye on the Maven Repository for the latest Data Binding compiler versions. Version discordance between Gradle, Android plugin, and Kotlin can be the Grinch to your Christmas!
Learn by example
Check GitHub repositories for DataBinding examples. It's like a cooking show, but for code!
Was this article helpful?