DevOps-ify Android libraries with GitHub Actions & Package Registry 🚀
Hello Android developers, in this article we’ll take a look at publishing Android library to the GitHub Package Registry and automating it with GitHub Actions CI. You might have developed a cool Android library and wanted to publish it somewhere. In some situations, the GitHub Package Registry is really a good choice. Let’s discuss more on it.
What is GitHub Package Registry? 🤷♀️
- With GPR, you can safely publish and consume packages.
- It supports various types of packages for maven, NPM, Docker, NuGet, RubyGems, etc.
What’s different in GitHub Package Registry? 🤷♀️
- In the Android library context, comparing it with Bintray / JCenter / MavenCentral, GPR provides security for consuming package.
- Anybody directly can’t use the package without an access token.
- If you want to develop a private Android library only for some authenticated developers. For e.g. You have to develop an Android library only for a specific organisation. The only organisation will be able to use this library. Comparing this with Bintray, then it’s PAID 💰 for private packages. That’s how GPR is the right choice in some cases.
That’s a short introduction about GPR 😃.
Getting started 🚀
You can refer to this repository which includes the code which we’re going to use in this example.
In this example, we’ll set up a GitHub Actions workflow which will be triggered whenever a git tag is pushed on GitHub and it’ll publish Android library to GPR and will also create GitHub release automatically. So you just need to code, push and chill 😍. So let’s start.
Configure Library module
Considering you already have developed your library we’ll directly start with configuring setup for publishing it to GPR.
build.gradlefile of your library module.
- Apply maven-publish plugin at the top of the file.
- Declare Android library details such as group, artifact, version, name, etc as below 👇
- Create a task for generating Android source (
- Configure maven publication options as below 👇. This will be responsible for a group and an artifact ID of your Android library.
- In repositories, add details of your package. In this configuration, keep the name as
“GitHubPackages”and URL should be in the format
“https://maven.pkg.github.com/USER/REPO”. Here we’ll get credentials from environment variables. Here password will be a GitHub’s PERSONAL ACCESS TOKEN which should have a permission
- Thus, Gradle task —
publishwill be generated. But package artifact should be generated before publishing package. So we’ll define it as below 👇.
gradlew publishis executed then assembling will be performed first.
That’s it 😃. Now let’s set up GitHub Actions Workflow for automating it 🚀.
We’ll create a workflow which will be triggered whenever a git tag is pushed. So let’s create.
- Create a file
/.github/workflowsdirectory. Initialize it as below.
- Add basic setup for job as below 👇. Here Gradle caching is optional.
- Now, let’s build
.aar(Android Library artifact).
- Now if the above build is successful then we can proceed to publish the package to GPR. Use
secrets.GITHUB_TOKENwhich is exposed by GitHub Actions CI already.
If you remember, we were reading username and password from System Environment variables in
build.gradleconfiguration of the library. We’ll need to expose them from here.
- Now once the package is published to GPR we’re ready to create a GitHub release. (Here we are keeping
trueso that we can release it later).
- Once the GitHub release is created it will upload library artifact to the release.
Yeah, that’s it! 😃 Now let’s test it 🧪.
Follow these steps for testing workflow:
- Go to VCS → Git → Tag
- Create a tag using a valid version
- Now push that newly created tag
- Once it’s pushed navigate to your repository’s Actions tab.
Once you see success status of CI as seen in the above image means everything worked perfectly 😍.
- Now navigate to Packages section of repository and you’ll see package details there as 👇.
- Now let’s verify if GitHub release is created or not. Navigate to releases section of the repository and see.
As you can see draft release is created here. Now you can edit it, add a description of the release and then you can publish this release.
Yeah! Now anybody can use this package if they have an access token. But how to use it? 🤔 Let’s see.
Creating an Access token for reading package
As we discussed, we’ll need GitHub access token for consuming package. So let’s create an access token for it.
- Navigate to https://github.com/settings/tokens and then click Generate New Token.
- Make sure to check scope —
This means only the reading package is possible using this token. So it’s safe to give it to the consumer.
- Click ‘Generate Token’ and the token will be generated. Make sure you’ve noted token.
Now it’s time to use Android library in app.
Configuring Android app
Open Android app project from which the library will be used and perform the following steps.
- Just open
build.gradleof the app module.
- Add repository for GitHub package.
In the credentials, keep your GitHub’s username as username and put the token which we created in the previous step as a password.
- Finally, add a dependency of the Android library.
Yeah, that’s it. Now just do Gradle sync and enjoy 🎉
Isn’t it easy 😃? This is how you can automate or DevOps-ify your Android library workflow so you will just write code, push it, chill and enjoy 😅.
I hope you liked this article and it’ll be helpful for everyone!
Thank you! 😄