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.
Now create a new module…
…by carefully selecting Android Library…
…and add your utilities to it as shown below.
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,
then click on “Add New Package“.
Fill-in the required informations as shown below, with your exact module name and your GitHub repo links.
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'
}
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.
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
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.
Feel free to click on the Files tab and browse the content of your library to have an idea of how it looks like.
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.
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.

In 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.
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]



















July 4, 2016, 8:06 am
Good tutorial 🙂 now i have made my first library 🙂
July 4, 2016, 8:07 am
Thanks, I am glad to hear that!
November 28, 2016, 2:27 am
It seems now i hace to create an ‘organization’ on Bintray to create and publish repositories, and it has a cost… Do you know what happened? I though I could publish my libs for free…
November 28, 2016, 2:28 am
Now I have to create an ‘organization’ on bintray to create repos and publish my libs and this is not free… Do you know what happened?
November 28, 2016, 7:14 am
Hi jflavio1, at the time of writing the article it was free. Not sure if it is changed now.
December 27, 2016, 8:30 am
January 10, 2017, 1:17 pm
Good tutorial. I tried to publish my own library. To upload the files to bintray is success, but I can’t see the button “Add To JCenter”. What happens or what I have missed?
February 6, 2017, 7:42 am
thank you but
what is this Error ?
Error:Execution failed for task ‘:my-lib:bintrayUpload’.
> Could not create version ‘0.1’: HTTP/1.1 401 Unauthorized [message:This resource requires authentication]
June 19, 2017, 11:22 am
Hi Antonio, I just published my library to bintray and realize that they automatically link it to jCenter, is there a way to unlink it and host only on my Maven repository?
August 23, 2017, 6:33 am
Hi, thx for your good tutorial…
October 19, 2017, 1:54 am
Nice article brow,
i try to create two library but one of both geting error, is that not work to create multiple library in 1 project? for example i create library date-util, time-util, and other-util.
sorry for my bad english 😀