JitPack - Publish Your Android Library With Examples

In this tutorial, we’ll demonstrate how to create your own open source application and use it as a dependency. You must have come across many open source libraries. Definitely, you would have used a few in your projects. Now it’s your turn to create one. We’ll be creating a library for vertical scrolling ViewPagers. Library name would aptly be ViewPagerVertical.

JitPack

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

It’s a good practice to create a sample app along with the library.

Creating our Android Library

Let’s get started to create our Android Library by starting a new Android Studio project.

Choose the Tabbed Activity template, since it gives a default code for ViewPager in our activity.
Goto File | New Module | Android Library.

android-create-library-jitpack-new-module

Enter the name of the module in the following screen. Let’s see how our new module looks in the project structure.

Android Library Project Structure

android-create-library-jitpack-project-structure

Let’s create a new java class in our viewpagervertical module.

The code from the VerticalViewPager.java class is given below.

Note: We’ve covered VerticalViewPager in the Nested ViewPagers Tutorial.

Add the Module dependency in our app module
To add the library module in our app module, go to File | Project Structure | Click the dependency Tab| Add New Module Dependency.

android library add dependency

The following dependency should be added in your build.gradle of the app module.

Now in our MainActivity.java class and activity_main.xml of the app module, replace the ViewPager instances with VerticalViewPager.

That’s it, our application is ready. Let’s check the output once before creating the library.

android vertical viewpager output

Publishing Android Library

To get our library production ready, we need to do the following changes in the Gradle.

Add the following dependency to the project level root build.gradle file.

In the library module build.gradle file, add the following two lines after the top line apply plugin: 'com.android.library'.

In the group field replace the last string with your github username when you create your library.

Sync Changes!

Go Ahead and push your project to GitHub.

You can go to VCS | Import into version control | Share Project on GitHub.

android-create-library-jitpack-project-structure

Once it is done, create a release TAG for your repo.

Enter the following lines in the command line console present in Android Studio.

Each time you do changes in the library, you need to update the version numbers in the above lines.

Typically version numbers are of form 1.0.x for minor bugs. 1.x.x for serious bugs/new features, 2.x.x for major changes.

Now the final stage!

Open JitPack, sign-in and search your repository by the url.

jitpack search library

In our case it looks like this:

android-create-library-jitpack-project-structure

Clicking on Get would show you the dependency url. Follow the instructions below.
Typically you’ll need to add the following in the root build.gradle.

Replace implementation ‘module_name’ in your app’s build.gradle with the following dependency.

That’s it. We’ve created our first Android Library.

You can download the Android VerticalViewPager Project from the link below. Try modifying the VerticalViewPager and publish it as your library following the above set of instructions.

By admin

Leave a Reply

%d bloggers like this: