How to create an Android Gradle library.

Levi Bostian
Mar 13, 2017 · 4 min read

*Update* Looking for a Spanish version of this tutorial? Someone who enjoyed this tutorial created one! View it here. Thank you gallet!

Do you build Android apps? Have some code that you want to share to the world? Have some code you always seem to copy and paste into all of your individual projects? This is for you.

What I cover here:

  • What is an Android Gradle library?
  • Why create an Android Gradle library?
  • How to create an Android Gradle library?

What is an Android Gradle library?

(Are you a JS dev who uses npm? An iOS dev who uses CocoaPods and Carthage? A Ruby dev who uses RubyGems? If so, Gradle is the Android version of all those services.)

What is an Android Gradle library you ask? It’s a collection of code that someone has written and they allow you to install that code into your Android apps for you to use. Let me explain why this is so handy.

You are developing an Android app on a Thursday night. You are developing a new To-Do list app that calls an API to save the user’s data. You browse GitHub to find this awesome library called Retrofit that makes it super easy to call an API using Java. Retrofit sounds awesome! Now, how do you use it in your app? All you have to do is add one line of code to your app/build.gradlefile: compile 'com.squareup.retrofit2:retrofit:2.2.0', perform a Gradle sync in Android Studio...

…and now you can call the Retrofit source code in your Android app source code. You have imported the Retrofit code into your project to use!

Why create an Android Gradle library?

Has this ever happened to you? You are sitting at your laptop, programming away inside of Android Studio creating the latest and greatest new Android app. Then, as you are half way into it you say, “Oh, I remember 3 months ago I wrote some code that I can reuse in this app.” You proceed to open the GitHub repo for that app you wrote 3 months ago, find that file you wrote, and then you copy and paste that code into your new app you are working on now.

Instead of digging up old code and copy/pasting it, create an Android library instead. Then when you find yourself in this situation again where you want to use that code you wrote way back when, install your Android library in your Android Studio project via Gradle project and away you go! Super fast and easy.

I found myself needing to work with Android animations in my apps and tended to forget the API for how to use them. To help me, I wrote this code file, AnimationUtil that allows me to create animations easier. I have used this file in a half dozen different Android apps I have built. This is great and all, but becomes a huge pain once I decide to update my AnimationUtil file. I had to go back and copy/paste this newly updated file into all of those half dozen apps I worked on previously. Now instead of copying/pasting into each app project, I created an Android library called AndroidBoilerplate that contains AnimationUtil for me. Anytime I want to update this file, I update the Android library, push a new version out, then in each of my previous apps I can update to the latest version in Gradle! So much better to manage!

How to create an Android Gradle library?

Creating an Android library is really quick and easy. Here is a video I created on how to create an Android library from scratch, add your source code to it, test it, and release to the world allowing other projects to use it.

The video covers the following topics:

  • Create a Java Android library.
  • How to test your Android library by creating a sample app for it.
  • How to publish your Android library to allow anyone to install your Android library into their Android app (using the awesome service).
  • Create a private Android library which you give permission to who can install your Android library (using the awesome service).
  • Create a Kotlin Android library.
  • Create an Android library with multiple modules to allow developers to only install what they need in their app.

After you create your first Android library, send me a tweet with a link to it. I want to see what you create!

Are you a side project hacker? Like to build cool projects with code? Check this out. Check out my other posts about Android, Software, and Tutorials.

Originally shared on my blog: **

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store