Publish an Android Library - Androhub

Publish an Android Library

Publish an Android Library

In the previous article, we learned how to create an Android Library. In this article, we will learn how to Publish an Android Library.

Benefits of Publishing an Android Library:

  1. Easy code management.
  2. One point of access to your library.
  3. Use the library in multiple projects.

There are several places where you can publish an Android library:

  1. Jitpack
  2. JFrog
  3. AWS

All the above options can host your public and private repositories. Hosting public repositories is free but private repositories are not.

Prerequisites

  1. Create an Android Library
  2. Working with GIT

VIDEO TUTORIAL

To publish an Android library we will be using GIT and Github. There is already an article and video about the same which you can go through before further reading. This will help you to understand the commands and process better.

Let’s continue where we left off from the previous article.

Steps to Publish the Library

1. Open the build.gradle.kts of networkmodule and make the following changes:

Add the Maven plugin, to publish build artifacts to the Maven repository.

Sync the project and then add the code below to declare your library module publication.

Full build.gradle.kts file after the above changes:

2. We will be using Jitpack for publishing our library as it is easy to use. To make the build at Jitpack we have to create a new file named jitpack.yml under the root directory and add the following code:

We required this file as the project is built on Java 17 but Jitpack works on Java 8. So to customize the setup we have to do this extra step. To learn more about this click here.

Note: If your project is configured to run on Java 8 then you can skip the above step.

3. Now commit your changes and push the code to your GitHub repository. Click here to learn how to push code to GitHub.

4. Open Jitpack site and enter your GitHub repository link in the input box and click on the Look up button.

Jitpack Repository LookUp

By doing this Jitpack will find the releases created in the repository. By default, it will pick the following releases:

  1. master-SNAPSHOT: Pull the artifact with all your master branch code.
  2. commit-Id: Pull the artifact based on commit Ids.

Jitpack Default LookUp

5. To provide the stable release artifact. First, we have to create Git Tags. To do that click on File => Git => New tag and this will open the below window where you have to enter the Tag Name and click on the Create Tag button.

Create Tag

6. After creating the Tag, we have to push the tag. To do that click on the branches dropdown and click on the Push option. This will open the below window where you have to check if the Push tags option is checked or not. If not then check it and push the code.

Push Tags

7. Go to the GitHub repository and refresh the page to see if Tags have been pushed successfully or not.

Repository before pushing Tag.

Before pushing tag

Repository after pushing Tag. Here you can see that the Tag count has increased to 1.

After pushing tag

8. Creating a Tag is not sufficient for Jitpack to Look Up for the releases. We have to create a new release from GitHub.

Click on Create a new release button.

Create New Release

9. Creating a new release will open a new screen where we have to select Tag and Branch from where the release has to be made. Then enter the release info like title and description which will represent what exactly this release contains.

Create new release form

After entering all the information click on the Publish Release button which is at the bottom of the screen when you scroll.

Publish release

On clicking Publish Release, a new screen will open which shows details about the created release.

Release Created

10. Let’s open the Jitpack again and click on the Look Up button. This time it will list all your created releases from the Tags.

Jitpack release building

Now, Jitpack will start building the artifact of the library module. The process goes like this:

  1. In-Progress: This represents that artifact building is in progress and can be identified by the loading bar as shown in the above image.
  2. Failed: Represents that artifact building has failed and can be identified by a red file icon under the Log column (see above image). Clicking on the file icon will lead you to the full log which will help you to fix the problem in building the artifact.
  3. Success: Represents that artifact building is a success and can be identified by a green file icon under the Log column (see below image). This means that the artifact is ready to be consumed in the projects.

Jitpack release success

 

Use the Published Library

Now we will learn how we can use the published library from Jitpack in our projects. Open any of your projects where you want to use your published library.

1. Open settings.gradle.kts file and add the Maven Jitpack URL to fetch the artifact.

Full settings.gradle.kts file after adding Maven:

2. Now open app-level build.gradle.kts file and add the following dependency and sync the project.

For your library, the dependency will be like this:

3. To upgrade the library version just commit and push the updated code to the repository and follow the steps of creating tags and creating a new release.

Happy Coding. :)




Post comment

Your email address will not be published. Required fields are marked *