Explain Codes LogoExplain Codes Logo

Kotlin-android: unresolved reference databinding

java
data-binding
gradle
kotlin-android
Alex KataevbyAlex Kataev·Feb 28, 2025
TLDR

DataBinding classes cannot be recognized due to an “unresolved reference databinding” error? Let's rectify it:

  1. Kickstart DataBinding within build.gradle (app):
android { ... buildFeatures { dataBinding true // Databinding - Engage! } }
  1. DataBinding magic needs the <layout> spell. Cast it around your XML views:
<layout ...> <data> <!-- Variables and data types come here --> </data> <TextView ... /> <!-- TextView goes "I'm involved too, right?" --> </layout>
  1. Finally, regenerate the DataBinding classes:
./gradlew clean build // Go, gradle, go!

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:
apply plugin: 'kotlin-kapt' // Summon the mighty kapt!
  • Make sure DataBinding gets along with Kotlin and Android plugin. Add correct versions in build.gradle (project):
buildscript { ext.kotlin_version = '1.6.10' // check dependencies { classpath 'com.android.tools.build:gradle:7.0.3' // "I'm ready to mingle" - Gradle version classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }
  • Add kapt dependencies and enable generateStubs, if required:
dependencies { ... kapt 'com.android.databinding:compiler:3.5.4' // "I'm not as old as I look" - Data Binding compiler } kapt { generateStubs = true // "Lots of stubs for everyone!" - kapt }

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:

// No <layout>? No party!`

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!

android { ... buildFeatures { dataBinding true viewBinding true } }

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!