Uploading Android APKs to Firebase App Distribution from a Node.js Application — Part 1

Jorge Luis Castro Medina
3 min readApr 28, 2024

--

Hello everyone! 👋 Today, I’d like to share with you how to upload Android versions to Firebase App Distribution through an external server using Firebase CLI. Before we begin, I’d like to say that these steps can be applied locally. The main reason is to have the Service Account isolated and secure to connect with Firebase without granting access to it to client users. (It is just another alternative you could apply) 😊

For the example, let’s assume we have a Firebase project configured with App Distribution with the following parameters:

  • App ID: 1:123456789300:android:12345abcdefghijk12abc9
  • group of testers: qa-testers

Configuring the Service Account

To connect with Firebase, we’ll need a valid Service Account. Here are the steps briefly outlined:

  1. On the Google Cloud console, select your project and create a new service account.
  2. Add the Firebase App Distribution Admin role.
  3. Create a private JSON key and move the key to a location accessible to your build environment. Be sure to keep this file somewhere safe, as it grants administrator access to App Distribution in your Firebase project. Skip this step if you created your app after September 20, 2019: In the Google APIs console, enable the Firebase App Distribution API. When prompted, select the project with the same name as your Firebase project.

Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to your service account key file

export GOOGLE_APPLICATION_CREDENTIALS=/absolute/path/to/credentials/file.json

We also need to add another environment variable for APP_ID.

export APP_ID=1:123456789300:android:12345abcdefghijk12abc9

Setting up our server with Node.js

We will need to do is install Firebase CLI on our server. For that, we can go to the Firebase documentation where we’ll have different ways to do it. I personally prefer using the option with NPM.

Install the Firebase CLI

Now we will create a project that will be our server, where we will expose an endpoint to upload the APK. This time I will use Node.js and Express, so I will assume that you have basic knowledge of these tools; if not, it is okay, what we are going to do is quite simple and you can use other preferred alternatives such as Go, Java, C#, Python, etc.

In your app.js or index.js file (depending on how you named it), add the following code:

I won’t go into the details of the code, I just want to highlight that we have set up an endpoint /upload that will be responsible for checking that the file we upload is a .apk and then it will start uploading it to Firebase App Distribution.

You can observe that the most important aspect of this function for uploading the APK is the Firebase CLI call, which follows the following structure (Line 52):

firebase appdistribution:distribute /Users/my-user/Android-Project/app/build/outputs/apk/debug/app-debug.apk \
--app 1:123456789300:android:12345abcdefghijk12abc9 \
--release-notes "Version uploaded from Firebase CLI" --groups "qa-testers"

Run the app with the following command:

node app.js

Now your application will be running on port 3000

Finally, we will test our server by uploading an APK using CURL.

Make sure of the following:

  1. Have built in order to generate the APK.
  2. Make sure your apk path is correct.
curl -X POST \
-F "file=@/Users/my-user/Android-Project/app/build/outputs/apk/debug/app-debug.apk" \
-F "group=qa-testers" \
-F "releaseNotes=Version uploaded from Firebase CLI" \
https://your-domain:3000/upload

If you like my content and want to support my work, you can give me a cup of coffee ☕️ 🥰

Follow me in

--

--

Jorge Luis Castro Medina

I'm a Software Engineer passionate about mobile technologies, and I like everything related to software design and architecture