Develop & Publish your own SDK in Android - Part 3(Create gradle dependency)
Tags: gradle, android, sdk, jcenter
Published: 2019-02-17 15:18:47

---
title: Develop & Publish your own SDK in Android - Part 3(Create gradle dependency)
published: true
cover_image: https://thepracticaldev.s3.amazonaws.com/i/a3fcuizve1znyo0s8lxw.png
description: Create a gradle dependency of your Android library using Bintray JCenter. 
tags: gradle, android, SDK, jcenter
---


This is the 3rd and last part of *"Develop & Publish your own SDK in Android"* series. We have already learned what an AAR file is and how is it different from a JAR file. We have also learned how to create a new library project and obfuscate it. If you haven't read the last two parts, you can read them at-
- [PART 1 - Know an AAR file](https://dev.to/mohitrajput987/develop--publish-your-own-sdk-in-android---part-1know-an-aar-file-g9k)
- [PART 2 - Getting started with SDK development](https://dev.to/mohitrajput987/develop--publish-your-own-sdk-in-android---part-2getting-started-with-sdk-development-3159)


There are plenty of platforms for hosting your Android library and create its gradle or maven dependency. You can choose over JitPack or JCenter or Maven Central.
I prefer JCenter. The reason for choosing JCenter over others is explained here. 


# Why JCenter?

The advantages of choosing jCenter are, 
- It's free even for private GIT repositories. In fact, it doesn't matter whether your project is in GIT or not.
- Any new project in Android Studio by default includes jCenter, 
You must have seen these lines in root level build.gradle file in an Android Project-
```gradle
repositories {
    jcenter()
}
```

Developers using your library won’t have to add any additional configuration except gradle dependency with implementation line in app level build.gradle file.

- The second thing is, jCenter is the largest Java repository on the planet. It is the superset of maven central. It means, whatever available in maven central is already available in jCenter.
- It is pretty easy to upload your library to Bintray. No need to sign them or do any complicated things like you have to do on Maven Central.


So getting started for publishing your awesome Android library to JCenter.

# Create an account on JFrog Bintray
Since jCenter is hosted on JFrog Bintray, create a new account on it by clicking on [sign up](https://bintray.com/signup/oss).
You can sign up using GitHub, Twitter, Google or by directly entering your email ID and other details. Create an organization for which you are going to create the library. Once you finish sign up and create an organization, log in to your account and select organization, you will see the following dashboard-
![Dashboard](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/click-on-add-new-repo.png)
 
You will see organization name at the top, repositories(we will create it in the next step) and other details you have filled.

# Create a new repository
The next step is creating a repository. Click on **Add New Repository** button on the dashboard; you will see the following form-
![Add New Repository](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/create-new-repo-form.png)
 
You can provide any name for your repository, but the recommendation is “maven” as it describes that it is a maven repository for all your Android and Java libraries. Select **Maven** in type. Provide license, description and select avatar.

Once created, you will be redirected to the profile dashboard. You can see your new repository name in the Owned Repositories list section. Click on the repository title there, and you will be directed to the following repository page-
![Repository Page](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/click-on-add-new-package.png)


# Add a new package
The next step is creating a package. Package in the Bintray is created for every module project. If you have multiple modules in your project, you need to create separate packages for them. Click on the **Add New Package** button on the repository page, and you will get the following form-
![Add New Package](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/package-form.png)

The package name is generally your project’s package, i.e. `com.otb.utility`.
If you have multiple projects under the same group id, you can append your artifact id in package name for the ease of understanding i.e. `com.otb.utility:preference`.
If you have private GIT repo for the library project, you can provide the link of the public sample app GitHub repository. You can see the sample utility project [here](https://github.com/mohitrajput987/android-utility). Fill all the details and go to the package page. You will see the package page as follows-
![Package Dashboard](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/package-dashboard.png)


# Create a new version
Every time you create a new version of your library, you will need to create the version from Bintray dashboard. Click on **New Version** and you will see a small form as follows:
![Add Version](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/add-version-form.png)

Fill the version name i.e., 0.0.1 and its description and click on **Create Version**. After creating a version, you will be redirected to the package details page. Now go to the version page. You will see the following empty version page as your library is not published yet-
![Version Dashboard](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/version-dashboard.png)


# Get API Key
API Key is used for the authentication to upload your AAR file to the Bintray. Go to the Bintray dashboard and click on your name at top-right corner. You will see the following drop-down with profile related options.
Click on the **Manage Organizations** which will redirect you to the following edit profile page-
![Get API Key](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/get-api-key.png)
 
Click on the last option *API Key* from the left panel. Copy the API Key and keep it with you. You will need it while uploading the library.
We are done with the setup of Bintray.

*It's time to jump into the source code of library again.*

# Configure Bintray in the module
After the setup of Bintray package and version, you need to configure your library in Android Studio.
First of all, add the following plugins in root level build.gradle file in the dependencies section-

```gradle
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
```

Next step is to configure library details, i.e., group id, artifact id, license, etc. You also need to add Bintray credentials, package and version details. Add the following information to your library’s build.gradle file-

```gradle
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

ext {
    bintrayRepo = 'maven' // Name of the Bintray repo you created
    bintrayName = 'com.otb.utility:preference' // Bintray package name

    publishedGroupId = 'com.otb.utility' // Group ID of the SDK which will be added in implementation line of gradle
    artifact = 'preference' // Artifact ID of the SDK which will be added in implementation line of gradle
    libraryVersion = '0.0.1' // Version of the library you created in Bintray

    libraryName = 'Preference Utlity' // It is generally the module name
    libraryDescription = 'Utlity to use shared preference in your app.' // Small description of the library
 
    siteUrl = 'https://github.com/mohitrajput987/android-utility'
    gitUrl = 'https://github.com/mohitrajput987/android-utility.git'

    developerId = 'ourtechnobytes'
    developerName = 'Our Techno Bytes'
    developerEmail = 'info@ourtechnobytes.com'

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

    //Bintray Credentials
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())

    bintrayUsername = properties.getProperty('BINTRAY_USERNAME')
    bintrayUserOrg = properties.getProperty('BINTRAY_ORG')
    bintrayApiKey = properties.getProperty('BINTRAY_API_KEY')
}

android {
    ...
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    ...
}

apply from: 'bintray.script'
```


You can see the comments in ext{...} section for the explanation of those variables. You can access this file from [here](https://github.com/mohitrajput987/android-utility/blob/master/preference/build.gradle).
Look at the credentials part, I prefer saving credentials in either `local.properties` file or environment variable as saving credentials in the code is not secure.
Your `local.properties` file will look like this-

```
ndk.dir=/home/mrajput/Android/Sdk/ndk-bundle
sdk.dir=/home/mrajput/Android/Sdk
BINTRAY_USERNAME=your_bintray_username
BINTRAY_ORG=your_bintray_organization
BINTRAY_API_KEY=your_bintray_api_key
```

Now you can see the last line of the gradle file above. Here you need to apply path of Bintray script which will upload library files to the Bintray. You can download this script from [here](https://github.com/mohitrajput987/android-utility/blob/master/preference/bintray.script) and copy to the module’s directory.

# Compile the library module
After all configuration and credentials setup, it’s time to generate the AAR and other files. Open a terminal, go to your project’s root directory and run following command-

```
 gradlew install
```


If you see any javadoc related error, there is a small workaround for it(If you find any other solution for it, please share in comment). Add following line in root level build.gradle-

```gradle
subprojects {
   tasks.withType(Javadoc).all { enabled = false }
}
```
If you figure out any solution for this error, please post it comments section.

After that, run the install command again. The build generation process will be started and you will see the following message on the success-

```
BUILD SUCCESSFUL in 46s
27 actionable tasks: 17 executed, 10 up-to-date
```

# Uploading library to Bintray
After a successful build, it's time to upload your library to Bintray. We can do it by running following command in the same terminal-

```
 gradlew bintrayUpload
```


After a successful upload, you will see a similar `BUILD SUCCESSFUL` like message as mentioned above.

Now go to the version page on Bintray and you will see your library details i.e. maven and gradle dependency settings there. 

# Add library to jCenter
If you add a gradle dependency that you just created in any other project, you will get an error like “failed to resolve com.yourlibraryname...”. It’s because your package is added to Bintray repo only, not in jCenter yet. 
To add the package in jCenter, click on the **Add to jCenter** button as shown in the bottom-right in below image-
![Add to JCenter](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/click-on-add-to-jcenter.png)


You will be redirected to the following form which will require small detail about your package-
![Add Package info](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/fill-jcenter-detail.png)

Write a brief about your library and submit the request. After submission, jCenter team reviews and approves the package which takes around **2-3 hours to 2-3 days**. When they approve, you will get a notification on Bintray.  Add to jCenter button will not be there and you will see following jCenter badge-
![](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/added-to-jcenter.png)


If your package is not added to the jCenter and still you want to integrate your library, you can do it by explicitly specifying your repository URL. For that, click on the blue **SET ME UP!*** button and then click on **Resolving artifacts using Gradle** as shown below-
![Set me up](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/bintray/set-me-up.png)

Copy this maven URL and add in repositories section.


*Congratulations! Your library is ready to use.*

# Conclusion
It seems like it is a very long process to create a gradle dependency. But don't worry, most of the steps are one time only. We have learned why to choose jCenter over other platforms. We have seen how to create an account and set up a package in the Bintray. Finally, have added some configurations in the code and published the library with the use of two commands. I hope you will love this article and it will help you with the SDK development. If you have any doubt, you can ask in the comments section. Also, share the link of your library in the comments. See you soon in the next article.