Reputation: 3628
In my project, I have the main app module, which has dependencies on submodules I added for my project, let's say one is for a custom alert dialog, another for custom views, etc...
How can I get a reference of a value in a submodule from the main app module? As an example, the custom alert dialog which has a layout xml needs to take a color value which is found in the main app module.
I tried to add the main app module as a dependency in the submodule but that definitely won't work since there will be a circular dependency.
Upvotes: 13
Views: 11904
Reputation: 4936
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.
https://github.com/AlexanderGarmash/AndroidModulesShowcase
<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')
ResourceModule
everywhere.Don't forget to import correct R
class.
Upvotes: 32
Reputation: 619
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.
Upvotes: 3