Android data binding include layout from different module

Viewed 1615

I have multiple modules in my project, let's say module app and module A that act as a library for module app. I use data binding and works fine by adding

dataBinding { enabled = true }

in each module build.gradle.

The problem occurred when I including layout from module A using tag. When I'm trying to access the databinding of included layout, it return View object instead of ViewDataBinding

Android studio autocomplete

However when I'm trying to continue to access the id inside the included layout, the compilation works fine even the IDE showing an error. I've tried rebuild project, invalidate cache and everything else.

Error on android studio

And I'm sure already follow the rules how to implement DataBinding. This is the layout on the module app :

<layout xmlns:android="http://schemas.android.com/apk/res/android>
    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@color/light_gray"
       android:orientation="vertical">

        <include layout="@layout/included_layout" id="@+id/contact"/>
    </LinearLayout>
</layout>

And the layout on module A :

<layout xmlns:android="http://schemas.android.com/apk/res/android>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</layout>
1 Answers

Could very well be a build order issue. You may need to make sure that module A is built before parent module tries to access it's content.

What you are seeing in your binding is the ID you assigned to contact. This is good as it found the layout you are trying to access. Next it will look for the binding file of contact. Should be something like IncludedLayoutBinding based on your naming convention. That file will have the nested textViews inside of it as tv_text.

Now if you don't see tv_text it is because the IncludedLayoutBinding was never created for the child module.

Are you sure your child module has enabled databinding properly and are you sure it has built before you are attempting to access it's values.

You can check the childModule/build/source/dataBinding folder to ensure that you see your layoutBinding class created. I'm guessing it is not there currently.

Related