androidandroid-studiodata-binding

Android Data Binding - Cannot Access Generated Binding Class for Some Fragments


I'm using Data Binding in my Android project, and I've completed all the necessary setup. However, while I can access the generated binding classes (e.g., FragmentTemplateBinding) for some fragments, I cannot access the binding classes for others.

I’ve tried so far to resolve the issue:

Verified that Data Binding is correctly set up. Tried Invalidate Caches and Restart in Android Studio. Ensured that Data Binding is enabled in build.gradle. Cleaned and rebuilt the project (Clean Project and Rebuild Project).

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="viewModel"
            type="com.example.MyViewModel" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- UI elements -->
    </LinearLayout>
</layout>


android {
    ...
    viewBinding {
        enabled = true
    }
    dataBinding {
        enabled = true
    }
}


Solution

  • You should have viewBinding inside buildFeature which nested inside android.

    android {
        ...
        buildFeatures {
            viewBinding true
        }
    }
    
    

    For more detail you can visit this Official documentation link