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.
- Enable data binding in
build.gradle, inside theandroid{}block:
- Write the layout with data binding expressions:
- Use the generated binding class in Kotlin to access and update the layout's components:
Causes of the error
- Misconfigured
build.gradle: data binding is not correctly enabled in the modulebuild.gradle. - Missing variable definition: a binding expression references a variable that is not declared under
<data>. - Wrong binding class usage: the generated class name is wrong, or the class is used incorrectly in Kotlin.
- Project not synchronized: an unsynced project leaves generated code inconsistent.
- 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:
2. Declare every variable in the XML
Every variable used in the layout must be declared in the <data> block:
3. Use the right binding class
Use the correct auto-generated binding class in your activity or fragment:
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:
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
| Cause | Solution |
build.gradle misconfiguration | Enable: dataBinding true in build.gradle |
| Undefined XML variable | Declare all variables in <data> section |
| Incorrect binding class usage | Ensure correct class name and usage |
| Project synchronization issues | Sync and rebuild project |
| Kotlin and Gradle version errors | Verify and update to compatible versions |
Related reading
- Kotlin and new ActivityTestRule The Rule must be public
- Kotlin async await with limited parallelism
- Kotlin Compiler in Android Application Connection refused to host 127.0.0.1
- Kotlin Interface ... does not have constructors
- KRaft - unable to register with the controller quorum
- KSQL table not showing data but Stream with same structure returning data
- Kotlin Spring Boot ConfigurationProperties
- Kotlin Ternary Conditional Operator
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.