Let’s Learn How to Upload Your First Android Library to Bintray and Jcenter.

Kaushik
The Startup
Published in
5 min readSep 23, 2020

If you are new to libraries and dependencies about the android project, you can learn more about it here: https://developer.android.com/studio/projects/android-library

Why do we need a library?

The libraries are mostly used for sharing codes among different projects for code reusability. Let’s take a small example here, consider your company has more than two mobile apps and is required to access the same database, API calls, and payment process, but the apps frequently have an issue and defect in API calls or database calls. The developer needs to handle both mobile apps code repository each time a new defect emerges and fix the defect; this wastes a lot of time in terms of productivity and resources. Imagine how easy it would be to fix such defects in one place instead of each different project?

It would be great for most organizations to maintain such code in one repository; this makes it easy to fix the defect in one place and consume such code as a remote repository for both projects. This can save resources and bring more productivity to the team.

Let’s set up a bintray account.

The first step we need to do for uploading our library is to create an account on Bintray at https://bintray.com/signup/oss. I already have an account, so I am skipping it. Once you create an account and log in, please click on “Add new repository” and fill in the information just like below according to your repository requirement.

You can see in the above screenshot I have created a new repository by selecting maven as a type and the default license as Apache-2.0; you can choose whatever license your organization thinks appropriate.

After creating a repository, let’s add the package inside the repository, which holds our library. Please click on the freshly created repository, and you will see an option says “Add new package” I have completed my package as below screenshot. I have also mentioned details beside each box by the arrow for what information to fill.

As you can see in the above screenshot, please fill out detail according to your library. The only last step we need to do on bintray is to retrieve your bintray API key for authentication when we are publishing our library to Bintray. To do that, please go into your Bintray profile and look for the API key like the below screenshot once you find it and copy it in any editor for later use.

Add Bintray set up inside Android Studio.

Let us come back to our android studio project, which we want to submit as a library. Now open your “local.properties” file inside Android Studio and add your API key and username of bintray, which we copied in the previous step. In the below screenshot, the left side“ local.properties file in Android studio, I have blurred my username and API key due to security purposes. Make sure you don’t push this “local.properties” file to GitHub while pushing your code to remote. It means to be for user-specific.

Open the project-level “build.gradle” file in the android studio as shown in the below screenshot and paste the below code in it. We have two Gradle files in our android project. Make sure you open the project level Gradle.

Now we will write the script to upload our build to the bintray and apply the build plugin into our build.gradle file.

Create a two new Gradle file with the name as “bintray.gradle” and “install.gradle” inside the app folder as shown in the below screenshot; make sure you choose your project view window should be a project.

As you can see in the above screenshot, we have two files inside the app directory with the name as “bintray.gradle” and “install.gradle” now open the “bintray.gradle” file and paste the below code inside it.

Now open your second Gradle file, which is “install.gradle,” which we previously created, and paste the below code.

All the tasks are done, and now we are ready to build and publish our library to bintray. The last step we need to do as a part of the setup is to apply both newly created gradle as a plugin inside our module build.gradle file, just like the below screenshot.

Run tasks for build and publish the library to Bintray.

From your android studio terminal, go to your root project and run the command as “gradle bintrayUpload” and wait for the execution, just like the below screenshot.

Now go to bintray, and inside your account, click on the repository. You will see your library published to your bintray maven repository as below screenshot. As you can see in the below screenshot, the “com.example.xfit” is published with version 1.0.2.

Add it to your project as a dependency.

The final step in the process is to submit your library to Jceter so you can consume it any project. To do so, we need to click on our package, and on the next screen, it will show you the option to add to Jcenter. Once you add to Jcenter, you have to wait for a couple of hours to get an approved library by Jcenter. Once it is approved, you can consume it any project by adding it to your build.gradle file.

--

--

Kaushik
The Startup

Enthusiastic Android developer, more interested in learning anything in android mobile, car, and watch.