This page looks best with JavaScript enabled

Guide to publishing your Android Library via Jcenter/Bintray

 ·  β˜• 6 min read  ·  ✍️ Nishant

NOTE: Jcenter has been sunset. It is encouraged to publish to MavenCentral.
You checkout the other guide: Guide to publishing your Android Library via MavenCentral

Developers are a different kind of people altogether. They tend to be lazy but strive to be super efficient at the same time.
A lot of this can be seen in the Android world where a certain library pops up everyday to solve a specific problem or to make the complex processes simpler.

I have a certain knack for re-using code blocks just to avoid repeative tasks and to facilitate this I usually end up converting those codeblocks into an android library.

But what if I wanted to share my android library with the world?

Well in a nutshell the steps to follow would be as below :

  • First of all I need to open source the andorid library, which should be easy as you can push it to Github or any other public git repository.

  • Next I need to push the android library as a maven artifact (aar/jar with a POM) to all of the or one of the below central repositories

We will walkthrough the process of publishing to each if these central repositories in the upcoming posts of this series.

For now lets lookup the steps to publish your android library to JCenter/Bintray.

Creating your Android “Awesome” Library

You can skip to the next part if you already have this built

  • Create an Android project or open an existing one in Android Studio

  • Init the project with git and also create a repo on Github for the same. Each step here onwards represent a commit and should be pushed to github.

  • Create and add a new module and choose Android Library.

    Goto File>New>New Module.. and select Android Library.

newmodule

newlib

newlibinfo

  • Implement your library code inside the library module you created in the last step.

  • Next add the library module as a dependency to the app module.

    1. Goto File>Project Structure..
    2. Select app module in the sidebar
    3. Select the Dependencies tab
    4. At the bottom is a + icon, click that and select Module dependency and select your library module.
    5. Press apply or ok.

project

prjstruct

addmodule

Publishing your Android “Awesome” Library
  • Once project is synced, add the required plugins to classpath in build.gradle file at root project level, as shown below
1
2
3
4
5
6
7
8
9
 dependencies {
    classpath 'com.android.tools.build:gradle:2.1.3'
    ..
    ..
    // Required plugins added to classpath to facilitate pushing to Jcenter/Bintray
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
    ..
   }
  • Next, apply the bintray and install plugins at the bottom of build.gradle file at library module level. Also add the ext variable with required information as shown below
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 apply plugin: 'com.android.library'

 ext {
   bintrayRepo = 'maven'
   bintrayName = 'awesomelib'   // Has to be same as your library module name

   publishedGroupId = 'com.github.nisrulz'
   libraryName = 'AwesomeLib'
   artifact = 'awesomelib'     // Has to be same as your library module name

   libraryDescription = 'Android Library to make any text into Toast with Awesome prepended to the text'

   // Your github repo link
   siteUrl = 'https://github.com/nisrulz/UploadToBintray'
   gitUrl = 'https://github.com/nisrulz/UploadToBintray.git'
   githubRepository= 'nisrulz/UploadToBintray'

   libraryVersion = '1.0'

   developerId = 'nisrulz'
   developerName = 'Nishant Srivastava'
   developerEmail = '[email protected]'

   licenseName = 'The Apache Software License, Version 2.0'
   licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
   allLicenses = ["Apache-2.0"]
 }

 ..
 ..

 // Place it at the end of the file
 apply from: 'https://raw.githubusercontent.com/nisrulz/JCenter/master/installv1.gradle'
 apply from: 'https://raw.githubusercontent.com/nisrulz/JCenter/master/bintrayv1.gradle'
  • Edit your local.properties
bintray.user=<your_bintray_username>
bintray.apikey=<your_bintray_apikey>

NOTE : bintray.user and bintray.apikey have to be in local.properties specifically or else you will get error later regarding the user and apikey values not available to the bintrayUpload gradle task as below

No value has been specified for property 'apiKey'.
No value has been specified for property 'user'.
  • Now lets setup Bintray before we can push our artifact to it.

    • Register for an account on bintray.com and click the activation email they send you.

    • Add a new Maven repository and click Create New Package

    • You should now have a maven repository. For instance:
      https://bintray.com/nisrulz/maven

    • Now once you have your maven repo setup , click on Edit

      ![edit](/images/posts/uploadtojcenter/edit.jpeg)
      

      and see that you have selected the option GPG sign uploaded files using Bintray's public/private key pair. and then click Update.

      gpg

  • Once everything is configured, run the below in your terminal in your root of the project

1
./gradlew clean build install bintrayUpload
  • Now once your project is up on bintray, simply hit Add to Jcenter button to sync with JCenter.

    addtojcenter

Using your Android “Awesome” Library in other projects
  • Your code is available through the private repo at bintray
1
2
3
4
5
6
7
repositories {
   jcenter()
   maven { url 'https://dl.bintray.com/<bintray_username>/maven' }
}
dependencies {
  implementation 'com.github.<bintray_username>:<library_module>:1.0'
}

i.e for the sample lib in this repo , awesomelib

1
2
3
4
5
6
7
repositories {
   jcenter()
   maven { url 'https://dl.bintray.com/nisrulz/maven' }
}
dependencies {
  implementation 'com.github.nisrulz:awesomelib:1.0'
}
  • Your code is available through JCenter if you have received the mail with confirmation

    finalmail

Few things to note when you received the final email.

  • Goto your maven repo at bintray and verify that you have Jcenter under the Linked to section

    linked

  • Now you would also want to sync the artifact to MavenCentral, for that you need to hit the Maven Central tab and sync

    synctomaven

  • Once you hit sync you would see as below. Wait for few hours for the sync to occur.

    syncstatus

You can use the lib now as follows

1
2
3
dependencies {
    implementation 'com.github.<bintray_username>:<library_module>:1.0'
  }

i.e for the sample lib in this repo , awesomelib

1
2
3
dependencies {
      implementation 'com.github.nisrulz:awesomelib:1.0'
  }
  • Let the world know of your AwesomeLib

    • Add a readme that explains how to integrate and use your Awesome library
    • Add a license block as in this repo
    • Also include the Bintray badge provided by Bintray in your readme
      badge
    • Promote your lib on social media so that others can know about it.
    • Always add a working sample app in your project that demonstrates your library in use.
    • Add screenshots if possible in your readme.

The code for the AwesomeLibrary and this guide itself is open sourced and available on github

The bintray and install plugins are also available on github. You may fork it and use it in your namespace.

Star it or just fork it to use it.

This post is first in parts of a series

  1. Guide to publishing your Android Library via Jcenter/Bintray
  2. Guide to publishing your Android Library via MavenCentral
  3. Guide to publishing your Android Library via JitPack
Share on
Support the author with

Nishant Srivastava
WRITTEN BY
Nishant
πŸ‘¨β€πŸ’» Android Engineer/🧒 Opensource enthusiast

What's on this Page