How to Send APK File Automatically with Firebase App Distribution

Image for post
Image for post

Now, I will share about how developers can send APKs to anyone who needs it directly.

You can integrate App Distribution into your Android build process using the App Distribution Gradle plugin. The plugin lets you specify your testers and release notes in your app’s build.gradle file, letting you configure distributions for different build types and variants of your app.

Before you begin

If you haven’t already, add Firebase to your Android project.

If you aren’t using any other Firebase products, you only have to create a project and register your app.

Download .json file from Firebase and add file to your project(module).

1. Set up your Android project

Open the build.gradle (project) file and embed the following plugin firebase appdistribution:

classpath 'com.google.firebase:firebase-appdistribution-gradle:1.3.1'
Image for post
Image for post
build.gradle (project)

Now, do not press the Sync button first. Open the build.gradle (module) file and embed the following script:

apply plugin: 'com.google.firebase.appdistribution'

It will be like this:

Image for post
Image for post
build.gradle (module)

Now Sync the project.

2. Authenticate with Firebase

Before you can use the Gradle plugin, you must first authenticate with your Firebase project. There are three ways to achieve this:

Authenticate using your Google Account

  • Run the following command:
For Window PC : </your_project/> gradlew appDistributionLogin
For Mac/Linux PC : </your_project/> ./gradlew appDistributionLogin
  • The command prints an authentication link.
  • Open the link in a browser.
  • When prompted, sign in to your Google Account and grant permission to access your Firebase project. The Gradle command prints a refresh token, which the App Distribution plugin uses to authenticate with Firebase.
  • Set the environment variable FIREBASE_TOKEN to the token from the previous step:
For Window PC : set FIREBASE_TOKEN=<token>
For Mac/Linux PC : export FIREBASE_TOKEN=<token>

Authenticate using a service account

To authenticate using service account credentials:

Image for post
Image for post
  • Add the Firebase Quality Admin role.
Image for post
Image for post
  • 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 your Firebase project.
Image for post
Image for post
  • 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.
  • Provide or locate your service account credentials:

A. To pass Gradle your service account key, in your build.gradle file, set the property serviceCredentialsFile to the private key JSON file which you previously downloaded in above steps.

Image for post
Image for post
build.gradle (module)

B. set the environment variable GOOGLE_APPLICATION_CREDENTIALS. Replace [PATH] with the path of the private key JSON file and [FILE_NAME] with the filename.

For Window PC : set GOOGLE_APPLICATION_CREDENTIALS=/path/to/file.jsonFor Mac/Linux PC :  export GOOGLE_APPLICATION_CREDENTIALS=/path/to/file.json

3. Configure your distribution properties

In your app/build.gradle file, configure App Distribution by adding at least one firebaseAppDistribution section

buildTypes {
release {

firebaseAppDistribution {
appId = "APP ID FROM FIREBASE"
serviceCredentialsFile = "/path/to/serviceaccount.json"
releaseNotesFile = "/path/to/releasenotes.txt"
testers = "text1@gmail.com, text2@gmail.com, text3@gmail.com"
}
}
}
Image for post
Image for post

4. Distribute your app to testers

Now it’s time we try to do (semi) automatic APK distribution. The first thing to do is to make the APK and the second thing just do the APK distribution.

For example, to distribute your app using the release build variant, run the following step:

  1. Open Terminal
  2. Run the following command to create an APK and Distribute to testers.
For Window PC: gradlew assembleRelease appDistributionUploadReleaseFor Mac/Linux PC: gradlew assembleRelease appDistributionUploadRelease

Press the Enter key and wait until the process is complete and if successful, the output on the terminal will be like this:

Image for post
Image for post

You can check app successfully uploaded in App Distribution section in Firebase Console.

Image for post
Image for post

After that testers got mail of how to accept invitation and download APK.

Enjoy your testing!!!

Image for post
Image for post

So, that’s it, this tutorial is for send APK file automatically with Firebase app distribution in Android.

Hopefully the knowledge is useful.

I have extensive experience in developing innovative and complex Android /iOS Apps and also in Website Developing. Been working in Kotlin for the last 2 years.

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