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.

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.