How to Build & Publish an Android Library

Blair Fernandes
3 min readJul 23, 2023
Photo by Nick Fewings on Unsplash

Creating and publishing an Android library allows developers to share their code and contribute to the Android community. Whether you want to provide reusable components, utility functions, or custom views, building and publishing an Android library can be a rewarding experience. This post will take you through the process of building an Android library and publishing it for others to use.

Understanding the Basics of Android Libraries

Android libraries are collections of code and resources that developers can use to enhance their Android projects. They encapsulate functionality and can be easily integrated into various applications. Libraries come in two main types:

  1. JAR (Java ARchive) libraries: These contain compiled Java code and resources and are compatible with Java and Android projects.
  2. AAR (Android ARchive) libraries: These are similar to JAR files but also include Android-specific resources and XML files.

Setting Up the Project Structure

  1. To get started open Android Studio and create a new project or open an existing project that has the code which you want to share as an Android library.
  2. After the project is opened click on File > New > New Module…
  3. That should open a new dialog for creating a new module. In this dialog select Android Library from the list of templates. Fill in the module details like name, package name, language, the minimum SDK level that should be supported by your module and click finish. This will create a new Android library module alongside your default app module.

Implementing the Library Code

Begin building the core functionality of your Android library in the new module. Write the necessary classes, interfaces, and utility functions that encapsulate the intended features.

If you have opened an existing project which contains the code that you want to share as a library then you can start by extracting the existing code and resources to the new library module.

The new library module has its own build.gradle file where you can put any dependencies required by your library code.

In the same build.gradle file add the following configuration:

//inside plugins
id 'maven-publish'

//at the end of the file
afterEvaluate{
publishing {
publications {
release(MavenPublication){
from components.release

groupId = 'com.github.YOUR_GITHUB_USERNAME'
artifactId = 'YOUR_MODULE_Name'
version = '1.0'
}
}
}
}

Now you can use your Android library locally in other modules for eg: app module of your project by adding a dependency in the following way to your app module’s build.gradle file.

implementation project(':YOUR_MODULE_NAME')

Publishing the Library

To make your Android library accessible to other developers, you can publish it to a public repository like Maven Central or JCenter. This allows other developers to easily include your library in their projects using Gradle. We will use JitPack which allows you to publish your library directly from your Git repository.

First create a jitpack.yml file in the root of your project and add the following to it:

jdk:
- openjdk17
before_install:
- ./scripts/prepareJitpackEnvironment.sh

Then you need to push your code to a public GitHub repository. Once it is on GitHub, go to the GitHub repository and create a new release as follows:

  1. Click on Releases > Create a new release.
  2. Enter a tag name for your release ( usually it is version number like 1.0) and select branch.
  3. Enter release title and optional description and click publish release.

Next go to https://jitpack.io/ and enter your GitHub repository URL and click ‘Look up’. After that it will list the release that you have created on GitHub and start the build process.

Using the Library in an Android Project

Once the build process is successful click ‘Get it’ for the release which you want to use. JitPack will show you the dependency which you can add in any Android project’s build.gradle file to use the library.

Add the following repository configuration to the settings.gradle file of the Android project in which you want to use the library.

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' } // Add this line
}
}

This allows gradle to resolve the dependency for your library.

And that’s it. Your Android library is now published and can be used by anyone.

You can find the sample code on my GitHub.

For more details refer the official documentation.

--

--

Blair Fernandes

I am a passionate programmer 💻, geek 🤓 & an avid gamer🎮