How to Publish an Open Source Android Library on JCenter

Mithat Sinan Sarı
Hipo
Published in
5 min readDec 18, 2019

--

At Hipo, we love giving back to the community through open source packages. So as much as possible, we set aside some time to publish some of our work for others to use. For Android packages, there are several ways of publishing open source libraries, such as jitpack, maven and jcenter.

Easiest one is jitpack.io
Just copy your git repo URL and paste it, and after a few clicks you are done! Ready to go! But as with everything else in life, you sacrifice something to make it so easy. We need to add it manually to project level build.gradle as shown below and also we cannot have full control over our published software and how it is distributed to the world like we do in JCenter.

If you want to learn what a remote repository is and how it works, you can check here.

Why did we choose JCenter instead of Jitpack?

Sharing a library in JCenter is a bit harder than Jitpack but it comes with some big advantages. As one of the most popular Maven hubs, it exposes your package to a huge audience, and yet, you retain full ownership and can control how your package evolves. Basically, you upload your package to one of your public Maven repositories and ask for it to be included in JCenter. Once approved by the Bintray team, your package will be searchable on JCenter and freely available for download.

Let’s dive in and publish our library!

  1. First of all, we need to publish our project on Github.
  2. Then we need to create an account on BintrayOss. It should be oss (open source plan)
  3. On main page, click “Add New Repository”

4. Fill in the required fields

  • Name: Library name
  • Type: Library type: Should be Maven
  • Licenses: Library licenses
  • Description: Library description, make sure this is a good explanation!

5. We’ve created the library. Now we should create package for the library. Click on “Add New Package”

6. Fill in the required fields:

  • Name: This should be the same as package name of the library
  • Description: Description of package
  • Licenses: Package licenses
  • Maturity: Describes the state of development of your library or artifact. If it is a production ready library, you’d select “Official” otherwise, you can select whichever option you feel is closest to the state of your project.
  • Website: Git repo url of project
  • Issue tracker: Issues url of git project repo
  • Version control: Git repo with .git postfix

Then click “Create Package”

7. Go to package page then click “New Version”

8. Set version of library then click “Create Version”

This is the end of Bintray setup. Now we should setup our project.

9. First we should add classpathes of Bintray and Maven into Project level gradle dependencies:

10. Then in library build.gradle file, we should add some necessary fields. These fields are need for readymade scripts to upload library to bintray easily.

Don’t forget to add “apply from x.gradle” at the end of library module build.gradle.

publish.gradle is a file that we are going to create on the next step. For now just add below snippet into your module’s build.gradle

After that, we need to create new gradle file in library module and paste below gist which helps us to upload bintray easily.
For example; publish.gradle.

Lastly we need to add bintray username and apikey to local.properties file like shown below;

Great! We are good to go!

Just type below lines to terminal;

  • ./gradlew install
  • ./gradlew bintrayUpload

IN CASE OF BUILD FAILURE
Sometimes it fails because of Javadoc. First you should check if everything is correct. Non-ascii characters also fails build. If you are sure that everything is correct, then you can disable Javadoc. Simply copy-paste this scope into library build.gradle.
You can check here to learn about Javadoc.

IMPORTANT NOTE for 401 Unauthorized;
If you are publishing your library under an organisation,

First: You should use organisation owner’s username and api key

Second: You should set userOrg = ‘your_organisation_name’ in publish.gradle

Third: Or for better use case, check the gist below

After calling these two lines in order, (if everything is correct, it will print “Build Successful”). Then we will be able to see latest version in the dashboard;

After clicking our last version, we should see “Version Publication Date” and “<dependency/>” snippet.

Lastly, we will click to “Add to JCenter” button, write library description and wait for the approval. Thats it!

After it is approved, it should be implemented like:

There you go, now go ahead and publish some open source libraries!

Keep an eye on posts from Hipo by subscribing to our newsletter and following us on Twitter.

--

--