Android Studio issue with data binding

Viewed 70

Sometimes, when an tag is used in the XML, the Binding that is created for the parent layout does not contain a reference to the Binding of the included layout. Instead, the included layout is seen as a View. This only affects Android Studio (and thus auto complete), but when you go to build the project, it builds properly with a reference to the Binding for the included layout. This only seems to affect some files in the project.

For example, if Layout A contains Layout X and Layout Y, it might have binding for Layout X while having the Layout Y being seen as a View. In such a case, every file in the project will fail to see the binding for Layout Y.

There doesn't seem to be a pattern for which files suffer from this. If I manually delete the cache ~/User/Library/Caches/Google/AndroidStudio4.1 folder, the issue tends to crop up for a different set of files.

Activity:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".feature.sidemenu.AboutActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/appbar_toolbar" />

        <include
            android:id="@+id/about_layout"
            layout="@layout/fragment_about" />

    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Toolbar:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/toolbarText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/elements_spacing"
        android:text="Toolbar"
        android:textSize="@dimen/text_size_medium" />
</layout>

About:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/aboutText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/elements_spacing"
        android:text="About"
        android:textSize="@dimen/text_size_medium" />
</layout>

and the kotlin Code:

        binding.aboutLayout.apply {
            aboutText.text = "" // I get error here (when I compile everything works)
        }
        binding.toolbar.apply {
            toolbarText.text = "" // But not here
        }

Kotlin Code

0 Answers
Related