Publish your library to jCenter in 3 steps!

I believe that you have copied your code from one project to the other at least a few times in your carrier as software developer. That’s normal, nobody want to create the same utilities or custom widgets all over again; copy&paste is so easy :-). However that’s not the best way to reuse your code, and if you are reading this post probably you have already figured out that it will be a great idea to place that reusable code in a library and eventually publish it!

Browsing the internet it seems that there isn’t an easy guide to accomplish this job, and at least in my case, I got discouraged many times by the complexity of the procedure. However, a few days ago, I have decided to learn how to do it and to share it with you in a simple way. I can promise that in 3 steps you also will be able to publish your own library!

[All the code shown in this post is available on GitHub]

Step 1: Build your library.

For the sake of this example, let’s say that we want to build a library with some Date manipulation utils that we may need in various projects. To keep it short, this Date utils at the moment contains only one method which is used to extract the time as a String from a Date Object:

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

    public static String getTime(Date date) {
        SimpleDateFormat timeFormatter = new SimpleDateFormat("kk:mm");
        return timeFormatter.format(date);
    }
}

To make a library, it is possible to create a library module inside an existing project, however, to keep it clean, I suggest to create an new project where you have both your library module and an example app which shows how to use the library.

So let’s create a new DateUtilsExample project in Android Studio by following the steps below.

Screen Shot 2015-11-01 at 11.08.28

Screen Shot 2015-11-01 at 11.09.11

Screen Shot 2015-11-01 at 11.09.32

Screen Shot 2015-11-01 at 11.10.10

Now create a new module…

Screen Shot 2015-11-01 at 11.15.47

…by carefully selecting Android Library…

Screen Shot 2015-11-01 at 11.16.11

Screen Shot 2015-11-01 at 11.23.35

…and add your utilities to it as shown below.

Screen Shot 2015-11-01 at 11.25.48

That’s it: you have your library!

Step 2: Prepare your Bintray space.

Before being able to upload your library to jCenter, you first need to upload it to a distribution centre like Bintray.

The sign up procedure is fairly simple, so please do it by yourself. Afterwards, log-in and click on maven,

Screen Shot 2015-11-01 at 11.45.24

then click on “Add New Package“.

Screen Shot 2015-11-01 at 11.48.39

Fill-in the required informations as shown below, with your exact module name and your GitHub repo links.

Screen Shot 2015-11-01 at 11.55.30

Click on “Create Package” and you are done for this second step.

Step 3: Upload your library.

At this point you need to configure your library module in such a way that you can upload it to your bintray space first and from there to jCentre.

Add to the project build.gradle file the following dependencies:

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}

Screen Shot 2015-11-01 at 12.11.26

Add your bintray authentication details in the local.properties file, which by default is added to the .gitignore file by Android Studio (those information are sensitive and should not be pushed to your version control system).

bintray.user=YOUR_USERNAME
bintray.apikey=YOUR_API_KEY

The username is the one that you created when you signed-up. The api key is listed in your profile configuration: click edit under your name and then the API Key tab.

Screen Shot 2015-11-01 at 12.22.06

Go back to Android Studio and create in your module a file named install.gradle with the following lines (make sure to change the lines commented with your own information):

apply plugin: 'com.github.dcendents.android-maven'

group = 'com.antoniocappiello.library' // CREATE A GROUP ID FOR YOUR LIBRARY

install {
    repositories.mavenInstaller {
        pom {
            project {
                packaging 'aar'
                groupId 'com.antoniocappiello.library' // CREATE A GROUP ID FOR YOUR LIBRARY
                artifactId 'date-utils' // THE NAME OF YOUR MODULE

                name 'DateUtils' // YOUR LIBRARY NAME
                description 'A collections of methods to work with Date objects.' // YOUR LIBRARY DESCRIPTION
                url 'https://github.com/AntonioCappiello/date-utils' // YOUR SITE

                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'AntonioCappiello' //YOUR ID
                        name 'Antonio Cappiello' //YOUR NAME
                        email 'antoniocappiello1@gmail.com' //YOUR EMAIL
                    }
                }
                scm {
                    connection 'https://github.com/AntonioCappiello/date-utils.git' // YOUR GIT REPO
                    developerConnection 'https://github.com/AntonioCappiello/date-utils.git' // YOUR GIT REPO
                    url 'https://github.com/AntonioCappiello/date-utils' // YOUR SITE

                }
            }
        }
    }
}

Now create in your module another gradle file, named bintray.gradle, which contain your bintray configuration used to automatically upload your library. Also here, remember to change the lines commented with your own information.

apply plugin: 'com.jfrog.bintray'

version = '0.0.1' //YOUR LIBRARY VERSION

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}
artifacts {
    archives javadocJar
    archives sourcesJar
}

// Bintray
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")

    configurations = ['archives']
    pkg {
        repo = 'maven'
        name = 'date-utils' //YOUR PACKAGE NAME
        desc = 'A collections of methods to work with Date objects.' // YOUR LIBRARY DESCRIPTION
        websiteUrl = 'https://github.com/AntonioCappiello/date-utils' // YOUR SITE
        vcsUrl = 'https://github.com/AntonioCappiello/date-utils.git' // YOUR GIT REPO
        licenses = ["Apache-2.0"] // A LIST OF YOUR LICENCES
        publish = true
        publicDownloadNumbers = true
    }
}

Finally “apply” those two new gradle scripts to your module build.gradle file, by writing those two lines at the bottom of that file:

apply from: 'install.gradle'
apply from: 'bintray.gradle'

(In case the sync and build fail, then make sure that in your project structure you have gradle version 2.2.1).

At this point everything is set-up.

Just execute those two commands in your terminal and your library will be uploaded to bintray.

> ./gradlew install
> ./gradlew bintrayUpload

Screen Shot 2015-11-01 at 15.14.38 Screen Shot 2015-11-01 at 15.15.27

Let’s check the bintray package status. If everything worked as expected, then you will see your library version uploaded as in the picture below.

Screen Shot 2015-11-01 at 15.17.09

Feel free to click on the Files tab and browse the content of your library to have an idea of how it looks like.

Screen Shot 2015-11-01 at 15.20.50

Your library is now available on your Maven repository, which means that if anyone wants to use it, then he needs to add the following line to the app gradle dependencies

compile 'com.antoniocappiello.library:date-utils:0.0.1'

beside your maven url (showed below) inside his repositories configuration.

Screen Shot 2015-11-01 at 15.23.54

Of course we don’t want to use a repository url for each library that we include in our project. So let’s be a bit more professional and let’s link our library to jCenter so that nobody need to add the maven url to their repositories.

To do this it is very simple, just click the button “Add to jCenter” from the package configuration.

Screen Shot 2015-11-01 at 15.32.09Screen Shot 2015-11-01 at 15.33.23In the page that will open, click “Send” and then wait. It may take a few hours before that your request has been accepted. When it happens, you should get a notification via email and you should also see the jCenter badge in your package details, under the “Linked to” section.

Screen Shot 2015-11-01 at 15.36.20

Well, that’s all. Congratulations! Your library is now on jCenter!!! 

Try to browse to http://jcenter.bintray.com and go to the sub-directories “/com/antoniocappiello/library/” (by using your group id instead then mine 🙂 ) and you will see your library in there.

And now, if you want to do be very cool, update your git repo with an example of use of your library, the community will appreciate it and more importantly, you will make sure that your library actually works 🙂

[All the code shown in this post is available on GitHub]

Advertisements

6 thoughts on “Publish your library to jCenter in 3 steps!

  1. Pingback: Happy DevOps Day Android Things – android.geek.nz

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s