AndroidPub
Published in

AndroidPub

DEPRECATED

Bintray just stopped accepting new packages from early 2021. Therefore, to host your Java/Library, you can use Maven Central (Sonatype) by following this post.

How to distribute Android library in a convenient way

If you are an Android developer, it is inevitable that your apps use some cool libraries because reinventing the wheel is unnecessary.
There are several ways to grab them and integrate into your project. You can add their .jar, .aar files or include them as submodules.

These above methods are OK but not convenient and not professional. Now, almost libraries are added by just declaring them in your build.gradle file.

compile 'com.android.support:appcompat-v7:25.1.0'

This sounds familiar to you, right?

If now you are building an awesome library as your personal project or your organization and provide to other developers like that way but still not figure it out, please go further in this article.

Firstly, what does the above line of code mean?

Since several years ago, Google has chosen Gradle as the default build system for Android apps in Android Studio, and Gradle supports something called Maven Repository Dependences. It works as a remote server which hosts Java and Android files such as .aar, .jar and so on.
The line compile ‘com.android.support:appcompat-v7:25.1.0’ means that “Hey Gradle, please go to Maven Repository to look for a library named com.android.support:appcompat-v7 version 25.1.0 and download it for me”.

So, you don’t have to search on internet for the library that you want, download it and integrate it into your project manually. Gradle takes care of this boring work.

So, how can other developers download your awesome library by declaring a similar line of code?

As mentioned above, Gradle finds the libraries from Maven remote servers and download them when building. So, it is so simple, you just need to push your library on these remote servers.
Two most popular Maven repositories are jCenter and Maven Central Repository. Google, however, prefers jCenter as the default remote repository because its simple and easy-to-use interface. If you look at your default project levelbuild.gradle file, you definitely see these lines:

repositories {
jcenter()
}

Although there are developers preferring Maven Central Repository and you can publish your libraries on both of them, we should keep everything simple at first. We are now focusing to publish our libraries on jCenter.
It only includes 3 simple steps:
- 1st: Create your account on JFrog Bintray.
- 2nd: Create your new repository.
- 3rd: Configure and publish it by Gradle plugin.

First: Create your account on JFrog Bintray

  • Go to their homepage and create your own account. https://bintray.com/
  • Edit your profile here: https://bintray.com/profile/edit
  • It is recommended that you should use GPG Signing key. It works as SSH keys when you use git on remote server, you don’t have to type your username and password over and over again.
    To complete this, you need to generate GPG keys by following these simple instructions. Also, please remember your passphrase because you will need it later.
    And then, copy and paste your public and private keys to the GPG tab in your profile edit page.

Second: Create your new repository

  • Go to your homepage and click Add New Repository button
  • Enter information into the form. Please, choose Maven as the repository type.

The repository name is also recommended to be “maven” because it is easy to group and manage your Java and Android repositories.
Now, you are going to add a new package which represents your Android library inside the repository.

Add a new package

Enter the required information for your new package.

Third Step: Configure your project and publish it by Gradle

  • In the project’s build.gradle file, please add two plugins (gradle-bintry-plugin and android-maven-gradle-plugin).
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// Two necessary plugins
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
}
}
  • Now, we need to configure our project to use these plugins.
    Bintray gives us a lot of options, here is the instruction.
    However, if this is the first time that you distribute your library on Bintray and jCenter, please read it later to avoid going into too specific details.
    So, we are going to use a simple configuration from these two files which is hosted in my Github repository.
    * install.gradle
    * bintray.gradle
    Also, just leave them here.
    In your project, please go to your local.properties and insert these lines.
bintray.user=your_username 
bintray.apikey=your_apikey //Go to your profile page on bintray homepage
bintray.gpg.password=your_gpg_passphrase //Remember when you generated gpg key.

These above properties are required to authenticate your account on Bintray.

  • Complete configuration by going to your library module’s build.gradle file and filling in the following information.
    (Please replace sample by your real data).
ext {

//For bintray and jcenter distribution
bintrayRepo = 'maven'
bintrayName = 'sample-library'

publishedGroupId = 'com.example.sample'
libraryName = 'SampleLibrary'
artifact = 'sample-library' //This artifact name should be the same with library module name

libraryDescription = 'Simple, clean and short description'

siteUrl = 'https://github.com/username/sample-library'
gitUrl = 'https://github.com/username/sample-library.git'

libraryVersion = '1.0.0'

developerId = 'your_id'
developerName = 'your_name'
developerEmail = 'your_email'
organization = 'your_organization' // if you push to organization's repository.
licenseName = 'The Apache Software License, Version 2.0' //Example for license
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]

}
//These two remote files contain Bintray configuration as described above.
apply from: 'https://raw.githubusercontent.com/quangctkm9207/template-files/master/android/gradle/install.gradle'
apply from: 'https://raw.githubusercontent.com/quangctkm9207/template-files/master/android/gradle/bintray.gradle'

Now, you can run a command to finish your work:

./gradlew bintrayUpload

If you publish successfully, you will see the version and dependence information on the package page.

Congratulation! You have just done an amazing job.

Now other developers can grab your library by declaring:

compile ‘com.yourdomain.yourpackage:yourlibray:1.0.0’

And

repositories{
maven {
url "http://dl.bintray.com/your_username/maven"
}
}

Why do they need the second one?

Because your library now has been uploaded in your own Bintray repository, not jCenter yet.

One more step required is to click the Add to jCenter button to send a request to the jCenter authorized team. They will review your library and it usually takes several hours.

Besides, please notice that jCenter requires not only .aar files but also your source code and java docs included. Therefore, for example, if you obfuscate your code by Proguard, uploading it to Bintray is the final step.

You have distributed your awesome library by a convenient and professional way which also makes other developer’s lives a little bit easier. Thank you!

I am going to write another article about iOS framework distribution.

If you are interested in my new blog posts and projects, you can subscribe to my newsletter by clicking the below link.

Subscribe to the latest blog posts and projects.

Or please get in touch with me via Github, Twitter, Facebook, or LinkedIn. Happy coding and have a good time!

--

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Recommended from Medium

Document Management Structure in Niche Industries

Laravel Rest API Passport Authentication for Ionic App

Instagram: Choose image from GDrive or local storage

NgAtlanta is Changing Tech

Anoma Research and Development Update: April 2022

Libraries and Using-for in Solidity

Automatically generating up-to-date database diagrams with TypeORM

PortSwigger: SQL injection attack, querying the database type and version on MySQL and Microsoft

PortSwigger: SQL injection attack, querying the database type and version on MySQL and Microsoft

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Quang Nguyen

Quang Nguyen

Software engineer and designer, specializing in native mobile apps, currently building digital scent technology at aromajoin.com.

More from Medium

Barista UI Testing(on top of Espresso)

Android Unit Testing —Choosing Naming Convention and Test Cases

Espresso UI testing made easy with kotlin extensions

Tabbed SlidingPaneLayout (Primary/Detail) using the Navigation Component Library ➡️🗔✨