3

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

halfer
  • 19,824
  • 17
  • 99
  • 186
  • please post the error and also the main.xml – Fahad Alotaibi Feb 25 '20 at 11:16
  • @FahadAlotaibi I added it, I hope it helps. – Mark Yukami Feb 25 '20 at 11:24
  • show java code for mainactivity – Wahdat Jan Feb 25 '20 at 11:26
  • @WahdatKashmiri I don't have the Binding code in my activity yet as the class fails to generate it. But this is how my oncreate looks like. protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(toolbar); initSessionMemberVariables(); initializeViewParts(); – Mark Yukami Feb 25 '20 at 11:30
  • Did you try to clean the project – Wahdat Jan Feb 25 '20 at 11:36
  • @WahdatKashmiri yep – Mark Yukami Feb 25 '20 at 11:43
  • It also happens for any other layout I add the parent to. – Mark Yukami Feb 25 '20 at 12:12
  • there might be some error in log where it is happening, can you show the screenshot where you are getting error – Wahdat Jan Feb 25 '20 at 12:20
  • @WahdatKashmiri There is not a single error in the log unfortunately. It just complains about the class not existing. The gradle and build go without issues. I am now wondering if ButterKnife might have some silent conflict going on since I am still using ButterKnife for pseudo viewBinding on other activities. – Mark Yukami Feb 25 '20 at 12:27
  • If it is a Kotlin project, https://stackoverflow.com/questions/44035504/how-to-use-data-binding-and-kotlin-in-android-studio-3-0-0] – iCantC Feb 25 '20 at 12:51

2 Answers2

1

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)

flauschtrud
  • 690
  • 7
  • 23
1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186