Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing to generate DataBindingClass (Impl does generate)

I have recently switched from using ButterKnife to using android DataBinding, but one of the classes fails to generate.

ActivityMainBindingImpl < Generates fine.

But ActivityMainBinding is missing and it also shows that it can't be found in the Impl class itself.

I have tried everything you can find on the internet through multiple search engines so far. (invalidate caches rebuilding updating etc.) Maybe this is a bit more niche and I hope some of you guys have encountered it before?

I do have

dataBinding {
    enabled = true
}

Important to note I am using androidx instead of the old support libraries.

It might be silently failing somewhere but I have tried it with and without any <data> elements in my layout file.

I also wrap my layout correctly with <layout> like so:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

It seems rather odd it can generate the Impl class fine, but the other doesn't exist.

I have gone into the folder where it is generated as well and only the Impl class is visible.

Full XML layout

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

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presentation.activities.MainActivity"
    tools:layout_editor_absoluteY="25dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/toolbar_container_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>


    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_container_layout">
        <!-- This is a comment  #001C40 -->

        <LinearLayout
            android:id="@+id/scroll_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/dashboard"
                android:layout_width="match_parent"
                android:layout_height="700dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbar_container_layout">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/dashboard_header"
                    android:layout_width="0dp"
                    android:layout_height="100dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/logo_container"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginEnd="8dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent">

                        <ImageView
                            android:id="@+id/facta_logo"
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            android:adjustViewBounds="true"
                            android:scaleType="fitCenter"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintHorizontal_bias="0.0"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintVertical_bias="0.0"
                            app:srcCompat="@drawable/facta_logo" />
                    </androidx.constraintlayout.widget.ConstraintLayout>

                </androidx.constraintlayout.widget.ConstraintLayout>

                <androidx.gridlayout.widget.GridLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:horizontalSpacing="16dp"
                    android:verticalSpacing="16dp"
                    app:columnCount="2"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/dashboard_header"
                    app:rowCount="4">


                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />

                    <include layout="@layout/dashboard_card" />


                </androidx.gridlayout.widget.GridLayout>
            </androidx.constraintlayout.widget.ConstraintLayout>

        </LinearLayout>
    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

The error is

   Cannot resolve symbol 'ActivityMainBinding'

Where my XML layout is activity_main.xml

like image 509
Mark Yukami Avatar asked Sep 15 '26 19:09

Mark Yukami


2 Answers

Did you try and add a data tag like so?

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="viewmodel"
            type="com.myapp.data.ViewModel" />
    </data>
    <ConstraintLayout... /> <!-- UI layout's root element -->
</layout>

At least my binding classes get messed up if I omit the data tag, although restarting Android Studio sometimes also has weird effects.

(sorry, I would have rather liked to add this question as a comment, but I do not have enough reputation for that)

like image 136
flauschtrud Avatar answered Sep 18 '26 09:09

flauschtrud


I am baffled.

The problem was that I had my layout files structured in subfolders. This is a trick you can do by changing the sourceSets through Gradle, allowing you to have an organized layouts folders (separated fragments from activities etc). Since I had over 100 layout files this made sense to me.

Unfortunately it's not very standard and it was apparently too confusing for both dataBinding and viewBinding to work.

Don't do this:

 sourceSets {
    main {
        //Tried to make it a bit more organized 
        res.srcDirs =
                [
                        'src/main/res/layouts/activities',
                        'src/main/res/layouts/adapters',
                        'src/main/res/layouts/components',
                        'src/main/res/layouts/forms',
                        'src/main/res/layouts/fragments',
                        'src/main/res/layouts/lists',
                        'src/main/res/layouts',
                        'src/main/res'
                ]
    }
}

It's a very niche issue.

like image 31
Mark Yukami Avatar answered Sep 18 '26 09:09

Mark Yukami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!