Android — Bitbucket — Pipeline — CI/CD with Firebase App Distribution

Gowthaman Jeevanandam
4 min readDec 23, 2020

--

This article explains how to build an android project using Bitbucket Pipeline and distributing that build via Firebase App Distribution as an automated process which is triggered by every push or commit that we make to the Bitbucket remote repository.

Following 5 steps are the summary of this doc

  • Setting up Firebase App Distribution dependency in an android project in Gradle
  • Getting firebase refresh token by authenticating with our firebase project
  • Adding the release note and testers email for the release
  • Adding the KeyStore for signing the build or build variant
  • Setting up the Pipeline configuration file (bitbucket-pipelines.yml) and bitbucket repo

Step 1

In your project-level Gradle file, add the App Distribution plugin as a buildscript dependency:

buildscript {
repositories {
// Check that you have Google's Maven repository (if not, add it).
google()
jcenter()
}
dependencies {
// Add the App Distribution Gradle plugin
classpath 'com.google.firebase:firebase-appdistribution-gradle:2.0.1'
}
}

In your app-level Gradle file, include the App Distribution plugin below the Android plugin:

apply plugin: 'com.android.application'
// ...

// Apply the App Distribution Gradle plugin
apply plugin: 'com.google.firebase.appdistribution'
// ...

Step 2

Using firebase login:ci command in the Firebase CLI, login to your firebase project and get the firebase refresh token which will be used to distribute to the firebase after the app is built.

Getting the firebase account token

Step 3

Text format (.txt) release note file and testers file which contains tester’s emails with comma separated can be mentioned as shown below in the app-level build.gradle file.

buildTypes {

debug {
versionNameSuffix "-bhoozt-debug"
}
development {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
versionNameSuffix "-bhoozt-development"
signingConfig signingConfigs.debug

firebaseAppDistribution {
releaseNotesFile = file('development/releasenote.txt')
testersFile = file('development/testers.txt')
}
}
staging {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
versionNameSuffix "-bhoozt-staging"
signingConfig signingConfigs.debug
}
production {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
versionNameSuffix "-bhoozt-production"
}
}

In above code, firebaseAppDistribution property is used to join the release note and testers file which are in the project directory by relative file path.

testers.txt

one@gmail.com,
two@gmail.com,
three@gmail.com

Step 4

android {
signingConfigs {
development {
storeFile file('development/KeyStoreDevStaging.jks')
storePassword 'store_password'
keyPassword 'key_password'
keyAlias 'key_alias'
}
} …
}

Above code shows how to add the signing keystore file and its credentials in the app-level build.gradle file. KeyStoreDevStaging.jks is in the project directory and joined by the relative file path.

Step 5

Bitbucket’s pipeline config file (bitbucket-pipelines.yml) has to be placed in the project root directory.

project root directory structure

bitbucket-pipelines.yml will contain the following codes.

image: mingc/android-build-box:latest

pipelines:
branches:
master:
- step:
name: Deploy To Firebase App Distribution
caches:
- gradle
script:
- export FIREBASE_TOKEN=$FIREBASE_REFRESH_TOKEN

# Needed for env variable changes
- ./gradlew --stop

# Build and deploy apk to Firebase App Distribution
- ./gradlew assembleDevelopment appDistributionUploadDevelopment
artifacts:
- app/build/outputs/**

In above code, image: mingc/android-build-box:latest refers to the docker environment (image) which is related to a template in docker container where the pipeline process is done. It can be changed or we can create a new one if we want. This environment is a general one for the android environment which is already set up with Android SDK and other build tools.

If we change this docker image or create a new one, we may have to set up the android SDK and licenses in this bitbucket-pipelines.yml file.

Here master branch is selected to the automated process and assembleDevelopment appDistributionUploadDevelopment are the build and distribute commands for the Development build variant.

$FIREBASE_REFRESH_TOKEN is an environment variable which can be created in the bitbucket repository.

adding firebase taken as variable

You can manage repository variables in Project Settings > Pipelines > Repository variables as shown in the above image.

Add the variable name and the value will be the firebase refresh token which was taken in the step 2.

Make sure that you have enabled the Pipelines in your bitbucket repo. You can do that in Project Settings > Pipelines > Settings

Finally, Commit and Push your project to remote, It will build and deploy to your firebase and send invitations to your testes with the release note as shown in below.

Thank you..

-Gowthaman Jeevanandam-

--

--

Gowthaman Jeevanandam
Gowthaman Jeevanandam

Written by Gowthaman Jeevanandam

Software Developer, Space & Aviation Enthusiast

No responses yet