Creating and Publishing an Android Library
I’ve always wanted to make a library and “contribute to open-source”, but never really had anything of considerable value to offer, until I needed to publish a library myself, so here’s a guide on how to make and publish one!
I’m currently working on a project where I had to make a time range picker, so my first thought was to show a TimePicker
dialog, and after the user has selected their Start Time, I’d show another one where they could choose the End Time.
While this would work, it would also be tedious for users because they’d have to remember the time they chose, and they wouldn’t have easy access to it in case they wanted to change it again. More-so, I’d drown in callbacks because I can only show the second dialog after the user is done with the first, and I’d have to save their chosen time somewhere, etc.
From this, my tiny library BottomSheetTimeRangePicker was born (that’s a ridiculously long name, I know!). It shows a BottomSheetDialogFragment
that has two tabs, each with its own TimePicker
where the user can chose their desired time range and change it as they please before hitting “Done”.
Alright, but how do you even make a library?
A library is essentially an Android project, just packaged differently. It can include anything that an app module does, like resource files, an Android manifest, and even external dependencies. However, instead of compiling into an APK that runs on a device, it compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module.
To create a library module in Android Studio, you just have to do this:
- Click File > New > New Module.
- In the Create New Module window that appears, click Android Library, then click Next.
- Give your library a name and select a minimum SDK version for the code in the library, then click Finish.
Wait for Gradle to sync, then your new library module will appear in the panel on the left. Congratulations!
You can learn more about this from the official docs here
Ok, how do I publish this?
There are many ways to distribute an android library, so here’s a couple of them
This is probably the easiest one. You sign in with your Github account, add your repo, and you’re set!
2. jCenter
jCenter() is a central repository hosted by Bintray, and in order to publish your library there, you’ll have to create an account on bintray. If you’re planning on publishing an open-source library, you can sign up for a free open source account here. Otherwise, you can look into their paid plans for hosting private repositories.
Add a new repository to host your project
Your new repository should now appear on your account. Open it, and add a new package
Create a package inside your new repository
Get an API key
Next, you need to get an API key to use it to push your library to the repository from Android Studio. Go to https://bintray.com/profile/edit, and choose API Key from the left menu.
You’re almost there! All you need to do now is add some Gradle scripts to your library
Add your username and API key to your local.properties file
Open your local.properties
file (make sure it’s added to your .gitignore
file!), and add the following:
Add the Bintray and Maven Gradle plugins to your project
In your app-level build.gradle
file, add these dependencies to your buildscript
Create the script for uploading your library package to Bintray
Thanks to Gradle’s modularity, we can extract all the scripts required to do this and put them in a single file. Inside your library package, create a new file called publish.gradle
and paste this code in it.
Then in your project-level build.gradle
file, add the following line to the bottom: apply from: 'publish.gradle'
The only things you need to care about in this file are these three lines:groupId, artifactId, version
. Together, they make up the link to your library, so users can download it by doing this:
implementation 'groupId:artifactId:version’
Upload to Bintray
The last thing left to do is to publish your shiny new library to bintray so others can use it. So build your project, fire up the terminal in Android Studio and type down this command:
If you’re on Windows
gradlew bintrayUpload
If you’re on Linux/Mac
gradle bintrayUpload
That’s it! Your library is now published and other people can use it. I hope you found this useful. You can see the code in action in my library here.