Beginners Guide for Publishing a Kotlin Library to jCenter

I haven’t published an open source library before but always wanted to. If you are in a similar situation, having troubles with Javadoc generation or bintray configuration, than this post could be handy.
So one day I decided to make it happen and here is what I have learned in the process.
Since the idea was a proof of concept, please don’t expect my library to be shiny, very useful, or useful at all actually…

1.Creating a library

If you don’t have a library there will be nothing to upload, so let’s begin with this step. Here is the official documentation.

Create a new project
Right click on app → New → Module
Android Library → Next
Enter the name → Finish
You should see the new module in the Project’s view.

I won’t be talking what a library is and what it contains since it is offtopic. So, assuming you are ready. Let’s continue.

2. Bintray

You may be asking what Bintray has to do with jCenter? Well, it appears to be the official place where you upload your library and then ask jCenter authorities to accept it.
  • First of all, you should create an open source (OSS) account.
  • Add a repository. Its name doesn’t matter. Some people call it “maven”. Its type should be Maven. You may also select a default license and add a description.

Click Create
Click Add New Package
Click Create Package
Click on the package name
You should see your package with no versions available

Done.

3. Gradle configuration

Here comes the gradle-bintray-plugin configuration hell, that I hope I can help with. Add the green lines to your project’s build.gradle file. Here is my project’s build.gradle file which you can use as a reference.

build.gradle (Project)

Here are the links to the applied plugins with more information about how to use them and what are their other capabilities:

Mark your librarie’s build.gradle

Click Ctrl+C and Ctrl+V to create a copy of your library’s gradle.

Name it deploy.gradle

The deploy.gradle file will contain the bintray configuration. The only value which is not set in it directly is the API key. Copy and paste the content from here.

deploy.gradle

But at that time you don’t have a file named keystore.gradle. So let’s create one and this time don’t add it to git since it will contain your bintray_key only. Again copy and paste the gradle file and name it keystore.gradle.

keystore.gradle

In order to find <your_bintray_API_key> go to the bintray site and click on Edit Profile → API Key → Copy


In your library’s buld.gradle file you need to apply deploy.gradle, add the plugins and configure dokka’s output format:

build.gradle(Module: flyingfab)

4. Publishing

And after successfully configuring the plugins you should generate the .aar file by opening the Android Studio’s terminal and entering the following command:
$ ./gradlew install
If everything goes well you will see the green text “BUILD SUCCESSFUL”. If you have any errors you need to fix them. When everything is ok, upload your library to bintray:
$ ./gradlew bintrayUpload
Again you should see “BUILD SUCCESSFUL”.

Building and publishing from the console.

If you go to bintray and navigate to your package’s page you will see that a version is now available. The Maven and Gradle dependency snippets should also be available.

Your package’s bintray page

If you don’t see your version wait a bit. If you still don’t see it, I am afraid that your configuration is not correct. Read the “Gradle configuration” sectionagain and be careful not to miss any step.
Now you are one click away from being published to jCenter. Click on the Add to jCenter button. Fill the message and send it. You probably will be approved within a couple of hours or even faster. Enjoy :)

Add to jCenter

And here are some extra tips:
  • You can add/change way more things from gradle. For more information look into the gradle-bintray-plugin#buildgradle.
  • To add the latest version shield there is a button above the Versions section of your package’s page in bintray. Copy and paste it to your readme. One thing I noticed is that it has a hardcoded value for your current version, so make sure to update it once you upload a newer version.


I do not claim that this tutorial is the best but it’s a lot simpler than many others and I would be happy if it helps someone. I tried to document every step, so no piece of the puzzle is missing. I skipped the commonly imported bintray configuration files and mentioned how you can easily keep your bintray key safe.

Comments

Popular posts from this blog

Create Diagonal Cut View in Android

HashMaps, ArrayMaps and SparseArrays in Android

Handling data change in RecyclerView gracefully using DiffUtil