Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android view binding between modules

Does view binding work between modules?

How to reproduce: Create 2 modules >>> create layout1 in module1 and create layout2 in module2 >>> Create Fragment1 in module1 with view which will be including layout1, layout2 >>> try to get binding >>> layout1 will work becouse it is in the same module as Fragment1, but layout2 will not work becouse of wrong binding generation

My example:

Authentication module has dependency to common module.

base_button.xml is in common module.

login_input_component.xml is in Authentication module so there is no problem.

But this error occures on loginButton binding.

It tries to find BaseButtonBinding in authentication module even if base_button.xml is in another module.

enter image description here

All modules has:

buildFeatures {
    dataBinding = true
    viewBinding = true
}
like image 630
Nikron Avatar asked Feb 07 '20 14:02

Nikron


1 Answers

If I am correct you have this kind of structure

project
| -- app
| -- module1 [ contains layout_one.xml and layout_fragment.xml]
| -- module2 [ contains layout_two.xml]

layout_fragment.xml will include both layout_one.xml and layout_two.xml

Now in order to make this work, all you need to do is go to module1 build.gradle file and add this dependency

implementation project(":module2")

Note your app will be aware of module1 and module2 files but module1 and module2 files will not be aware of both of there existence. So by specifying this dependency we make module1 to depend on module2

and also make sure in each module viewBinding is enabled

like image 53
AgentP Avatar answered Oct 19 '22 23:10

AgentP