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
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 level
build.gradle file, you definitely see these lines:
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 Repositorybutton
- 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.
Enter the required information for your new package.
Third Step: Configure your project and publish it by Gradle
- In the project’s
build.gradlefile, please add two plugins (gradle-bintry-plugin and android-maven-gradle-plugin).
classpath 'com.android.tools.build:gradle:2.2.3' // Two necessary plugins
- 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.
Also, just leave them here.
In your project, please go to your
local.propertiesand insert these lines.
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.gradlefile and filling in the following information.
(Please replace sample by your real data).
//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:
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:
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.