How to publish your own SDK or library in Android Studio (Part: 2)

Implementation and overview of jcenter for publishing SDK or library

Govind Prajapati
9 min readAug 4, 2020

As we discussed in Part 1 that why should we use SDK or library in our projects, How a Gradle pull the library from a repository and How to publish our library to Jitpack. Now in this part, we will understand about jcenter and maven central that what actually both of these.

Actually, both of them are the repositories having the same duty: hosting Java/Android libraries. It is a developer’s choice to upload their libraries to which one or maybe both.

But the big problem of Maven Central is it is surprisingly hard to upload the library too. To be able to do so, the developer has to be at some level of geeky. And with some more reason, for example, security concern and etc, Android Studio team decided to switch the default repository to jcenter instead as you can see that once you create a new project from the latest version of Android Studio, jcenter() would be automatically defined instead of mavenCentral().

There are a lot of good reasons why they decided to switch from Maven Central to jcenter. Here are some of the major one.

- jcenter is the largest Java Repository on earth. So whatever that is available on Maven Central could be implied that it would be available on jcenter as well. In other words, jcenter is a superset of Maven Central.

- It is incredibly easy to upload our own library to the repository. No need to sign or do any complex thing like we have to on Maven Central.

- If you want to upload your library to Maven Central you could do it easily with a single click on bintray site (and with some step of one-time setup).

With the above reasons and from my own experiences, I must say that it is the brilliant decision switching default repository to jcenter.

So this article will focus on just jcenter since once you successfully upload your library to jcenter.

How to publish your library to bintray and link to jcenter?

As described previously jcenter is a Maven Repository hosted by bintray.com.

Now let’s begin the most important part: uploading processes. The objective here is as simple as how to upload our library files to http://jcenter.bintray.com. Once we could do it, the library is published. Well … two things to concern: how to create an aar file and how to upload built files to the repository?

It is as easy as described in the image above.

Create an account in Bintray and create a repository.

Navigate to https://bintray.com/signup/oss and simply create an account. You can also signup with Github, Google or Twitter account for faster signup.

Login to the dashboard and add a new repository like this:

You can choose any name you want but for demo purposes I choose OwnSDK. Make sure the type is Maven. You can also choose the licence and press Create.

You should be on a page that looks like this:

Create a package for the repository in Bintray

Since everything is properly set up so far let’s go ahead and add a package to the repository we created in the previous step.

Navigate to your repository created in the previous Step and click at the Add New Package

Fill Package Details

Fill in the details like:

Name: your package name, e.g. OwnSDK

Licences: The licence that you are using for your package, e.g. Apache-2.0

Tags: The tags that better describe the technologies used in your library

Website: Your website URL, if there is none use the repository URL from Github or Bitbucket.

Issues Tracker: You can leave it blank or use the Github/Bitbucket repository URL/issues

Version Control: Your Github/Bitbucket URL of the project’s repository

After that, you can click at Create Package.

Once the package is created it should look like this:

Enable auto signing for Bintray

As mentioned above, we could upload a library to Maven Central through jcenter but to do that we need to sign that library first. bintray provides a mechanic to do that easily through a web interface that allows the library to be signed automatically once uploaded.

The first step is to generate a key via command line with the command below. (In case you use Windows, please do it under cygwin or cmder)

gpg --full-generate-key

You should be prompted to select a Passphrase after you press O so select one, then retype it and press enter. It should look something like this:

Once the key is created. Call the following command to see the created key’s information.

gpg --list-keys

You should now see something like the above. Now we have to upload the public key to keyservers. To do so, please call the following command and replace PUBLIC_KEY_ID with hexadecimal value in the pub line which is 729C5DBF40C53A3C8F9F0BFBB5D63DADD56B46C1 in this example.

gpg --keyserver hkp://pool.sks-keyservers.net --send-keys PUBLIC_KEY_ID

And then, export both public and private key as ASCII format with the following command and please replace yourmail@email.com to the email you used to create your own key in the previous step.

gpg -a --export yourmail@email.com > public_key_sender.asc
gpg -a --export-secret-key yourmail@email.com > private_key_sender.asc

