If you have ever thought of writing your own custom Android library
for others
to use or extend, and wonder how you could achieve that, then you can join me on
this ride. In this post, I will be showing a step by step guide on how to create
an Android library
. I will also be showing you how to distribute your library
through JitPack.
We will be creating a simple statistics library, that can be used to compute basic
Arithmetic Mean
, Median
and Mode
. To achieve this, we follow the steps
below:
- Create a new Android app that showcases the library
- Create a new library module within the Android app
- Write the code for your custom library
- Add the library as a dependency to the project
- Publish your library to Github
- Setup to share your library with any other android project using
Jitpack
Create a new Android project
Create a new Android project using the create new project wizard on Android studio. This project will be used to test your library and also for later showcasing it.
Create a new library module
Next is to create a new Android libraray
module within the created Android app above.
Right-click the project new -> module then select Android Library
.
Give your library a name on the next screen and click finish.
Once the project is done building, we should see our newly created module in the project.
Write library code
Our library for now will have just one class. CentralTendency.java
. Create this
file in the java
folder under the library module.
Check this github repo for the complete code.
Add the library module as a dependency of the android project
Go to File -> Project Structure, click on the app module and select the Dependencies tab. Use the (+) icon at the bottom of the window to add a new module dependency
. Select the newly created library.
Click the ok
button when done. The dependency should be automatically added to the app
module build.gradle
file.
In the onCreate
method of our MainActivity
, we add the code to showcase this library.
Publish your library to Github
Now that our app is ready, we need to upload to Github
. Init a git repository in the project root, create a new remote git repository on Github and push all committed codes to the remote repo. If you are not familiar with git, you can learn about git here.
Setup and share your library through Jitpack
Jitpack is an easy to use package repository for Git. It allows you share your library with anyone by just adding the dependency of your library to their projects. We need to setup our library for Jitpack
by adding some few lines to our gradle files.
- Add the android-maven plugin. In the root
build.grade
file, add:
- In the library module
build.gradle
file, add the following below the first apply plugin line:
- Create a Github release or add a TAG In the project root, run the command below to create v1.0 tag for newly created library:
- Ensure that you have
Gradle Wrapper
in your repository. If you don’t then create it using the commandgradle wrapper
and commit it. When this is done, run this command in the project root:
This will install this library in the local maven repository ($HOME/.m2/repository)
.
- If the build is successful and the project has also been uploded to Github, go to this
jitpack
url:
On the website, select the release you would like to share by clicking the Get It
button next to the list of releases of your repository.
Add a README file
Automatically, Github looks for a file names README.md
in the root of your project and displays it below. At minimum, the README file should contain the following:
- A badge with the status of your library repository.
- A line describing how your library can be added to an android project.
- Sample code showing how to use the library.
- Finally, the library license usage. Check out this link for sample open source licensing.