Create an Android Library and use in your Android Project

This blog is providing information that why modular code structure is important and how the android library can play an important role in clean project architecture.

I always have one thought while developing and designing the project's architectures. The bad project architecture can lead to a lot more difficulties in the implementation of the software.

I am mentioning a few major drawbacks of a bad project architecture design.

  1. Difficult to understand the software code.
  2. Complex to adopt the new features.
  3. Complex the bug traceability.
  4. Not easy to maintain the software code and etc.

A working code does not make sure it is correctly written.

In this article, I am explaining how to create an Android Library so before going ahead into the practical details just look at the below pieces of information and concepts that make us learned why we should create a library module.

What is an Android Library?

An Android library is a piece of the code segment or a module that is independently designed for specific functionality. and this functionality can be easily used by multiple other modules.

What are the possible advantages of a Library in Android?

  1. The redevelopment again for the same code for a specific functionality can be avoided. If we have common functionality in different modules of the same project. Or a functionality which can be reused by multiple different projects then instead of re-writing the same code everywhere, it is good to build a Library and use it where ever required.
  2. If the Android Projects is built for different flavors and a core component is common in both then why write it twice.
  3. The clean Architecture: In clean architecture, the projects are always divided into different modules. I can’t summarise the clean architecture principles in short so here is the blog is written by Robert C. Martin (Uncle Bob) please go through with the details.

All the advantages of a library are basically related to reusability, clean code, easily maintainable design, easy to integrate, and the Prefered way of design and code the software.

Now I would like to go ahead into practical details that how we can design a Library and use it in our Android application.

Note: I am using Android Studio 4.0.2 so issues related to the older and specific versions are not considered.

Problem statement:

We will design a simple android app that has a current time text label and a get time button. On the Click of the get Time button, the current time will be fetched and the time text label will keep showing the current time. The UI will have only a function call to the library and All the business logic will be written in an Android library.

Follow the steps>>

Step1: Start the Android Studio and create a new project with an Empty Main activity.

Note: I considered that you are aware of android app development so I am not explaining the basics steps related to creating a project and write the code for UI.

Step2: Add a button and time text view, For me, the UI looks like below.

Step3: Add the code to perform the click on the button and further code details will be explained later in the article.

Step4: Now build the Library >> Follow the below steps.

4.1 Click File > New > New Module

4.2 In the Create New Module window that appears, click Android Library, then click Next.

Finally, click the Finish button and the library module will be added to the project structure as marked in the below image.

Note: app module is a UI module and currentTimeLib is the library module.

Step5: Add your library as a dependency

In this example, we have created the library module within the project so the library module will be already available and included in the settings.gradle. if It is not available then include the library module in the settings.gradle file.

Verify as in the image it is shown below

5.1 Add the library module in the app module Gradle file.

Sync the project and run, It should run with Gradle successful so we can make sure all the steps are correctly done.

Step6: Now we will write the business logic to fetch the current time in the library module.

6.1 Create a java class named CurrentTime.

6.1: Create an interface to have a callback in MainActivity for receiving the current time.

Now we have the following classes and the code for those classes looks like showing below. For final output please add the code into the respective files.

Note: Please read the comments associated with functions to understand the code.

CurrentTime.java

CurrentTimeImpl.java

MainActivity.java

activity_main.xml

This example is covering how to create the android library in an Android Project and use it with the same project. For using the Library with another project we can generate the AAR file and can use that with any other Android Project also. I will cover this in the next article.

The full code example is available on Git, click here.

Thanks!! Please follow me for an upcoming article on this topic.

Software Developer with Stack: Android, JAVA, Python, DevOps | Writer | Technical Trainer | Technologies Enthusiast, Motivational Speaker, Learner Forever