Whenever I add a android library-project as module to my Android Studio project, the sources get copied to the Android Studio project folder.
Is there a way to solve it like in eclipse, where there is only one copy of library project, any many projects can reference it?
I would compile your shared code as library project. So you will get an aar file which you can reference.
To create that Android Archive you need to build the project as a release build with this command:
After that you have a file called
<modulename>-release.aar
this file is located in<projectroot>/<modulename>/build/outputs/aar
. I rename those files to<modulename>.aar
then you can put it into your lib directory of your module.When done you can reference it from the module where you need it like this:
This also speeds up the build time since you don't need to compile the project anymore.
You have different ways to achieve it:
CASE 1:
Using gradle and a local library, inside a project you can refer an external module.
Just use:
In
settings.gradle
:In
app/build.gradle
:Pay attention to myLib.
You have to use the path of the library inside the other project, not the root of the project.
CASE 2:
Compile the library module, get the aar file, and then add to the main project:
Add the folder where you put the aar file as repository:
Then add the dependency:
CASE 3: Another way is to to publish your module into a maven repository.
In this way, just use: