Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android databinding with same layout in different modules

Supposedly I have two android modules

ModuleA
- HelloWorld.Xml
ModuleB
- HelloWorld.Xml

Notice I have same xml layouts in different modules.

Now there will be two item bindings generated that would also be properly kept in their corresponding module

ModuleA
- HelloWorld.Xml
- HelloWorldItemBinding.java
ModuleB
- HelloWorld.Xml
- HelloWorldItemBinding.java

This compiles however when we land on ModuleB's HelloWorld.xml we get an error with the following

Cannot cast ModuleA.HelloWorldItemBinding to ModuleB.HelloWorldItemBinding.

I understand that this is because the project's modules are actually merged at compile time , however I still dont understand that when two binding files are actually created already at build time , why does android have an issue with finding the right one ?.

Also I couldn't find any link on this subject about databindings in multiple modules caveats at the official documentation

like image 880
Muhammad Ahmed AbuTalib Avatar asked Mar 18 '26 17:03

Muhammad Ahmed AbuTalib


1 Answers

By default, a binding class is generated based on the name of the layout file but you can set custom binding class name or full package name to avoid conflict.

ModuleA

<data class="HelloWorldModuleA">
…
</data>

ModuleB

<data class="HelloWorldModuleB">
…
</data>

here is official info

like image 150
Kishore Jethava Avatar answered Mar 21 '26 07:03

Kishore Jethava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!