Creating and Integrating Android Library

vijay sn
4 min readAug 7, 2021

Introduction:

As a developer in general, We write a lot of code and we generally create a lot of things by ourselves over the years. In the process, we often forget the great things we have created already and we begin to again reinvent the same wheel again and again.

Wouldn’t it be tiring to do the same thing again and again 🤔🧐

Yes, it is… We do it unconsciously that's why we are not noticing at times.

Pic Credits: School vector created by pch.vector — www.freepik.com/vectors/school

Framework/Library is so useful in this case, where we can create things one time and consume it across the projects demanding the same behavioral functionality.

Getting Started

We are going to take a simple Regex validator which we mostly use for almost all the projects and we are going to see how it can be made as a library/framework.

However, instead of compiling the module into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module.

The main purpose of this blog is to encourage developers to make their own individual utility framework/library whenever new functionality is being developed. This saves our own time and also saves other’s time by sharing it with them.

Let's Begin 🤘🏼

Creating the Android Library

In Android Studio, click File -> New -> NewModule…

Select Android Library and hit Next

Configure the new module step in the wizard. At this point, you are required
to provide the module name, package name, language, and minimum SDK.

Setting up the Android Library

Make sure to configure the library using the properties — compileSdkVersion, buildToolsVersion, minSdkVersion, targetSdkVersion, versionCode, versionName based on your Application needs.

Include the Core Library Files

Include all the core library files needed for the library to perform the intended functionality.

We are going to add Regex Validator in this case, which helps us to do simple regex

Integrating the Android Library in your App

Open your app’s build.gradle file and add the following inside dependencies to integrate the regexvalidator library inside your app.

implementation project(':regexvalidator')

Build once to make sure everything is working fine and no error is thrown.

Consuming the Android Library Component

Let’s take a use case for an example, since we have included the Regex Validator library, we can make use of the functions in the library to do Regex Validations.

Email Validation

As stated in the above screenshot, we are going to validate the email, once the user enters the email and clicks on validate button, we will get help from the library and makes the regex validation.

Here’s what the implementation is

validateEmail method connects with RegexHandler in the library and gives back the validation result. This will show the success or failure toast to the user based on the validation.

As in the above screenshot, you can see how the email validation through the library is done and we are throwing the relevant toast to the user.

What’s Next?

I hope you felt the same way as I am, that the process is very easy. It's always good to spend an extra 5 to 10 mins on top of your work to make the library/framework for the new functionality being developed.

The habit of creating the framework always saves time in the long run and helps us not to reinvent the same wheel again and again.

You can publish your own android library in a public git repository and public Maven repository and consume it in your project directly. Stay tuned for the upcoming blog regarding this topic.

Please free to go through the similar story I have written for Creating and Integrating iOS Framework—https://vijaysn02.medium.com/creating-and-integrating-ios-framework-8b30d8a972e3

Check out this sample project that we have gone through this blog — https://github.com/vijaysn02/Android-RegexValidatorLibrary

--

--