Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing components between modules

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.

like image 842
usernotnull Avatar asked Jan 15 '16 09:01

usernotnull


People also ask

How do you use a component of one module into another module?

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.

What is sharing a 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.

Can a component be declared in two modules?

Component is part of the declaration of 2 modules.


2 Answers

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

like image 127
Aleksandr Avatar answered Oct 17 '22 05:10

Aleksandr


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.

like image 3
Bhawna Raheja Avatar answered Oct 17 '22 06:10

Bhawna Raheja