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
}
Setup Important to note I am using androidx instead of the old support libraries.
I have been stuck on this for 5 hours and don't see how something seemingly so simple can go so wrong.
It might be silently failing somewhere but I have tried it with and without any elements in my layout file.
I also wrap my layout correctly with 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
I am baffled xd.
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 but hopefully this can help out someone else.