Start contributing with Android Library & JetPack

How to create and publish an Android Library

Write once and use in every project -with GitHub & JetPack

  1. What is the necessity of the Android library
  2. Create an Android library
  3. Create an empty repository in GitHub
  4. Upload code to GitHub repository
  5. Create a Release in GitHub
  6. Open the git-repository in JitPack
  7. Generate JitPack Library
  8. Importing JitPack Library into a new project
  9. Updating GitHub repository & JitPack library
  10. Useful links

What is the necessity of the Android library

Android library is structurally same as an android app with resource files, source code, manifest file and so on. However, instead of compiling into an APK, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency so that we don’t need to create or write the same code all the time.

If you’re an android developer for a while you should’ve at-least used one library(maybe support libraries 😉). If you see GitHub, it has many open source libraries for the date picker, Image Loading and so on. The core intention of all these repository’s is to reuse the code.

My Experience to create first android library

A few days back in one of the companies I’m working as Android Lead has got a requirement to create a library with one of our app’s functionality so that it can be added as mini-app.

In this case, I thought of many ways like

  1. Integrate our app as a module to parent app but the problem is that they can see our code and also can alter it.
  2. To create JAR/AAR files but it has two problems firstly, they can see the code with some technical knowledge and when AAR files are created somehow it is not including all the dependencies of the library.
  3. Create the library, add it to git as a repository and add JitPack library support. so that the end developer can use it but can’t alter the code.

Create an Android library

You need to create a new project as you’re already familiar with the process I’m not gonna explain that now. After setting up the project successfully navigate as shown below

File > New > New Module > Android Library

Create new module panel

proceed through the next button then fill the details and click finish.

After the android studio completed the processing you can see your android library in the left project panel but it hasn’t finished yet. Now open project structure by pressing the following buttons
Ctrl + Alt + Shift + S

Now click on dependencies option in left side & select app module then press + in declared dependencies, have a look

Project structure

Now click on the Module dependency option in the top window then a new window opened as shown below. Now select your library and click ok.

importing module into project

After the android studio completed the processing you can add all the resource files & functionality into your library directories.

Create an empty repository in GitHub

Now after you implemented all the functionality required in your library its time to create a space for your library in GitHub for that first you need to create a repository, Let’s take it step by step

  1. Login into GitHub or create a new account if you haven’t already had one.
  2. Click the new button on the left panel to create a new repository. If you haven’t found a new button click on the GitHub icon at the top and it’ll take you to the home page where you can find the new button to create a new repository.
  3. Then fill all the details required in new repository form, have a look

You need to pay if you want the repository to be private.

Upload code to GitHub repository

After creating the repository now you’ve to upload the code into the repository through the instructions shown after you created the repository in GitHub.

things to be executed after creating repository

In our case, we are creating a new repository we need to follow the first set of instructions. I prefer to execute those commands in the terminal panel of Android Studio which is present at the bottom

Terminal in android studio

While executing those commands you should authorize with your GitHub credentials and the 4th command which is shown below is responsible to link your local code to the empty repository you created.

git remote add origin https://github.com/SG-K/FirstLibrary.git

If everything goes as planned the code will be uploaded to git and if you refresh the repository page you will see that.

Now you can do whatever changes you want and execute below commands to update in your git repository.

git add .
git commit -m “message”
git pull
git push

Create a Release in GitHub

Now that your library is ready in GitHub, the next step is to create a release.

A release is nothing but a way to tell the user that we have tested that library and it is stable to use or to announce that new functionality is added. Each release has version number through user can update our library in their app.

To create a release click on the releases tab in repository home page

Releases tab in GitHub

Then click on Draft new release after that fill the details of the release like Version number, description explaining about the release and from which branch code does the release built on. After finishing that you publish the release or you can it for later.

Open the git-repository in JitPack

JitPack is a package repository for JVM and Android Projects. It contains the aar and dependency jars of the libraries.

Now how do you link your GitHub repository to jitpack, This is pretty easy first, you need to copy the repository URL

repository link

Then open Jitpack and paste the URL then hit the Look Up button. Here JitPack will take care of everything of connecting the repository

Generate JitPack Library

Now that we’ve our repository in Jitpack and as said above after clicking the Look Up button you will be shown a list of releases of your repository from which you can create a Jitpack library by pressing get it button beside the release.

JitPack releases list

After clicking Get it button JitPack will process the request and generate the implementation Gradle link as shown below

After you click Got it button Jitpack starts processing your repository after finishing if the build is successful you’ll be shown green file like above and if not it’ll be in red color. If the build fails your library can’t be imported into the user project.

Jitpack library integration instructions

Importing JitPack Library into a new project

All of the above this is the easiest part, you just need to add jitpack support to your project level Gradle file with the following code

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

than add below code in build.gradle file of the module

dependencies {
implementation 'com.github.SG-K:New-Sample-Library:0.0.1'
}

Updating GitHub repository & JitPack library

Now that you’ve released the version, after a few days or months, you might need to give an update for that library.
It is no different than the first release. When you want to give an update you just need to create a release and use that release in the Jitpack to create the library so when the users updated the version number with the latest your new functionality in the library will be imported to there project.

That’s it you’ve created & published your first library 🍻Cheers!!

You can find me on Medium, Twitter and LinkedIn Cheers!

Thank you for reading.

Learn. Code. Write. Repeat. Visit me at https://about.me/sivaganesh_kantamani & Join my email list at https://sgkantamani.substack.com/p/subscribe

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store