How to Publish Your Android Studio Library to JCenter
So following on from my earlier post about setting up a library project in Android Studio — I figured it would have been easier to publish to JCenter than it actually was! I also struggled to find a sequence of instructions that actually worked — so these are the steps that worked for me 😀
1. Module Gradle file
Firstly, open your build.gradle file for your module. We’re going to include a script to generate our .aar files (Android Library files) from this GitHub Repo: https://github.com/blundell/release-android-library
The code we’ll need to insert into our gradle file, right at the bottom, is the following:
apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle'
3. Module Gradle
Next we need to add a section into the same gradle file, below the first line that says the project is an android library.
This will be the information people will use as a gradle dependency in their own projects! So think carefully about what group and artifact id’s will be!
My information looks like the following:
PUBLISH_GROUP_ID = 'cyd.awesome.android'
PUBLISH_ARTIFACT_ID = 'awesome-material'
PUBLISH_VERSION = '1.0'
4. make file
Next, we’re ready to compile our library. You’ll need to open up a terminal (in Mac — you’ll have to Google how to do this bit in Windows) and navigate to the root of your library project and then type the following code (note that this might take a while and generate a lot of lines in your terminal)
./gradlew clean build generateRelease
When it succeeds you should see the following “BUILD SUCCESSFUL” — it also helpfully tells you the full location of the release zip folder that we’ll need later!
Bintray seemed to mentioned a LOT when I was Googling around how to upload my library. It’s a site that allows you to host files for other people to download.
a. Create New Repository
The first thing to do is to create a new repository for your library. This works out as an organisation name, you can have many projects inside. Which is a little different from GitHub.
Additionally, you can give it any name that you’d like, as long as it makes it easy for you to remember what it is! People installing your library won’t ever see or use this bit!
b. New Package
Next, we need to create a package inside. This works out like the project name. Again nobody else other than you (and any other co-creators) will see this name.
I named my project after the GitHub Org I had set up for my Android open source libraries, and the package was the current project I wanted to upload. If that helps!
c. New Version
Finally, we get to create a new version. Versions don’t have to be traditional numbers, you could give them any name you want. Again, this information won’t be used by anybody else. Which is fairly confusing the first time you go through all of these steps!
d. Upload files
So now we have our version, we can upload our files. This button took me a LONG time to find — but at the top of the page after you have created a version there should be an “upload files” button. I’ve highlighted the button in red below:
e. Tiny Check Box Bit
Here, you should be able to click to add your zip folder (the one we made in the terminal earlier) and upload it.
Before saving your changes, make sure you tick the tiny check box about exploding the archive. Without ticking this your project just won’t work!!!
f. JCenter request
You should receive some confirmation when your library is uploaded. The next bit is to ask JCenter to “link” with your project, which makes your library discoverable with gradle dependencies.
If you go to your “project” page in bintray — there will be a little button above “linked to” to add your project to JCenter. Here you’ll have to fill out a message and wait to be accepted. Though that has never taken me more than a few hours to get accepted 😀
6. What to tell other people!
This is the bit that had me stumped for the longest of time — and took me a good few days once my project was uploaded to actually download it.
You have to wait until you get a notification in bintray that your library has been accepted to join JCenter before anyone can begin to use your library. But what information do you give them? the project and package information from bintray? This took me several days to work out, and is only really obvious upon reflection 😥
Whatever you put into your build.gradle file inside Android Studio is the bit that actually matters — the publish group : publish artefact : and version make up the gradle dependency.
For example my dependency looks like this:
Written by Danielle Emma Vass — www.de-velopment.com
Twitter — @de_velopment
GitHub — @daniellevass