How to use android libraries locally and leaving your development faster

Denis Vieira
Stant
Published in
4 min readOct 22, 2017

In android development we have a problem that makes us a little frustrated every day, the build time . The android studio build time has a tendency to increase with each day that our project is getting bigger, with that isolating everything we will have in the one project, in a single module, and this is an option that is not so interesting, both to avoid rewriting code in a future project, both to avoid creating a liability relationship of some common feature .

One option for us to try resolve this is to create libraries, which are nothing more than independent modules which has an easy integration and has its responsibility defined and isolated, avoiding side effects and being portable for numerous cases.

To use this portability we need to take a few steps:

  1. Creating new project with sample module
  2. Creating the library module
  3. Importing the module created in an external project
  4. Sharing with community .

I am writing listening a brazilian band called Bullet Bane, in my posts i like share music in view of my great musical passion and the responsibility to disseminate more music around the world. So Let’s go code ..

1. Create new project with sample module

Creating libraries is a task that can be done at any time and in the way that you think necessary, my suggestion is to create them in an external project and thus removing any possibility of interdependence, enabling it to develop outside the scope of only one project and too create one identity to a common feature. So let’s go with my suggestion, create the new android project ..

With the new project created and its initial structure simple, with MainActivity and everything else, we will determine that this App module will be our sample module, which will be used to test the integration of our library, if you want rename the “App” module to “Sample” .

2. Create the library module

To create is a simple task, in Android Studio each library is a module. To create a new library module in Android Studio, select File ▸ New ▸ New Module and select Android Library.

When we create the library it is not automatically imported into our sample module, so simply go to the build.gradle of our sample module and add in the dependencies :

compile project (“:library-name-created”)

With this we can develop our feature in this created library module, with all its responsibilities much more clear, specific and without any complexity of a great project, without mentioning that clearly the build time of our sample module destined to be our workspace of this library is much faster than our main project. A great suggestion is also to add integration tests in the sample module and unit tests in your library module to ensure that it develops with complete security and consistence of its main and future functionalities.

3. Importing the module created in an external project locally

This task can be done automatically by Android Studio, but my suggestion is that you do it manually so that you understand the process clearly and can fit in case you need it.

3.1 Include module folder into project

Automatically is simple only in Android Studio, select File ▸ New ▸ Import Module and write path of library project and select only library module .

Manually it’s more cool, despite having to write some more but i think it more interesting to eliminate any magic that IDE makes and to facilitate understanding the process. Steps below ..

Add into settings.gradle and sync gradle .

':libraryname'
project(':libraryname').projectDir = new File(settingsDir, '../your-library-folder/libraryname')
When your sync gradle after add code, will appear new module in project tree .

3.2 Add library locally into project

Simply do same earlier step to add the module, add compile into build.gradle dependencies of main module .

compile project (“:libraryname')

PS : This process can cause some conflicts if the projects use other libraries in different versions, it is important to always keep the same of both libraries updated.

4. Sharing with community

There are many ways to distribute your library, but if your new library module has no business rule (as it should be), it is important to share it with the community. So that other people can use it to find bugs, fix them, develop new features for it. Thus creating an ecosystem of interaction with your library that will give much more powers to it .

I suggest https://jitpack.io/ to this, jitpack is very easy to use, follow the tutorial below and you will get it easily.

https://medium.com/@ome450901/publish-an-android-library-by-jitpack-a0342684cbd0

In STANT so far we are using 2 android libraries and we have other projects on other platforms, open-source and using this principle and has facilitated enough thing, internally the trend is to adopt even more is this idea.

Thank you , and Let’s code !!

Referencies

--

--

Denis Vieira
Stant
Writer for

Software Engineer | Android Engineer Specialist