Kotlin
Android
Data Binding
Unresolved Reference
Programming Error

Kotlin Android: fixing unresolved reference for data binding classes

Codemia Knowledge Hub ·

An "unresolved reference" for data binding usually means the binding classes were never generated or are referenced wrongly: data binding is not enabled in the module's build.gradle, a layout variable is missing from <data>, the generated class name is wrong, or Gradle needs a sync and rebuild. Kotlin and Android Gradle plugin version mismatches can also cause it.

How data binding generates classes

Data binding lets you bind UI components in layouts to data sources declaratively, which cuts boilerplate. The build generates a binding class for each XML layout file, and your code uses that class to update the UI.

  1. Enable data binding in build.gradle, inside the android&#123;&#125; block:
groovy
1   android {
2       ...
3       buildFeatures {
4           dataBinding true
5       }
6   }
  1. Write the layout with data binding expressions:
xml
1   <layout xmlns:android="http://schemas.android.com/apk/res/android">
2       <data>
3           <variable
4               name="user"
5               type="com.example.User" />
6       </data>
7       <LinearLayout
8           android:orientation="vertical"
9           android:layout_width="match_parent"
10           android:layout_height="match_parent">
11           <TextView
12               android:id="@+id/nameTextView"
13               android:layout_width="wrap_content"
14               android:layout_height="wrap_content"
15               android:text="@{user.name}" />
16       </LinearLayout>
17   </layout>
  1. Use the generated binding class in Kotlin to access and update the layout's components:
kotlin
   val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
   binding.user = User("John Doe")

Causes of the error

  1. Misconfigured build.gradle: data binding is not correctly enabled in the module build.gradle.
  2. Missing variable definition: a binding expression references a variable that is not declared under <data>.
  3. Wrong binding class usage: the generated class name is wrong, or the class is used incorrectly in Kotlin.
  4. Project not synchronized: an unsynced project leaves generated code inconsistent.
  5. Kotlin compatibility: the Kotlin version and the Android Gradle plugin version do not work together.

Fixes

1. Check the build.gradle configuration

Enable data binding in the module-level build.gradle:

groovy
1android {
2    ...
3    buildFeatures {
4        dataBinding true
5    }
6}

2. Declare every variable in the XML

Every variable used in the layout must be declared in the <data> block:

xml
1<data>
2    <variable
3        name="user"
4        type="com.example.User" />
5</data>

3. Use the right binding class

Use the correct auto-generated binding class in your activity or fragment:

kotlin
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

4. Sync and rebuild

  • Click "File" -> "Sync Project with Gradle Files".
  • Rebuild with "Build" -> "Rebuild Project".

5. Check Kotlin version compatibility

Make sure the Kotlin version is compatible with the Android Gradle plugin, and set it in the top-level build.gradle:

groovy
1buildscript {
2    dependencies {
3        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:<latest-version>"
4    }
5}

If the error persists

  • Clean and rebuild: cleaning the project and rebuilding sometimes clears the error.
  • Invalidate caches and restart: this clears cached data that can cause incorrect builds.

Causes and fixes at a glance

CauseSolution
build.gradle misconfigurationEnable: dataBinding true in build.gradle
Undefined XML variableDeclare all variables in <data> section
Incorrect binding class usageEnsure correct class name and usage
Project synchronization issuesSync and rebuild project
Kotlin and Gradle version errorsVerify and update to compatible versions

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms