Publish Android Library with Jitpack

Ayoub Ben Khemis
6 min readMay 6, 2021

--

Photo by 🇸🇮 Janko Ferlič on Unsplash

Introduction

Recently I started playing with Jetpack Compose and I really like it! I have built a CircularProgressView component with this modern UI toolkit, and I want to share it. So I was searching for tips out there that may help me publish my CircularProgressView as a library to make it available to others.

I found out these choices:

  • JCenter: is retiring soon.
  • MavenCentral: a good solution to publish your library. However it requires many steps to go through. If you are interested in using MavenCentral, you can read more about in this great article from Stream.
  • Jitpack: a simple way for publishing Android library.

In this article, for the sake of simplicity, I will cover the publishing process of an Android library using Jitpack.

What’s Jitpack ?

JitPack is a novel package repository for JVM and Android projects. It builds Git projects on demand and provides you with ready-to-use artifacts (jar, aar).

Why Jitpack ?

It’s very easy to get started with, you simply have to push your project on Github, and JitPack checks out the code, builds it and distributes it.

let’s continue and go step by step to the publishing process.

Getting Started

A quick overview of the steps to go through:

1- Create a library module.

2- Use maven publish plugin.

3- Setup the publication script.

4- Make sure to publish your library to MavenLocal() before going remote.

5- Publishing library on Jitpack.

6- Check the library usage in a basic app

Let’s detail all these steps now!

Creating Android Library

In this article I will use my CircularProgressView library as an example.

To get started, create a new library module under Android Studio.

File -> new -> new module, then select Android Library and enter the name of your library.

Then create your library files. In this example, I created two composable functions InfiniteProgressView and DeterminateProgressView.

Before I dive into publishing process, let’s try to use the library locally by adding a sample app in the project. To do this, in app/build.gradle, add the library dependency as below:

dependencies {
implementation(project(":circularporgressview"))
}

Now, the two composable functions are accessible within the sample app and we can use them to draw a custom CircularProgressView.

Using Maven Publish Plugin

Maven Publish plugin is responsible for uploading your library or application artifacts to Maven repository. Here what the official documentation says:

The Maven Publish Plugin provides the ability to publish build artifacts to an Apache Maven repository. A module published to a Maven repository can be consumed by Maven, Gradle (see Declaring Dependencies) and other tools that understand the Maven repository format. You can learn about the fundamentals of publishing in Publishing Overview.

To start using Maven Publish Plugin, you need to add the following code to the build.gradle of library module:

plugins {
id 'maven-publish'
}

or in the module’s build.gradle.kts

plugins {
`maven-publish`
}

Publication Script

As mentioned in Android guide for using Maven Publish plugin, the script below produces a publication for an AAR library’s release build variant. We will add it to the root level of the build.gradle of our library module.

Publishing Library on MavenLocal()

After adding the publication script, we can test our work locally by installing the library in the local Maven repository(~/.m2/repository).

So let’s check the local maven publishing process:

Run the following command on Android Studio terminal if you have named your publication “release”:

./gradlew publishReleasePublicationToMavenLocal

or simply run publishReleasePublicationToMavenLocal task from Gradle tasks tab as shown below:

If everything goes well in the previous steps, your dependencies folders with versions should be added to the ~/.m2 folder (for Mac users):

Now our library is ready to be published to some remote Maven. I will cover that in the next section.

Publishing library on Jitpack

Commit all the changes we have done to the library module, the full version of build.gradle file should look like below:

After pushing the project to Github, create a GitHub release or add a git tag to your Github repo.

Lookup for your repo

open https://jitpack.io/ and lookup for your Github repo

If everything goes well in the previous step, you should have a Successful Build as below. Then, you can jump directly to the final step : Check the library usage in a basic app.

If there is any error, you can see it in the log tab with details. For this example I can check what went wrong with my 1.0.0 version.

It says the Android Gradle plugin requires Java 11 to run and you are currently using Java 1.8. We can see in the screenshot above the JVM version used is 1.8.0.

As mentioned in the jitpack.io documentaion:

Builds are run with Java 8 by default but can be configured using a jitpack.yml file.

So we can configure builds to run on Java11 by adding jitpack.yml file in the root of our project. This file contains the following code:

jdk:
- openjdk11

Make sure to increment the library version:

publications {
release(MavenPublication) {
from components.release

groupId = 'com.bks.compose.circularprogressview'
artifactId = 'circular-progress-view'
version = '1.3.0'
}
}

Commit changes to Github repo, create another release and lookup again for your repo. Now you should have a Build Success.

Check the library usage in a basic app

Congratulation! our library is ready to be used by other applications 💪 💪

All you need to do is to add jitpack repository to the project build.gradle.

allprojects {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}

and finally, in the app module build.gradle add the library dependencies.

implementation 'com.github.ayoubbk:Compose-Circular-Progress-View:1.3.0'

--

--

Ayoub Ben Khemis
Ayoub Ben Khemis

Written by Ayoub Ben Khemis

Software Engineer, Android Developer

Responses (3)