How to publish Android Library on Bintray/JCenter

Sometimes being a lazy developer is a good thing, especially when you want to reduce repetitive tasks and find efficient ways to accomplish more with less.

Many times I have to re-write code to solve the same problem again and again, which is painful. As developers, all of us have copied code between projects. But this is not the best way to reuse code.  It’s better to convert that code into a library so that it can be reused easily to save time.

Bintray JCenter

Here, I will try to guide you on how to package your code into a library and share it with the world in just 3 easy steps.

In our first step, we will create a library module in our Android Studio that we want to publish. After that we will create a space on Bintray and make a repository there so that we can upload our library module in our second step. In the final step we will upload our library module on Bintray. Once module gets successfully uploaded we will push it to JCenter so that it becomes available to everyone.

So let’s start-

1. Make library module in Android Studio

Let’s build a library with some time manipulation utils that we might need to use in various projects. For the sake of this example, the library will contain a single method that will extract time as a String.

So let’s create a new project and name it as TimeUtilsExample in Android Studio. This will keep both Android Library & app that will use that library project.

Your new project is ready now, let’s create a new module inside this project.

Make sure to select Android Library from the dialog.

Let’s add some utility method in the module. The basic idea of this library module is to show the time picker dialog and select the time and get back into the app.

Bingo! You have created your first Library Project.

2. Create Space on Bintray

Before uploading the library on JCenter you need to upload it on Bintray first and then you can push it to JCenter.

I am assuming that you already have an account on Bintray and it’s simple too. Once you login into the account there is an option “Add New Repository”. Click on it.

Fill the details as shown in the screenshot and click on “Create”. Make sure to select “Maven” in type section.

You will get a success message and will be redirected to the screenshot below. Click on “Add New Package” to create a package in the repository.

Fill in the required information as shown below with your exact module name and your GitHub repo links and click on “Create Package”.

Congrats! You have successfully completed the second step.

3. Upload your library on Bintray and JCenter

Now you need to configure your library so that you can upload it to Bintray followed by JCenter.
Modify the project build.gradle file by adding these below dependencies:

dependencies {
   classpath 'com.android.tools.build:gradle:2.3.1'
   classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
   classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

Add your Bintray authentication details in the local.properties file. That information should not be pushed to the version control system.

bintray.user= “YOUR USERNAME”
bintray.apikey= “YOUR API KEY”

You can find both credentials in your account by following these steps-
Open your Bintray account, click on your name and then click edit under your name and then the API Key tab.

Now we need to modify our module build.gradle file. Open the file and modify according to the below-mentioned steps.

buildscript {
   repositories {
   dependencies {
       classpath 'com.android.tools.build:gradle:1.1.0'

ext {
   bintrayRepo = 'Utility' // your repo name
   bintrayName = 'time-utils' // has to be same as your library module name
   publishedGroupId = 'com.timeutility' // your module package name
   libraryName = ‘time-utils’ 
   artifact = 'time-utils' // has to be same as your library module name
   libraryDescription = 'A set of methods used to manipulate time object'
   siteUrl = 'https://github.com/androidCode/time-utils'
   gitUrl = 'https://github.com/androidCode/time-utils.git'
   libraryVersion = '1.0'
   developerId = ‘sachit’
   developerName = Sachit
   developerEmail = 'sachit.wadhawan@quovantis.com'
   licenseName = 'The Apache Software License, Version 2.0'
   licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
   allLicenses = ["Apache-2.0"]

android {
   // -------- your target version and all required configuration. These are normal as for other projects and generate by android studio itself

dependencies {
   // Your dependecies
// add it to the bottom of the file

apply from: 'https://raw.githubusercontent.com/numetriclabz/jcenter/master/installv.gradle'
apply from: 'https://raw.githubusercontent.com/numetriclabz/jcenter/master/bintrayv.gradle'

Sync the project. 
Once everything is configured, run the command below in your terminal, in root of the project.

./gradlew clean build install bintrayUpload

If there is nothing wrong, it will show something like this:

Shout Hurray! if it shows BUILD SUCCESSFUL.

Check your package in the Bintray web interface. You will see a change in Versions area. Now it needs to be pushed to JCenter. Now once your project is up on Bintray, simply hit “Add to JCenter” button to sync with JCenter.

It may take a few hours before your request has been accepted. When it happens, you should get a notification via email and also see the jCenter badge in your package details under the “Linked to” section.

Congratulations! Your library is now on jCenter!!! You can give the gradle path into your project now. For example my dependency looks like this:

compile 'com.sachit.timehelper:timehelper:1.0'

Update your repository with an example that indicates the use of your library so that users get to know about your library and how they can use it in their projects. Also, make sure that your library actually works before you share it with the world.

I hope this blog was useful. If you have any questions, please drop in your comments. Thank you for reading.

You might also like