You have just built an awesome Android library but you don’t know how to publish it? You are in the right place; in this article, I will explain to you the process that I use to publish my two libraries.
First of all, let’s assume that you have already developed your Android library, so you have a library module in Android Studio.
The library will be published to *jCenter, *a Maven repository hosted by bintray.com. The process is simple: the library is built locally in Android Studio, the artifacts will be upload to bintray and then linked to jCenter.
In this way, the developers that want to use your library has only to declare it in the *build.gradle *file of their projects. For example:
The first thing to do is to create a Bintray account and for open source projects is FREE.
After the creation of the account, you have to choose a repository. Let’s go with Maven (I know, you are thinking “Why? I want to publish it to jCenter”. Well, as said above jCenter is a Maven repository so we have to choose Maven).
Now you can create a new package inside your Maven repository.
After adding all the basic information of your library, like name, description, license, tags, etc., the repository of the library is ready.
Now, back to the code. I’ll take as reference, a library that I’ve developed, so you can dive into the real code if you want.
First of all, you have to add some dependencies into the build.gradle file of your project.
android-maven-gradle-plugin has been deprecated and you have to change the plugin to generate the artifact if you are using Gradle 5 and above. The new plugin is the one developed by Sky-UK and you need to apply it on the publish.gradle file (more details about it later on).
So we can get rid of the deprecated dependency:
At this point, we are ready to create the publishing script. Inside the library project, create a publish.gradle file and apply it on the *build.gradle file *of your library.
EDIT: Starting from Gradle 5 and above, the previous script is not valid anymore and you need to change it a little bit.
To better understand the changes, you can refer to the diff.
Before explaining the whole script, a clarification of the library signature is mandatory. As mentioned earlier, the signature of your library has the following shape:
and it is composed of three parts:
the Group ID (com.prof.rssparser);
the Artifact ID (rssparser);
the Version (2.0.4).
So, for the example library that we have created above, the Group ID is: “*com.your.awesome.lib*”, the Artifact ID is: “*awesomelib*” and the version is: “*1.0.0*”. These fields are arbitrary and you have to choose a name; keep in mind that with a new update, only the version will change in the signature.
Returning to the script, the lines [7–33] - (EDIT: [6–13]) - are responsible to build the library, generate the artifacts and link all the external dependencies that you are using.
The lines [38–58] - (EDIT: [15–38]) - instead are responsible for the uploading of the library. Here you have to specify your bintray username, bintray API key (you can retrieve and revoke it under Edit Your Profile -> API Key), the version, the artifact and the group of the library and other useful information. You can found all the different information that you can provide, in the documentation of the gradle bintray plugin.
Remember to keep private the username and the private key. There are many ways, for example, I saved them in the local.properties:
and loaded them in the *publish.gradle *script with a few simple lines of code.
Finally, it’s time to open the Terminal and launch the build and upload task (I recommend to use the *– info *flag to have a better understanding of what it is going on).
EDIT: After the deprecation of the
android-maven-gradle-plugin, the command to launch the build and upload task is changed
If everything went well, the artifacts have been uploaded to bintray but not yet published.
I have intentionally disabled the automatic publishing because it can save your life if there are some errors. But if you want, there is a flag that you can put in the publish.gradle file that automatically publishes the library as soon it has been uploaded.
To publish the library, you have to click the *Publish *button on a banner that appears in the repository page of the library.
And now the last step. You have to link the library to jCenter. This process must be done only the first time. At the right bottom of the library repository page, there is a “*Add to jCenter” *button.
Send the request and wait for the approval.
Within max two hours, your library is published and linked to jCenter, as you can see in the repository page.
Finally, you can tell the world about your awesome library and provide the signature to implement it.
And that’s all folks! I agree with you that the process is not simple but I hope to have cleared all your doubts.
Published also on Medium