Back to top
From Zero to Open Source Hero: Episode 3
Creating an Android Library Project

Hello again and welcome back to our “From Zero to Open Source Hero” series. If you missed our last post where we gave tips about launching open source mobile libraries, or our first post in this series, now’s a good time to check it out. This week, we are going to create an Android open source library and walk through the steps to get a simple project up and running. Let’s get to work!

Note: This tutorial uses Android Studio 2.0

Because we want to end up with an example app alongside a library, the easiest way to start is to create the example application first. To do this, let’s just create a simple Android application using the new app wizard inside Android Studio.

When filling out the project information there are a few things to keep in mind to make this as easy as possible.

  •  Application name: The name of your overall library
  •  Company Domain: The reverse of the package name you want to use for your library
  • Package Name: This is where you will make a slight change to account for the example application. The full package name should be something like “com.mypackage.mylibrary.example”. I followed the pattern of using the package I want for my library code with the suffix of “example”

Follow the rest of the prompts for creating the example application.

At this point, we should have a working example Android application with the following structure.

Now we need to add the library to the Android Studio project. To do this, right-click on the highlighted “app” folder above and select New -> Module.

This should open a new dialog for creating a new module, select “Android Library” and click “Next.” Set the application name, module name, and package name to what you want for your published library.

You should now have both an Android application as well as an Android library in the same Android Studio project.

We’re almost done. Not only does the example application serve as an easy way to get up and running for developers wanting to use your library, it also creates a quick and easy way to test your library as you build it. We need to make a couple changes to the example app and we’ll be good to go.

First, I like to rename the example app from “app” to something like “example”. Right-click on the “app” module and select Refactor -> Rename and change the module name to “example”.

After the rename your structure should look like this.

Second, we want to make sure the example application always has the latest version of the library to test against, so we want to add the library project as a local dependency. To do this we just need to add a local project compile line to the example Gradle file. Notice we use ‘:mylibrary’ as the compile dependency. Make sure to substitute this with the module name of your library.

That’s it!

Now you have a blank library project that can be tested and demonstrated easily in an accompanying example app! The next post in this series will discuss architectural considerations you should think about before diving in to write your library code.