Building an Android Library Tutorial

See how to create an Android library using Android Studio, publish the library to a Maven repository on Bintray, and host the library in the public JCenter repository. By Nishant Srivastava.

2.7 (7) · 1 Review

Download materials
Save for later
Share

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling 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.

In this tutorial, you’ll get to learn everything about building an Android library, from creation to publishing it for others to consume.

In the process, you’ll learn:

  • How to create an Android library
  • How to publish your Android library
  • How to use your Android library
  • Best practices around building Android libraries

Note: If you’re new to Android Development, it’s highly recommended that you work through Beginning Android Development and Kotlin for Android to get a grip on the basic tools and concepts. Other prerequisites include knowledge of using the bash/Terminal, git and Gradle. You’ll also need Android Studio 3.0 or later, and to publish your library you’ll need to have a GitHub account.

Introduction

Code reuse has been around since the advent of code, and Android is no exception. The end goal of every library developer is to simplify abstract complexities of code and package the code for others to reuse in their projects.

As Android developer, you often come across situations where some code is a good candidate to get reused in the future. It is in these situations when packaging that code as an Android Library/SDK makes the most sense.

Every Android developer is different in their own ways. At the same time, there are no set standards around building Android libraries. What that means is that developers come up with their own version of the solution and usually that leads to inconsistencies. Best practices, if defined and followed, could make things more streamlined. In case of library/SDK development, the goal should be to design better APIs so as to enable intended usage. Along with that, you also need to make sure that the API users are clear about its intended use and limitations.

The above holds true for every library and not only for Android library development. If you spent some time solving a problem and believe that others might be facing the same problem, then abstract it into an Android Library. At the very least, it is going to save you precious time in the future when you revisit the same problem. And hopefully a bigger group of Android developers will benefit from your Android library. So it is a win-win in either case.

It’s important to note, however, that the reason to create an Android library should not just be because you think so. If a solution already exists then use that, and if it does not solve your issue then you could make a request for the feature in the existing Android library. Brownie points to you if you decide to solve the problem and contribute back to the existing Android library. The advantage of that is huge because you helped to make the ecosystem better and in the process helped a lot of other Android developers. Better still you did not have to spend a lot of time managing an Android library but your code is going to get used by others.

Phew! Looks like you are all set to embark on the journey to becoming a better Android Library developer! Let’s dive right into it! :]

Getting Started

Begin by downloading the materials for this tutorial at the top or bottom of the page.

Inside, you will find the XML Layouts and associated Activities containing some boilerplate code for the app, along with helper scripts for publishing the Android library to Bintray, and some resources such as Drawables and Strings that you’ll use later on in this tutorial.

If you already have Android Studio open, click File\Import Project and select the top-level project folder you just downloaded. If not, start up Android Studio and select Open an existing Android Studio project from the welcome screen, again choosing the top-level project folder for the starter project you just downloaded. Be sure to accept any prompts to update to the latest Gradle plugin or to download the correct build tools.

Take some time to familiarize yourself with the project before you carry on. MainActivity contains three EditText which you can use to enter email, password and credit card number. When you tap the Validate button, you’ll pass the text entered in the EditText fields and validate them via already declared methods.

Another thing to note is the usage of the ext variable from the project’s build.gradle file in the app/build.gradle file. You will be defining more ext variables in this tutorial and referencing them in the same way later on.

Build and run the app. You should see the following:

Starter app

You will see it does not do anything right now. That is where you come in. Next up you are going to create the Android library which will validate the entered text in the fields. Let’s get rolling!

Creating the Android Library

Inside your Android Studio, click File\New\NewModule…

New module

Select Android Library and hit Next.

New library

You will be at the Configure the new module step of the wizard. At this point, you are required to provide a name for your library , module name, package name and minimum SDK. Put validatetor as library name and module name, com.raywenderlich.android.validatetor as package name and set the minimum SDK to be at 14.

Configure the new module

Once done, hit Finish and go get yourself a coffee. Just kidding, wait or be back in 5 minutes tops (Gradle doing what it does best, compiling and building stuff…) for the next steps!

Looks like you are back and you also have your Android library module named validatetor setup in your project.

Go through the new module added to your project and get familiar with its files. An important thing to note is that under the validatetor module the folder src/com.raywenderlich.android.validatetor/ is empty!

Hmm, that seems odd. Usually, the app module has at least the MainActivity.kt or MainActivity.java inside the same path under it. Well, let me clear this up for you! The reason it is empty is because it is a library and not an app. You need to write the code that the app module will later on consume.

You need to set your Android library up for future steps. To do that, Add ext variables to the project’s(root) build.gradle file inside the already defined ext block, below the Project variables

ext {
  // Project
  ..

  // ValidateTor Library Info
  libVersionCode = 1
  libVersionName = '1.0.0'
}

Next, update your validatetor/build.gradle file to use the ext variables from project’s build.gradle file.

  1. Update compileSdkVersion and buildToolsVersion:
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    
  2. Update minSdkVersion, targetSdkVersion, versionCode and versionName:
    minSdkVersion rootProject.ext.minSdkVersion
    targetSdkVersion rootProject.ext.targetSdkVersion
    versionCode rootProject.ext.libVersionCode
    versionName rootProject.ext.libVersionName
    
  3. Update the version field for the support library dependency:
    testImplementation "com.android.support:appcompat-v7:$supportLibVersion"
    
  4. Update version field for the junit dependency:
    testImplementation "junit:junit:$rootProject.ext.junitVersion"
    
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.libVersionCode
versionName rootProject.ext.libVersionName
testImplementation "com.android.support:appcompat-v7:$supportLibVersion"
testImplementation "junit:junit:$rootProject.ext.junitVersion"

All the above makes sure is that your code is consistent when it comes to defining versions. It also enables control over all these from the project’s build.gradle file.

Next, you have to write the logic of validating the strings that will be included in the validatetor library. The validation code is not the focus of this tutorial, so you’ll just use the Java files from the download materials in the validatetor-logic-code folder.

Once downloaded, extract the files and copy all the files:

Copy files

Paste the copied files inside your validatetor module under src/com.raywenderlich.android.validatetor/ folder.

Paste files

This is what your project will look like now:

validatetor project

Nishant Srivastava

Contributors

Nishant Srivastava

Author

Joe Howard

Final Pass Editor

Over 300 content creators. Join our team.