Sphere
Log in Join
☆ 9

Unresolved reference viewBinding after enabling it

Android, AGP 8.4, Kotlin 1.9.24. Added to build.gradle: android { buildFeatures { viewBinding = true } }. Sync OK, but the generated binding class is not found: e: Unresolved reference: ActivityMainBinding. Inflating with ActivityMainBinding.inflate(layoutInflater) in MainActivity.kt. Clean and rebuild did not help. Did I need to add something else? I have used viewBinding on other projects without issue so this feels like a config detail I missed.

⚑ Report

4 replies

☆ 0
D duct ·2026-10-25#1

Check that the layout file is actually named activity_main.xml, the binding class name is derived from the file name (snake_case to PascalCase plus Binding). If your layout is activity-main.xml or activitymain.xml the generated name differs. Also check the file is in res/layout, not res/layout-land only.

⚑ Report
☆ 0
N null59 ·2026-10-27#2

The layout was in res/layout and named activity_main.xml, turns out I had a build cache issue where generated sources were not refreshed. Deleted the app/build folder and did a rebuild and the class appeared. Annoying that clean did not clear generated binding sources.

⚑ Report
☆ 0
A async48 ·2026-10-29#3

Actually check for a typo in the import, it should be your.package.databinding.ActivityMainBinding. If Android Studio auto-imported from a different package or you have an import pointing at a stale generated class, you will see this. Invalidate Caches and Restart helps too.

⚑ Report
☆ 0
R regex ·2026-11-05#4

For what it is worth, generated binding classes only appear after a build that processes resources. If you are reading code before the first successful build, the IDE will show red even though it is fine. Build once, then judge.

⚑ Report
Log in to reply.