Publish Android libs to Nexus
Creating a great library is hard work. Coming up with the idea, implementing it, making sure you have a nice, stable public API that you control carefully and maintain… That’s already lots to do.
After all that, you need to make your library available to the public. Technically, you could distribute the .aar file any way you want, but the norm is publishing it to a publicly available Maven repository. It’s a good idea to use one of the well-established repositories that people are already likely to have in their projects, to make getting started with your library as easy as possible.
The fanciest place you can be in is The Central Repository via Sonatype OSSRH (OSS Repository Hosting), which I’ll refer to as simply MavenCentral from here on. This is the place to be if you’re a Maven dependency. Artifacts on MavenCentral are well trusted, and their integrity can be verified, as they are all required to be signed by the author.
The simplest choice would be JitPack, which might not give you much in terms of customization or control, but is very easy to get started with. All you have to do is publish your project on GitHub, and JitPack should be able to build and distribute it immediately. If you’re new to libraries, this is a great choice for getting your code out there.
I try to use JitPack but failed to config. If you success to use JitPack in the Android SDK for Stream Chat, please let me know. Thanks.
Overview
Here's a quick overview of the steps we'll go through:
- Registering a Jira account with Sonatype, and verifying your ownership of the group ID you want to publish your artifact with
- Generating a GPG key pair for signing your artifacts, publishing your public key, and exporting your private key
- Setting up Gradle tasks that can sign upload your artifacts to a staging repository
- Manually going through the process of checking your artifacts in the staging repo and releasing them via the Sonatype web UI
- Automating the close & release flow with a Gradle plugin
- Configuring CI workflows with GitHub Actions to automate all of the above A lot of ground to cover - let’s go!
Prerequisites
We’ll be using the following tools for this tutorial. You are free to use alternatives, but these are our favourites, and they work well for us.
- GitHub as the public host of the library’s repository
- Registering a Sonatype account
- The command line gpg tool
For this article, we’ll assume that you already have your library developed, and have uploaded it to a public GitHub repository. We’ll use our very own Android Chat SDK in our examples. This SDK is made up of multiple artifacts, but for simplification, we’ll just talk about publishing the low-level networking client, which lives in the stream-chat-android-client module of the GitHub repository.
Registering a Sonatype account
First things first, you’ll need an account in the Sonatype Jira. Head over there and hit Sign up. Registration is straightforward, it just requires a username, an email, and a password. After you’ve logged in, you’ll need to open an issue, asking for access to the group ID that you’ll want to publish your project under. For us, based on our domain name (gitcoins.io), our group ID is io.gitcoins. If you own a domain, it’s best to choose the reversed version of that as your group ID. Otherwise, you’ll have to stick with having a GitHub-based group ID.
After choosing a language and an avatar, you’ll end up on this landing page - click on Create an issue
- Summary: Create repository for your.group.id.here
- Description: An optional, quick summary of what your project is.
- Group Id: Your group ID, as described a few sections earlier.
- Project URL: If your project has a webpage, the URL of that page. This can also be just the GitHub repository.
- SCM url: Your source control URL, i.e. the GitHub repository link.
- Username(s): If you want additional users (on top of the one you’re using for this process) to have deploy access for your group ID, you can list them here.
- Already Synced to Central: If you’re just getting started, this should be No.
Soon after opening it, your issue will get a comment telling you to verify that you own the domain corresponding to your group ID. To comply with this, add the required TXT record to your domain - how to do this will depend on where your domain is registered, but it should be a fairly simple task. Login to the goDaddy to set the TXT record with the guide from the issue created in jira for Sonatype.
Generating a GPG key pair
As we eluded to earlier, artifacts published on MavenCentral have to be signed by their publishers. You’ll need a GPG key for this.
MavenCentral also has its own documentation for Working with PGP Signatures which you can reference if you get stuck along the way.
This part requires access to the gpg command. There are several ways to install this via package managers, and there are many distributions available for different platforms on gnupg.org.
We'll stick to the command line here on Linux for generating and managing keys.
To generate a new key, run the following command:
gpg --full-gen-key