Android: Author Library for Distribution

As a developer, you don’t want to restrict your codes only within your project. It would be awesome to modularize your own invention and distribute to the world so that the industry won’t need to reinvent the wheels, which you believe that you already have built the best.


STEP 1: username and API key

Register on bintray.com, and get your username and API key.

bintray.com

Store the 2 values in your Android project’s local.properties file.

Note: local.properties should not be submitted to GitHub.

local.properties (do NOT submit to GitHub)

STEP 2: Create a Module in Project

Create an Android project named “AndroidAmazingView”.

Then create a new module of type Android Library named “mylibrary”.

don’t worry about the “amazingviewlib”, which is another module I played with

STEP 3: Write Module Source Codes

Now you start building a customized view in module “mylibrary”.

com/chensuworks/mylibrary/ButtonPair.java
res/layout/button_pair.xml

STEP 4: Reference Module from Local Project

So far, the custom view has been created and is ready for use locally.

If the local project “AndroidAmazingView” wants to use “mylibrary”, edit in app level build.gradle.

compile project(“:mylibrary”)
View view = new ButtonPair(this);

STEP 5: Prepare for Distribution

Make sure in bintray you have a default maven repository, and it’s empty.

bintray.com
bintray.com

In project level (top level) build.gradle, add classpath for bintray and github plugins.

In “mylibrary” module level build.gradle, make the following modifications.


STEP 6: Install

In “Gradle projects” window, run the task :mylibrary/Tasks/other/install.

:mylibrary/Tasks/other/install

Double click the “install” task, and make sure the build succeeds.


STEP 7: Upload

Then run task :mylibrary/Tasks/publishing/bintrayUpload.

Make sure the task succeeds.

In bintray’s repository, the new package should show up.


STEP 8: Reference Module in My Maven Repository from Anywhere

Since the “mylibrary” module has been published to bintray, and we could reference it in whatever project we like.

Let’s create a new Android project.

First thing is to point to my maven repository hosted on bintray.

https://dl.bintray.com/martinsuchen35/maven

In project level (top level) build.gradle, point to the URL we get from bintray.

url ‘https://dl.bintray.com/martinsuchen35/maven’

In app level build.gradle, add “mylibrary” as a dependency.

compile ‘com.chensuworks.mylibrary:mylibrary:0.0.1’

Now you can happily use the view from “mylibrary” in this new project.

View view = new ButtonPair(this);

STEP 9: Add to JCenter

In the previous step, you’re referring to “mylibrary” from my Maven repository. Next step is to promote “mylibrary” to jcenter(), which is the default place where Android Studio searches for libraries.

In bintray’s package page, click “Add to JCenter”.

Write the message and then “Send”

STEP 10: Wait for Approval

“mylibrary” has been approved by bintray about 3 hours after request submission.

Go to bintray’s console and there is a new jcenter link created!

HOORAY! Now the world could use this super cool library by directly adding it in app level build.gradle.

compile ‘com.chensuworks.mylibrary:mylibrary:0.0.1’