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).
there is layout_c.xml `
</variable>
<variable
name="handler"
type="xxxxxx">
</variable>
<RelativeLayout
......
</RelativeLayout>
`
there is layout_a.xml
<include
android:id="@+id/layout_c"
layout="@layout/layout_c"/>
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);
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 :
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With