Simple way to publish your Android library to JCenter

Yegor Zatsepin
4 min readAug 24, 2017

--

DEPRECATED. Read this: https://jfrog.com/blog/into-the-sunset-bintray-jcenter-gocenter-and-chartcenter/

First of all, many thanks to Sittiphol Phanvilai for his original article. It is very detailed and has interesting digressions about the repositories and gradle. Please follow it if you want your library published on both JCenter and Maven Central.

Here is my slightly updated and very dried-out version that drives you directly to the point. Here is the source code for reference.

1. Setup your repository on Bintray

Sign Up for an Open Source Account

Click the “Add New Repository” button.

Give your repository a name, select “Maven” as a type. Optionally, specify a default license and type a description.

Click the “Add New Package” button, fill in the data. You could use your GitHub repository for the “website” and “issue tracker” fields.

Now you should be able to access your new package using the Bintray URL. Here is mine: https://bintray.com/lissf/maven/diagonalscrollview

2. Setup your Android library project

Check your Android project. I highly recommend you to make sure that your project has two modules: the Android Application (sample) and the Android Library.

Open your local.properties file (I hope it is in your .gitignore already, make sure that it is). Add your Bintray username and API key. You can get your key at https://bintray.com/profile/edit.

bintray.user=USERNAME
bintray.apikey=API_KEY

Open the build.gradle of your library module. You’ll have to add two plugins to help you package and publish the library.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'

}
}

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

To the same file add the following block. Set your data to every variable. Your library will be available as “publishedGroupId:artifact:libraryVersion”. It’s “ua.org.tenletters.widget:diagonalscrollview:0.3.3” for me.

ext {
bintrayRepo = 'maven'
bintrayName = 'diagonalscrollview'

publishedGroupId = 'ua.org.tenletters.widget'
libraryName = 'DiagonalScrollView'
artifact = 'diagonalscrollview'

libraryDescription = 'This view is a container that supports diagonal scroll and fling gesture. It is based on AOSP HorizontalScrollView.'

siteUrl = 'https://github.com/LissF/DiagonalScrollView'
gitUrl = 'https://github.com/LissF/DiagonalScrollView.git'

libraryVersion = '0.3.3'

developerId = 'lissf'
developerName = 'Yegor Zatsepin'
developerEmail = 'Liss_F@ukr.net'

licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}

To the bottom of the same file you should add this script:

group = publishedGroupId
version = libraryVersion

install {
repositories.mavenInstaller {
pom.project {
packaging 'aar'
groupId publishedGroupId
artifactId artifact

name libraryName
description libraryDescription
url siteUrl

licenses {
license {
name licenseName
url licenseUrl
}
}
developers {
developer {
id developerId
name developerName
email developerEmail
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}

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

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
}

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 = bintrayRepo
name = bintrayName
desc = libraryDescription
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = allLicenses
dryRun = false
publish = true
override = false
publicDownloadNumbers = true
version {
desc = libraryDescription
}
}
}

This script is not supposed to be changed a lot, so it is safe to move it to a separate file and substitute it with this one line at the bottom of the build.gradle of your library module:

apply from: 'LINK_TO_SCRIPT'

3. Package and upload your library

Now you should run two gradle commands.

clean” is optional

First one is “install” to build your library and package everything that has to be published.

Second is “bintrayUpload” to, well, upload the files to Bintray.

After successful execution of theese commands, you should be able to see your package updated on https://bintray.com. New “version” should appear.

Now you may publish your library to JCenter. Click the “Add to JCenter” button.

In a few hours, you’ll receive an email. After that, everyone will be able to include your library to their Android projects with just one line.

--

--

Yegor Zatsepin

Senior Android Developer, Raspberry Pi enthusiast, Dungeon Master.