Build apk automatically with Github Action | by Anang Pambudi | Mar, 2025

It’s been long time I explore about this automatic build with github action and recently successful implement it. This story has documentation purpose so I can refer here when need to implement github action for my android project.

There are many article about github action for build apk, but when I refer to the article, it doesn’t work in my environment. And after brainstorming with Claude.AI finally I success implement it. I think it’s because I became little more understand about the flow. So actually You could skip this story and try asking some AI out there 😅😅, they are very helpful in this case.

In github action, actually we just replicate what our local machine do. In this case is, what actually Android studio do to compile and build our code become APK or AAB. We can start from create new workflow by open our repository page, go to Actions -> New Workflow.

We can start from scratch by click set up a workflow yourself or using template.

The flow start by define when this workflow will execute. Usually we define when we push or merge request on certain branch. Here I define workflow name “Generate APK” and will execute when I push commit into staging branch.

name: Generate APK

on:
push:
branches:
- staging

Next step we define machine to execute our workflow and environment version. For safety We could adjust just like java version in our local machine.

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up JDK (fixed)
uses: actions/setup-java@v3
with:
java-version: '17' # Your desired Java version
distribution: 'adopt' # Using AdoptOpenJDK (replace if needed)

Now We need define command to execute build our apps. We can refer on build variant on Android studio for which build configuration We want to use.

- name: Make Gradle executable
run: chmod +x ./gradlew

- name: Build APK for Staging
run: ./gradlew assembleBetaStaging # Adjust as per your build configuration

As previous step is built our app, then We need to upload generated apk into Github Artifact

- name: Debug - List build outputs
run: find app -name "*.apk" -type f

- name: Upload APK artifact
uses: actions/[email protected]
with:
name: Gameskii-staging-apk
path: app/build/outputs/apk/**/*.apk # Adjust path and filename

If our project using local.properties and keystore to build apk, We need to save all properties value in github secret to build our app. We can go to Settings -> Secrets and variables -> Actions.

Secret and variable

We can add secret and store all value We need to build We apps. For example, Key Alias, Key Password, Keystore Path, Keystore password and Keystore File itself. For Keystore We need to extract the file into Base64 format. You can execute this in your terminal. -i for input and -o for output

base64 -i your_keystore.jks -o keystore_base64.txt

We can check there is new file generated. And copy the text into github secret. After add all secret We can add this step in Our workflow.

 .... setup jdk

- name: Decode Keystore
run: |
echo "${{ secrets.BASE64_KEYSTORE }}" | base64 --decode > keystore.jks

- name: Create local.properties
run: |
echo "sdk.dir=$ANDROID_HOME" > local.properties
echo "storeFile=${{ secrets.STORE_FILE }}" >> local.properties
echo "storePassword=${{ secrets.STORE_PASSWORD }}" >> local.properties
echo "keyAlias=${{ secrets.KEY_ALIAS }}" >> local.properties
echo "keyPassword=${{ secrets.KEY_PASSWORD }}" >> local.properties

..... make gradlew executable

I use local.properties as file name because in my gradle I called local.properties to access my credential

And all the code would look like this.

name: Generate APK

on:
push:
branches:
- staging

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up JDK (fixed)
uses: actions/setup-java@v3
with:
java-version: '17' # Your desired Java version
distribution: 'adopt' # Using AdoptOpenJDK (replace if needed)

- name: Decode Keystore
run: |
echo "${{ secrets.BASE64_KEYSTORE }}" | base64 --decode > keystore.jks

- name: Create local.properties
run: |
echo "sdk.dir=$ANDROID_HOME" > local.properties
echo "storeFile=${{ secrets.STORE_FILE }}" >> local.properties
echo "storePassword=${{ secrets.STORE_PASSWORD }}" >> local.properties
echo "keyAlias=${{ secrets.KEY_ALIAS }}" >> local.properties
echo "keyPassword=${{ secrets.KEY_PASSWORD }}" >> local.properties

- name: Make Gradle executable
run: chmod +x ./gradlew

- name: Build APK for Staging
run: ./gradlew assembleBetaStaging # Adjust as per your build configuration

- name: Debug - Find APK files
run: find app -name "*.apk" -type f

- name: Upload APK artifact
uses: actions/[email protected]
with:
name: Gameskii-staging-apk
path: app/build/outputs/apk/**/*.apk # Adjust path and filename

And the result should look like this

Hopefully it would help us when need to implement this github action. Thank you for reading this story.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top