DEV Community

Cover image for Use GitHub Secrets in Your Android App
Sumanth Perambuduri
Sumanth Perambuduri

Posted on • Edited on

Use GitHub Secrets in Your Android App

Handling Secrets Securely ๐Ÿ”

If your Android app uses API keys or other sensitive data, you definitely donโ€™t want to hardcode them in your source files or accidentally push them to GitHub.
Hereโ€™s how you can securely inject secrets into your Android app using GitHub Actions โ€” so your keys stay safe and your builds stay automated.

Alright, Letโ€™s Get Straight to the Point โšก

  • Add the following paths to your .gitignore file:
app/src/main/res/values/secrets.xml
Enter fullscreen mode Exit fullscreen mode
  • Create a new file named secrets.xml in the path: app/src/main/res/values/

  • Add the following code in 'secrets.xml' :

<resources>
    <string name="API_KEY">YOUR_KEY_HERE</string>
</resources>
Enter fullscreen mode Exit fullscreen mode
  • Next, add the following code to your activity.kt file:
val secret = getString(R.string.API_KEY)
Enter fullscreen mode Exit fullscreen mode
  • Then, add this code under steps in your workflow file โ€” right before the build step:
- name: Access Api keys
  env:
    apiKey: ${{ secrets.API_KEY }}
    path: app/src/main/res/values/secrets.xml
  run: |
    touch $path
    echo \<resources\> >> $path
    echo \<string name=\"API_KEY\"\>$apiKey\</string\> >> $path
    echo \</resources\> >> $path
Enter fullscreen mode Exit fullscreen mode
  • Finally, add a new secret to your GitHub repository with the key API_KEY.

Thatโ€™s it โ€” youโ€™re good to go! ๐Ÿš€

Hereโ€™s an example project implementing the entire process:
๐Ÿ‘‰ Spotify Playlist Downloader

Recommended Read ๐Ÿ“–
If youโ€™d like to understand the complete process of building, signing, and releasing Android apps on GitHub using GitHub Actions, I highly recommend reading my earlier post first โ€” it covers all the fundamentals before we dive into using secrets:
Building, Signing and Releasing Android Apps with GitHub Actions

Top comments (0)