How To Create And publish An Android Library

Posted By : Daljeet Singh | 09-Jan-2018

An android library is quite useful when we have to use the same functionality or UI across multiple apps.It saves us the trouble of writing a lot of boiler plate code over and over again.This blog explains the process of creating and uploading an android library to jCenter.

 

Creating a library in android studio

  • Open an existing project or create a new one.

  • Go to File->New->New Module

  • Select Android Library in the new module window and click next.You can also create a Java Library instead of an android library which would output a .jar file only containing java classes.The output of an android library would be an .aar file which contains android resources such as drawables,layouts and manifest along with java classes.

  • Input a suitable name for your library and select the minimum sdk level for the library and click finish.

  • After the completion of gradle sync,you can start writing code in your library.

  • Once you are done writing code for your library,you can generate an .aar file for the library by building the project(the output .aar file is usually found in the builds->outputs->aar directory of your library) ,which can later be imported by other apps wanting to use your library's functionality.

Uploading your library to jCenter

You might want to make it easier for other people to add your library to their projects by using a single line dependency in their app module like :

compile 'com.github.bumptech.glide:glide:3.7.0'

In order to make this possible,you need to add your library to a repository server such as jcenter or maven central. Here are the steps required to upload your library to jcenter:

  • Create an account on bintray.com,you can sign up using either your email address or your github,google or twitter account.

  • Create a repository after signing up by using the Add a new repository option.

  • Specify a suitable name for your repository and choose Maven as the Type.

  • Add a new package in the repository.By convention,try to keep the name of the package the same as your library name.Choose any license from the list given,specify your library's github url in the version control box.

  • Now specify your bintray apikey and username in your android library project.It is wise to specify these values in the local.properties of your project as this is usually ignored by git and hence you prevent exposing your key to anyone.You can find your bintray apikey by clicking on the edit tab below your username.

  • Add your bintray username and apikey in the local.properties as:

    bintray.user=your_username
    bintray.apikey=your_api_key
    
    avoid declaring your values inside quotes(“”) as this can result in “401 Unauthorized” error while uploading the library to bintray.
  • Define the following classpath in the build.gradle of your project:

    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
    
  • Add the following properties to your library's build.gradle just below the apply plugin:'com.android.library' line:

    ext {
        bintrayRepo =  'your bintray repo name'
        bintrayName = 'bintray package name which is same as your library module name'
    
        publishedGroupId = 'your module package name'
        libraryName = 'your library module name'
        artifact = 'your library module name' 
    
        siteUrl = 'provide github url if you dont have the url to a site'
        gitUrl = 'github url'
    
        libraryDescription = 'short library description'
        libraryVersion = 'version number(can be kept 1.0)'
    
        developerId ='your bintray id'
        developerName='your name'
        developerEmail='your email address'
    
        licenseName = 'The Apache Software License, Version 2.0(selected at the time of creating the package)'
        licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
        allLicenses = ["Apache-2.0"]
    }
    
  • Now,add the following lines at the end of your library build.gradle file,below the dependencies:

    apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
    apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
    
    these lines are used to build the library files and after building them uploads them to bintray.
  • After syncing your project, switch to the root of your project in the terminal,and run the following line:

  •  
  • ./gradlew clean build install bintrayUpload
    
    If all goes well,you will receive a build successful message in the terminal.If you receive an “Unsupported major.minor version 52.0” error,please make sure you are using JDK 8.
  • After the successful building of the project,the library should now be visible inside your bintray package with its version mentioned.

  • Click on the Add to jCenter button to request the addition of the library to the jCenter repository.If your request is accepted,you'll see jCenter in the Linked to section of your package within a few hours.

  • Now your library can be added to android projects by using single line dependency in the app's build.gradle like:

    compile 'publishedGroupId.artifact:libraryversion'
    

 

 

 

About Author

Author Image
Daljeet Singh

Daljeet has experience developing android applications across a range of domains such as Cryptocurrency, Travel & Hotel Booking, Video Streaming and e-commerce. In his free time, he can be found playing/watching a game of football or reading up on either

Request for Proposal

Name is required

Comment is required

Sending message..