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.

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

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

A tree structure as above is formed. The modules in the project work separately. There are three 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 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", "Bu log kütüphanenin içinden gönderildi"); } }
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.

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.

On the next screen, we select the library module and click “OK”.
Another way to add a 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();
Suppose we want to use the library we created in other local projects. In that case, 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 is 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 the 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: 'com.android.library' apply plugin: 'com.jfrog.bintray' apply plugin: 'com.github.dcendents.android-maven' group = 'com.yetisir.sdkexample' version = '1.0.0' //Aşağıda ki kısım bintray kullanılarak SDK publification işlemi yapmak için 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'] }
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 also upload the library we have created for global use in the Maven Repository. If we have new versions, we 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 create a Maven Repository using the Bintray platform.
We select the“Add New Repository” option.

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

We click on“Add New Package”.

We enter the required information. (I recommend that you enter 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 of 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 publish
- version: the version of the library we publish
- user: our Bintray Username
- key: our Bintray API key
- repo: the name of the repository where we publish the library
- name: the name of the package where we 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 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:
Date Published: Feb 8, 2019