Marcin Kitowicz
Written by Marcin Kitowicz
Android developer
Published September 14, 2016

Be Open Source and publish your library on Android platform

As software developers we have the possibility to share our code based on the Open Source principles. What are the main advantages? Other people have free access to our work, they can improve, expand and use your code in their own projects. This article shows, how to configure and publish your own library on Android platform.

Public Repositories

When it comes to our work, the project’s code is mainly stored in repositories. It’s a very convenient solution for both continuous development and cooperation with other users. But when we want to share the final version of our project’, it’s better to use distribution repositories such as: jcenter or mavenCentral. These two are very well known to Android Developers. They allow easy downloading and managing dependencies in their applications, for example:

Distributions servers store only generated archives of our projects without their source code.

Every distribution has to have a special name, for example: com.some:library:1.2.3 according to the following scheme: GROUP_ID:ARTIFACT_ID:VERSION we will name our library following this pattern.

Before setting up our project, we need to create an account on bintray.com. This service has a lot of plans at different prices, yet the open source software is completely free.

The next step is to generate and send the GPG keys to bintray.com. With this piece of information, the service will identify us as the authors of published libraries. We generate keys the following way:

All the questions related to keys creation can be answered with default values. The password should be difficult to guess. If we don’t have gpg in our system, we can install it with: brew install gpg (OSX) or even use the online generator. Once they keys are generated in the terminal we have to publish and export them.

To publish our public key we have to run the following command:

Here we obviously use our key’s ID. If we had generated keys by another tool, we could  also send public key using form on the site. The last step is to export public and private key:

If everything has worked out well, we can copy the keys to bintray.com (Your profile -> edit -> GPG Signing).

Library Project in Android Studio

When we’re done with the process of creating and generating keys, we can go straight to Android Studio and start working on our Library. It’s a good idea to divide our project into two modules: library and example how to use it. This layout lets us keep order. Here is the example of this structure.

Bez tytułu

You can create library module (timertextview) by choosing from the main menu: File -> New -> New module… -> Android Library. Our example of use (timertextview-sample) is a standard Android application. If you want to create a structure like this, you must modify the file: settings.gradle :

In this project we can distinguish several key configuration files:

This is the place to provide our group’s ID (it’s the first part of our unique name), current version, description and link to the code repository.

A separate module configuration allows independent distribution (we can also do the distribution for “example of usage”). POM_ARTIFACT_ID  is the second part of our distribution’s name, the third one is  the earlier given version.

This configuration file should be ignored in our repository (look .gitignore), because it contains confidential data. user  is our account’s name in bintray.com, apikey  comes from bintray profile (Your profile -> Edit -> API Key), gpg.password  is the password given when creating GPG keys.

rootProject/gradle/gradle-mvn-push.gradle

This file uses the official Bintray Gradle Plugin, which allows uploading the library we built to bintray.com. I recommend you copy and, if possible, modify it after reading the plugin documentation. As the last configuration step, add this file in the library’s build.gradle  as the last line:

Distribution publishing

If you’ve gotten this far, it means that you’re ready to publish your distribution. Sending the library to the server looks like this:

From this moment, our compiled code will be available to other users. The following dependencies configuration is necessary to use it :

Additionally, we can add our repository to jcenter . This eliminates the need to define the direct URL to our distribution. We can do this by visiting our published distribution: https://bintray.com/yourName/maven/yourLibrary  and clicking on “Add to JCenter”. After having our application accepted our (on average it takes about an hour) we will be able to replace the entire URL by jcenter ().

Initially, the whole setup process of distribution may seem complicated. Don’t be misled!  It’s totally worth your work, especially if you want to share the code in a professional manner.

Written by Marcin Kitowicz
Android developer
Published September 14, 2016