How to create and publish your first Android library

Sooner or later every Android developer accumulates a fair amount of ready-made solutions and decides that it would be great to share them with the rest of the world. Unfortunately, most of us never get down to it since every solution should be turned into a separate library and put into a separate open repository. Plus, some tend to think that it’s all too complicated to even bother. Here I’d like to bust this myth since with Gradle coming into Android stack sharing your library with the world is much easier than it used to be.

Using BottomTabLayout

Of course, the first step is to write your own library. In this example I will be using BottomTabLayout. This library allows you to add a bottom TabLayout that came from the iOS realm.

BottomTabLayout in action

1. A set of tabs is specified using XML file of menu resources. For example:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="">
        android:title="Button1" />
        android:title="Button2" />
        android:title="Button3" />

2. In the code above icon property is a selector resource we create in drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
    <item android:drawable="@drawable/ic_button1_dark" android:state_pressed="true" /> <!-- pressed -->
    <item android:drawable="@drawable/ic_button1_dark" android:state_selected="true" /> <!-- selected -->
    <item android:drawable="@drawable/ic_button1" /> <!-- default -->

state_selected and default states are mandatory here since we need to highlight a selected tab.

3. Let’s create a selector for text:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
    <item android:color="@color/black" android:state_pressed="true" /> <!-- pressed -->
    <item android:color="@color/black" android:state_selected="true" /> <!-- selected -->
    <item android:color="@color/white" /> <!-- default -->

4. Now create a style for text:

<style name="TabButtonTextStyle" parent="android:Widget.Button">
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">@drawable/tab_button_text_selector</item>

5. Add BottomTabLayout to Activity markup:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
        android:layout_above="@+id/bottomTabLayout" />
        android:background="@color/dark" />

6. Set activity in class::

    protected void onCreate(Bundle savedInstanceState) {
        container =; //Container for fragments
        //Setup button tab layout
        bottomTabLayout = (BottomTabLayout) findViewById(;
        //set button text style
        // set buttons from menu resource
        //set on selected tab listener.
        bottomTabLayout.setListener(new BottomTabLayout.OnItemSelectedListener() {
            public void onItemSelected(int id) {
        //set button that will be select on start activity

7. In our example when the tab is switched a fragment in the container changes:

     * Show fragment in container
     * @param id Menu item res id
    public void switchFragment(int id) {
        Fragment fragment = null;
        switch (id) {
                fragment = ColoredFragment.newInstance(, "Fragment 1");
                fragment = ColoredFragment.newInstance(, "Fragment 2");
                fragment = ColoredFragment.newInstance(, "Fragment 3");
                fragment = ColoredFragment.newInstance(R.color.blueDark, "Fragment 4");
                fragment = ColoredFragment.newInstance(R.color.white, "Fragment 5");
        if (fragment != null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(container, fragment);

We’re done, now it’s time to publish the library. Visit our GitHub to take a closer look at BottomTabLayout.

Publishing your Android library

Now I will tell you how having something similar you can share it with the world. We’ll start with creating a module, then upload it to GitHub and make public using Bintray.

Creating a module in Android Studio

1. Create a project in Android Studio. For your convenience rename app module into sample. It will contain your library usage example.

2. Create a new module:

Creating new module in Android Studio

3. Choose Android Library type:

Choosing module type in Android Studio

4. Enter a name — I will be using bottomtablayout.

5. Now settings.gradle should look like this:

include ':sample', ':bottomtablayout'

6. In the sample module add a dependency into build.gradle:

compile project(':bottomtablayout')

It is a temporary solution since currently our library is local.

7. Now you can write your own library (or move an existing code into it) and test it in a sample module.

8. When your library is ready, upload it to your GitHub account.

9. Write a tutorial.

Adding library to Bintray

1. If the library is ready, it’s time to show it to the whole world! Get yourself a Bintray account.

2. Add a dependency into a build.gradle file of the project:

dependencies {
   classpath 'com.novoda:bintray-release:0.3.4'

3. In build.gradle file of library module (bottomtablayout in our case) apply a plugin:

apply plugin: 'com.novoda.bintray-release'

In the same file:

publish {
   groupId = 'com.github.stfalcon'
   artifactId = 'contentmanager'
   publishVersion = '0.1.1'
   desc = 'Library that helps a few lines of code to get pictures and video from the android file system, or ures cloud, as well as the camera device.'
   licences = ['Apache-2.0']
   website = ''


  • groupId — a package (can be different from the package of the module). I recommend to name groupId as follows: com.github.<username of your github account> since when you log in to upload your library to Maven Central you’ve got to be the owner of the domain used in the name groupId.
  • artifactId — module name.
  • publishVersion — version of publication.
  • desc — library description.
  • licences — licence.
  • uploadName — library name.
  • website — link to project GitHub.

4. Now you need an API Key of your Bintray account. Go to your Bintray profile page:

Bintray profile

5. Find Edit button and click it:

Editing Bintray profile

6. Now go to API Key:

Getting API Key from Bintray

7. Put your key into a safe place.

8. Open Android Studio terminal and run:

gradlew clean build bintrayUpload -PbintrayUser=<юзернейм на bintray> -PbintrayKey=<api ключ с bintray> -PdryRun=false

9. Wait for library to be assembled and uploaded to server.

10. Now go to Bintray website and find a page of your newly added library.

11. Currently if you want to use a library you’ve got to add a link to the repo into build.gradle project file:

repositories {
    maven {
        url  "<username>/maven" 

But can we simplify the last step somehow? Sure, we can!

Adding Android library to jCenter

Recently jCenter became a default Android repository (previously Maven Central was used). Still, adding your library into jCenter is very simple.

1. On your library page click add to jCenter and send a query for adding your library. Within 24 hours your library will be added to jCenter and you will get an email notification.

2. Now you can connect your library via build.gradle of the module by adding a respective dependency:

compile 'com.github.stfalcon:contentmanager:0.1.1'


Congratulations! Now your library is out and you’re ready to show it to your friends, share with the development community and watch it earn you GitHub stars and forks.

UPD: Check out a new article on our Android libraries!

Need MVP development, iOS and Android apps or prototyping? Check out our portfolio and make an order today!

About author

Android Developer
Anton used to work in studio game dev team and currently he is developing apps for Android. He is meticulous, enjoys implementing solutions that speed up the development and improve code quality. Uses Java, DataBinding, Retrofit 2.0 and Realm.

Related posts

Return to list Return to list