Distributing Android Libraries via jCenter for Gradle Importing

This is really easy! Let's walk through what it takes to publish libraries so that other developers can import them this way. Expect this to take 1-2 hours your first time. If you get stuck, it may help to look at project I wrote for this tutorial.

Background

When you add the above line to your build.gradle, Android Studio downloads the library from a Maven Repository Server. Apache Maven is a tool that provides a file server for distributing libraries. In this tutorial, I am going to focus on the JCenter repository server, since it is the default one used by Android Studio. You can setup additional repositories to search through by adding to the repositories section in your project's build.gradle, but you rarely need to.

The files downloaded by gradle are nothing more than regular .jar or .aar files.

Part 1: Setup Bintray Account

Bintray is the website that owns the JCenter Maven repository. So you are going to need a Bintray account. Fortunately, it is free.

First create an account. After you are done, view the precreated Maven repository in the Owned Repositories section.

Next, create a new package.

The name of the package you create won't have any bearing on the way that other developers import your library through gradle. When filling out the values for Website, Issue Tracker, and Version Control you may eventually wish to use the URL for a github project. Since these values can be changed later, you can use any URL for now.

Part 2: Android Studio Project

You can use Android Studio for both developing and uploading libraries to Bintray. After you've created a new Android Studio project, add a Library Module.

You now have an app and library module inside your Android Studio project. You don't need the app module in the rest of this tutorial. But, you may wish to keep it in order to test your library.

Next, apply Bintray's plugin to your project. To do this, you need to update your project's build.gradle's dependencies section. Be careful not to add this to a module's build.gradle file instead.

In order to authenticate your library with Bintray, you need to append your Bintray credentials to local.properties. Append the following lines.

bintray.user=(BINTRAY_USERNAME)
bintray.apikey=(BINTRAY_API_KEY)

In order to generate the API key needed above, visit Edit Profile in Bintray. If you are publishing your source code, this local.properties file should be added to your .gitignore so you don't accidentally publish your password.

Next, add the following to your library module's build.gradle file and update them as appropriate. This allows you to configure the name and metadata that other developers will see for your library.

ext {
// Where you will see your artifact in Bintray's web interface
// The "bintrayName" should match the name of the Bintray repro.
bintrayRepo = 'maven'
bintrayName = 'rating-bar'
// Maven metadata
publishedGroupId = 'com.brianattwell.rating_bar'
libraryName = 'RatingBar'
// Save yourself a head ache, and set this equal to the name of the Android Studio library
// module. The artifact name needs to match the name of the library.
artifact = 'ratingbar'
libraryDescription = 'A custom RatingBar view that measures its indeterminate drawable, in order to avoid the RatingBar "bleeding problem"'
libraryVersion = '0.3.1'
developerId = 'attwellbrian'
developerName = 'Brian Attwell'
developerEmail = 'brian@brianattwell.com'
}

With the above configuration, people will be able to include your library using the following inside their build.gradle.

compile '(publishedGroupId):(artifact):(libraryVersion)'

Finally, you need to append the following lines to the bottom of your library module's build.gradle. This includes scripts, forked from @nuuneoi's scripts, that builds your library files and uploads them to Bintray.

Part 3: Upload library to Bintray

You are now ready to upload your library to your Bintray repository. First, check the correctness of your library.

> ./gradlew install

If the above completed successfully, you can now upload to Bintray.

> ./gradlew bintrayUpload

Great! Your library can now be used by anyone! But your library is still inside your own Maven repository instead of jCenter. If someone wanted to use it, they would need to add the following code to their app's build.gradle.