Products Solutions Contact TR

Creating an Android Library and Publishing with Bintray – For Beginners

 

A software library is a package that contains codes that developers have written before, classes they have prepared, procedures, scripts, configuration data and more. It is a structure used to simplify or facilitate repetitive operations.

 

How can we create a library for Android?

 

1) Creating a Library Module

 

Go to File -> New -> New Module in Android Studio

 
New Module Screen
 

We select “Android Library” option and click “Next”.

 
Configure the New Module Screen
 

We enter the library, module, and package name. We choose the minimum supported Android version and click “Finish”.

 

2) Writing the Codes to be Used

 
Project File Tree
 

A tree structure as above will be formed. The modules in the project work separately. There are 3 gradle files in the tree structure as you see above.

 

Project: SDKExample file contains Gradle settings for the project.

Module: apilibrary holds Gradle settings for our library.

Module: app contains Gradle settings of the Android project.

 

Module files are kept separately. Library files should be located below the module name we entered while creating the project. We will create operations, classes, layout files or any extra data in this module

    package poc.yetisir.com.apilibrary; import android.util.Log; public class Api { public Api() { Log.d("Library", "This log was sent from inside the library"); } }

 

As you see in the “Api.java” class above, we write the codes and classes for the operations we want the library to perform.

 

3) Adding the Library to the Local Project

 

Because the modules work separately, we need to add the library to the Android Project Module as a dependency so that the local project can see and use our library.

Go to File -> Project Structure in Android Studio

 
Project Dependencies
 

We select the “app” module on the left and go to the “Dependencies” tab. Then we click on the “+” button on the above left, and we select the “Module dependency” option.

 
Choose Modules
 

In the next screen, we select the library module and click “OK”.

Another way to add module is by adding it to the “settings.gradle” file as below:

 
include ':app', ':apilibrary'
 

Then we add the library module into the “build.gradle(Module: app)” file as below:

 
implementation project(':apilibrary')
 

Now we can use the library we added as in the sample code.

 
Api api = new Api();
 

If we want to use the library we created in other local projects, we run the library module with gradle operations on the right, using the option librarymodulename -> Tasks -> build ->assembleRelease.

 

After gradle completes the operation, the .aar file will be created under librarymodulename -> build -> outputs -> aar as in the tree structure on the left.

 
 

Then we copy that library file with the extension .aar to the libs folder in the Android project we want to use, and add below code lines to the Module: app gradle file.

 
repositories {
flatDir {
   dirs 'libs'
}
 

We can start using our library by adding the following code to the same gradle file according to our library settings.

 
compile (name: 'apilibrary-release', ext:'aar')
 

Another way to use our library in other local projects is to use the Maven Local Repository. In this method, we add the Android Maven plugin to the “build.gradle(Module: apilibrary)” file as below:

 
apply plugin: 'android-maven'
 

Afterward, thanks to this plugin, we run the “install “command under our library module in Gradle tab to add the library we created in Maven Local Repository.

 
 

Or we can run the “clean build install” command after selecting our library module with Execute Gradle Task operation, as shown in the below image.

 
 

In this way, we have successfully added our library to Maven Local Repository. Now, let’s use this library in the repository in other projects.

 

First of all, to use Maven Local Repository in the project that we want to add the library, we add the mavenLocal() code to the gradle file.

 
repositories {
    mavenLocal()
}
 

The project can now use the libraries in the Maven Local Repository. We include the library in the project according to the group, artifact, and version, as in the example.

 
implementation 'com.yetisir.sdkexample:apilibrary:1.0.0'
 

And now our library is ready for use.

 

4) Creating a Bintray Account

Bintray is a distribution platform that enables us to work seamlessly with industry-standard development, configuration, and deployment tools, and supports all major package formats.

 

Maven is a build tool. Maven Repository provides a service that enables global access to the libraries. We need to add the GroupID (indicates the organization), the ArtifactID (indicates the library), and the Version (indicates the library version) as a dependency to the Maven or Gradle project, to use the libraries.

 

We will also upload the library we have created for global use in the Maven Repository. If we have new versions, we will release them with this Maven Repository.

 

Firstly, we go to bintray.com, register, and log in. Bintray has 30 days free trial period.

 

Now we will create a Maven Repository, using Bintray platform.

We select the“Add New Repository” option.

 
Bintray Repositories
 

We enter the required information and select “Maven” for type option. Then we go to the repository detail page.

 
Bintray New Repository
 

We click on“Add New Package”.

 
 

We enter the required information. (I recommend that you enter here the name of the library package you created in Android Studio.)

 
   

We create a new version by clicking the “New Version” button on the right side in the package.

 

 

5) Publishing the Library

 

We go to our Bintray Profile page, and we copy the key in the “API Key” section.

 
 

We add our information in the “build.gradle(Module: libraryname)” file as below:

 
  • group: the group name of the library we will publish
  • version: the version of the library we will publish
  • user: our Bintray Username
  • key: our Bintray API key
  • repo: the name of the repository where we will publish the library
  • name: the name of the package where we will publish the library
  • vcsUrl: URL for the version control repository of our project
 
apply plugin: 'com.android.library'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'
group = 'com.yetisir.sdkexample'
version = '1.0.0'
//Below section is for SDK publification using Bintray
bintray {
user = 'mahmutyetisir03'
key = '************************************'
pkg {
repo = 'sdk-example'
name = 'com.yetisir.sdkexample'
userOrg = 'yetisir'
version {
name = '1.0.0'
desc = 'My test upload'
released  = new Date()
vcsTag = '1.0.0'
}
licenses = ['Apache-2.0']
vcsUrl = 'https://github.com/Dilolokko/android-sdk-example.git'
websiteUrl = 'https://github.com/Dilolokko/android-sdk-example'
}
configurations = ['archives']
}
 

Then, we open the “Gradle” tab on the right-hand side and execute the “bintrayUpload” function.  With this, we upload our library to our Bintray account. In about an hour, we will have published our library.

 
 

But, why are we uploading a project we built with Gradle on Maven Repository?

 

Gradle and Maven are similar tools (both are build tools). While Gradle has no repository section, Maven can work well with library structure. So, we use Maven Repository as a distribution tool and Gradle as a build tool.

 

6) Using the Library in Other Projects

 

We add “maven url” in the “Project Gradle” file to access our Bintray repository.

 
allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url  "https://yetisir.bintray.com/sdk-example"
        }
    }
}
 

We add the library to the project as below:

 
implementation 'com.yetisir.sdkexample:apilibrary:1.0.0'
 

Now we can use the library we have published on Bintray, in any project.

You can access the sample library project codes from the GitHub link below:

   

Author: Mahmut Yetişir

Publish Date: Feb 8, 2019    
%d bloggers like this: