Sharing components between modules

Explanation:

Try to organise submodules with next rules:

1). Main app module with Activity, Fragment + xmlayouts

No modules must depend on this module. This module will depend on resource module, submodule 1, submodule 2.

Don't store any shared values in this module like colors, dimens etc.

2). Resource module with colors, attrs, dimens etc.

Just create android library module and store here only shared resources. This module must have no dependencies. And each module which needs resources will depend on this module.

3). Submodule 1 with custom alerts.

This module will depend on resource module.

4). Submodule 2 with custom views.

This module will depend on resource module.

Diagram:

enter image description here

Code:

https://github.com/AlexanderGarmash/AndroidModulesShowcase

Description:

<module_root>/app/build.gradle dependencies section:

compile project (':ResourceModule')
compile project (':Submodule1')
compile project (':Submodule2')

<module_root>/ResourceModule/build.gradle dependencies section:

nothing

<module_root>/Submodule1/build.gradle dependencies section:

compile project (':ResourceModule')

<module_root>/Submodule2/build.gradle dependencies section:

compile project (':ResourceModule')

Advantages:

  • Low coupling
  • Transparent clear structure

Disadvantages:

  • If you have a lot of modules you need to not forget link to ResourceModule everywhere.

UPDATE

Don't forget to import correct R class. enter image description here


You should add the resources values in library module instead of app module if you want to use them in library. Because when adding a library, app can use library's resources but vice versa can not be.