Image for post
Image for post
Photo by Andrew Neel on Unsplash

DevOps-ify Android libraries with GitHub Actions & Package Registry 🚀

Shreyas Patil
Oct 2 · 5 min read

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.

What’s different in GitHub Package Registry? 🤷‍♀️

  • In the Android library context, comparing it with Bintray / JCenter / MavenCentral, GPR provides security for consuming package.

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.

  • Open build.gradle file of your library module.
  • Declare Android library details such as group, artifact, version, name, etc as below 👇
  • Create a task for generating Android source (.jar)
  • 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 write:package.
  • Thus, Gradle task — publish will be generated. But package artifact should be generated before publishing package. So we’ll define it as below 👇.

Thus whenever gradlew publish is executed then assembling will be performed first.

That’s it 😃. Now let’s set up GitHub Actions Workflow for automating it 🚀.

Setup Workflow

We’ll create a workflow which will be triggered whenever a git tag is pushed. So let’s create.

  • Create a file release.yml in /.github/workflows directory. 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_TOKEN which is exposed by GitHub Actions CI already.

If you remember, we were reading username and password from System Environment variables in build.gradle configuration 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 draft as true so 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 🧪.

Test Workflow

Follow these steps for testing workflow:

  • Go to VCS → Git → Tag
Image for post
Image for post
  • Create a tag using a valid version
Image for post
Image for post
  • Now push that newly created tag
Image for post
Image for post
Image for post
Image for post

Once you see success status of CI as seen in the above image means everything worked perfectly 😍.

Image for post
Image for post
Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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.
Image for post
Image for post

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.gradle of the app module.

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! 😄

References

GitHub Repository:

ScaleReal

We build applications that scale beyond reality

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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