Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android databinding using in different modules

Now I have three modules,module A ,B,C.module A compile module B,module B compile module C.There is a layout(layout_c.xml) in module C.Then I using layout_c.xml in module A's layout(layout_a.xml).

  1. there is layout_c.xml `

    </variable>
    
    <variable
        name="handler"
        type="xxxxxx">
    
    </variable>
    

    <RelativeLayout
        ......
    </RelativeLayout>
    

    `

  2. there is layout_a.xml

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

  3. question:IDE think bindingA.layoutC return a view not a databinding.And module C has BR class and all databinding class.But module A doesn't have.So,what should I do?

    LayoutABinding bindingA = DataBindingUtil.setContentView(this,R.layout.layout_a); newTitleBarViewModel.setDataBinding(bindingA.layoutC);

like image 848
ZY Song Avatar asked Sep 29 '16 01:09

ZY Song


1 Answers

In order to get data binding to work across multiple modules - in my case - I had to make sure that each Android Studio module (library, phone/tablet, etc) has data binding enabled in its corresponding build.gradle (not just for the library .gradle file, as that was not enough),

like so:

android {
        ...

    defaultConfig {
        ...
    }
    buildTypes {
        ...
    }

    // Looks like this needs to be set in the app module that uses the lib
    // The lib needs it for the layout binding code there
    dataBinding {
        enabled = true
    }

}

The project structure where solution is applied :

  1. There is a library module, and multiple "app modules" (aka, Android Studio module for Phone or Tablet that can be run) that use Activities/layouts from that library in the same project

  2. The library project has .xml under /res/layout that relies on data binding, like so:

    <TextView
        android:id="@+id/display_name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@={user.displayName}"
        android:textSize="20sp"/>
    

This one was a bit tricky to resolve. Especially in my case where I have multiple modules. After I applied this solution for the main module I was working with, I still continued getting build errors. Finally I noticed in all the error output that there was 1 other module - which I hadn't been working with - that also had the library as dependency , but was missing the data binding enable in build.gradle. When finally that was identified and addressed, the builds are working fine. I'm glad to have found this as it makes things a lot nicer when you can reuse layouts with data binding across multiple modules

like image 166
Gene Bo Avatar answered Sep 23 '22 02:09

Gene Bo