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.
Component can be declared in a single module only. In order to use a component from another module, you need to do two simple tasks: Export the component in the other module. Import the other module, into the current module.
Sharing moduleslink Creating shared modules allows you to organize and streamline your code. You can put commonly used directives, pipes, and components into one module and then import just that module wherever you need it in other parts of your application.
Component is part of the declaration of 2 modules.
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.
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.
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