After both keys are exported open Bintray’s Edit Profile page and click at GPG Signing. Fill in both Public Key and Private Key using content in public_key_sender.asc and private_key_sender.asc files exported in the previous step respectively.

Bintray GPG Signing

Put your public key and your private key and then click Update to save the keys. The last thing to do is go back to your repository and click Edit and then you should be presented to a page that looks like this:

and check the Checkbox that says GPG sign uploaded files automatically.

Create the Android Studio Library Project and Prepare files for upload to Bintray

Create an Android Studio Project and create a New Android Library Module that looks like this:

Modify the Project’s build.gradle to include those classpath’s:

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

It should look something like this:

Then in your library module’s build.gradle add those lines:

apply from: 'https://raw.githubusercontent.com/govindsa1011/maven-upload/master/bintray.gradle'
apply from: 'https://raw.githubusercontent.com/govindsa1011/maven-upload/master/publications.gradle'

It should look like this:

Notice that we also modified the versionName attribute from “1.0” to

project.getProperty("libraryVersion")

We did this just to make it easier for us to modify the version from only 1 place. Now the interesting part is to modify the gradle.properties file accordingly like this:

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
#
http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
libraryVersion=1.0.0
libraryName
=OwnSDK
libraryDescription
=Demo Description
publishedGroupId
=com.github.govindsa1011
artifact
=OwnSDK
bintrayRepo
=OwnSDK
bintrayName
=OwnSDK
siteUrl
=https://github.com/govindsa1011/OwnSDK
gitUrl
=https://github.com/govindsa1011/OwnSDK.git
developerId
=govindsa1011
developerName
=Govind Prajapati
developerEmail
=govind.prajapati.sa@gmail.com
licenseName
=The Apache Software License, Version 2.0
licenseUrl
=http://www.apache.org/licenses/LICENSE-2.0.txt
allLicenses
=["Apache-2.0"]

notice that I named my bintray repository “OwnSDK” earlier in Step 2 and the bintrayName(package name) as “OwnSDK”. The others are pretty much self explainable, you can use both different also.

Do not forget to modify your local.properties like this:

bintray.apikey=abcefdaslkjdhakjsdhlkaskkjd54asd321qw8dasd1
bintray.gpg.password
=mypassword
bintray.user
=mybintrayusername

You can find your API Key under Edit Profile in Bintray just as shown in here:

When you are done with all of these modifications open terminal from Android Studio and execute the tasks:

in Windows:

gradlew install

or if you are in Mac/Linux

gradle install

and after the task successfully executed you should execute the task:

in Windows:

gradlew bintrayUpload

or if you are in Mac/Linux

gradle bintrayUpload

If everything is successful you should see your files uploaded to Bintray packageName like it looks in here:

the .asc files mean that the files are automatically signed with the GPG Keys that we enabled earlier.

Add to jCenter

If you are happy with the library you can press from the General tab Add to jCenter.

Check the is pom and Host my snapshot as look in above image then add comments regarding (for example) what is the uses of your library, then just hit Send so that it will create a Request to bintray and once it is approved your package name will be included to jCenter.

You have to wait something like 2–3 hours normally for the request to be approved and once it is approved you will see something like this:

It is finally done. Your project now can be used by any developer that includes the jCenter() in Android Studio. You can check your project in jcenter.bintray.com like this for example:

https://dl.bintray.com/govindsa1011/OwnSDK

Please note that linking to jcenter is a one-time action. From now on, if you do any modification in your package, for example, upload new version binary, delete old version binary, etc. The change would affect jcenter as well. Anyway since your own repository and jcenter are at different place so you may need to wait for around 2–3 minutes to let jcenter sync the change.

And please be careful. If you decide to remove the whole package, library files placed on jcenter repository would not be deleted in this case. And they will be left just like zombie files which nobody could delete it anymore. So I suggest you that if you want to delete the whole package, please delete every single version from web interface first before removing the package.

Congratulations! That’s all. Although it requires quite a lot of steps but steps are quite straightforward. And most of them are one-time action. Once things are set, you almost have to do any additional step after that.

I hope this article will help you!

Reference:-

--

--

Govind Prajapati

Hi, I am a software developer at Solution Analysts. While not developing apps, I share my thoughts with peers. I just love to read, write, roam, and code!