Setting up GitHub Actions for Android Projects with several API keys.

Setting up GitHub Actions for Android Projects with several API keys.

- 5 mins

If you are familiar with the concept of CI/CD, you would agree that GitHub Actions is one of the best things to happen in this field as it makes it so easy to automate your software workflows. You get to build, test, and deploy your code right from GitHub and this is what makes it so cool.

While I was building Instant Weather, I setup the project without a CI/CD tool and this only dawned on me when the project got its first Pull Request. This post would explain how I integrated GitHub Actions into the live project and how you can do so for any of your Android projects.


In the world of Android development, we are always consuming one API or the other which we connect to using a key. In the process of open-sourcing our projects, we definitely can not expose these API keys to the public. In an attempt to hide the API keys Instant Weather uses, I used Gradle to secure these API keys. You can check this article on how to get this done. Now let’s walk through how I setup GitHub Actions for my live project.

Step 1

The first step was to create a new branch in the project and checkout that branch. A simple “git checkout -b new-branch-name”. After which I created .github\workflows\ directory in the root of the Android project. In that same directory, I created the workflow file. I named this workflow file android_build.yml. The file uses the YAML file extension.

Step 2

Next step was to write out the content for the workflow file starting with the name. This name should match the workflow file’s name. After which I defined when I wanted this workflow file to be executed using the on key which were when a Pull Request was made or when a push occurs to the repository. By now we have something like this:

name: Android Build

on: [push, pull_request]

Step 3

Next step was to define the jobs that would run when the workflow is triggered. We use the jobs keyword to define this. A Job is basically an environment with a task or several tasks. Note, every workflow must have at least a Job. Now we have something like this:

name: Android Build

on: [push, pull_request]

jobs:
  build:

    runs-on: ubuntu-latest

I want this to run on the pre-installed runner ubuntu-latest and I indicated this using the runs-on keyword.

Step 4

After defining the environment, the next step was to define the tasks that would be run during the build. We make use of the steps keyword to achieve this.

 - name: Create apikey.properties
        run: |
          cat <<EOF> apikey.properties
          API_KEY = "API_KEY"
          ALGOLIA_API_KEY = "ALGOLIA_API_KEY"
          ALGOLIA_APP_ID = "ALGOLIA_APP_ID"
          EOF
- name: Set Up JDK
        uses: actions/setup-java@v1
        with:
         java-version: 1.8
- name: Run Tests
        run: ./gradlew test

      - name: Build Project
        run: ./gradlew assemble

Wrapping this up, we should have this by now:

name: Android Build

on: [push, pull_request]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Create apikey.properties
        run: |
          cat <<EOF> apikey.properties
          API_KEY = "API_KEY"
          ALGOLIA_API_KEY = "ALGOLIA_API_KEY"
          ALGOLIA_APP_ID = "ALGOLIA_APP_ID"
          EOF

      - name: Set Up JDK
        uses: actions/setup-java@v1
        with:
         java-version: 1.8

      - name: Run Tests
        run: ./gradlew test

      - name: Build Project
        run: ./gradlew assemble

Final Step

Now, we have successfully written out a simple workflow for our project. You can validate the syntax of this YAML file using the YAML Lint tool. GitHub Actions would only recognize this workflow if it’s in our master branch. The final step was to save all our changes git add ., commit the changes git commit -m "Commit message" and push to the current branch I was working on. I then proceeded to GitHub, created a Pull Request from that branch and merged it into my master branch. Having done this, we would have setup GitHub Actions for our repository. Any commit we push to the repository, or a Pull Request made to the repository would trigger our workflow as we have set it to. And voila! we have this:

Incase you are experiencing any issue setting this up, Nate Ebel has a good tutorial on YouTube to help you understand. Check it out here. You can also send me a message on Twitter. Thank you for your time :thumbsup:

Mayokun Adeniyi

Mayokun Adeniyi

Android Developer

comments powered by Disqus
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora