Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KvRae/202e71c0b6c9303f287a05d5abc2e3a7 to your computer and use it in GitHub Desktop.
Save KvRae/202e71c0b6c9303f287a05d5abc2e3a7 to your computer and use it in GitHub Desktop.
Full guide on how to integrate bitbucket pipelines with your Android project to build and deploy your artifact (APK) to Firebase App distribution

Android Project with Bitbucket Pipelines and Firebase App Distribution

Bitbucket Firebase Android Gradle Node.js Firebase CLI Google Cloud Console

Table of Contents

Introduction

This guide will walk you through the process of setting up Bitbucket Pipelines for your Android project and deploying it to Firebase App Distribution for testing.

Prerequisites

  • A Bitbucket account with a repository (you will need admin access to enable Pipelines)
  • An Android project hosted on Bitbucket
  • A Firebase account
  • Firebase CLI installed on your local machine
  • Node.js installed on your local machine
  • A Google Cloud Console account

Step 1: Configure Firebase

  1. Login to Firebase with your Google account.
  2. Create a new project or use an existing one.
  3. Navigate to Project settings > Your apps.
  4. Download the google-services.json file and place it in the app directory of your Android project.
  5. Install Firebase CLI by running the following command in your terminal:
npm install -g firebase-tools

NOTE

  • You need to have Node.js installed on your machine to use npm. You can download it from Node.js official website.

Step 2: Generate a Firebase Token

Run the following command in your terminal to generate a Firebase token:

firebase login:ci

This will open a browser window asking you to log in with your Google account. After logging in, you will see a token in your terminal. Copy this token as you will need it later in the Bitbucket Pipelines configuration.

Step 3: Create a Service Account Key

  1. Go to the Google Cloud Console.
  2. Select your project.
  3. Navigate to IAM & Admin > Service Accounts.
  4. Click on Create Service Account.
  5. Enter a name for the service account and click Create.
  6. Assign the Firebase Admin SDK Administrator role to the service account.
  7. Click on Create Key and select JSON as the key type.
  8. Save the JSON file to your local machine.
  9. Encrypt the JSON file using Bitbucket Pipelines environment variables. You can use the Bitbucket UI to encrypt the file or use the following command:
cat service-account-key.json | base64 | pbcopy

Step 4: Enable Bitbucket Pipelines

  1. Navigate to your Bitbucket repository.
  2. Go to Repository settings.
  3. Click on Pipelines > Settings.
  4. Toggle the Enable Pipelines switch.

Step 5: Create a bitbucket-pipelines.yml File

Create a bitbucket-pipelines.yml file in the root of your repository. This file will define your build pipeline.


NOTE

You will need to enable pipelines in Repository settings > Pipelines > Settings > Enable Piplines before you can create the bitbucket-pipelines.yml file. You can use the following template as a starting point for your bitbucket-pipelines.yml file.

image: cimg/android:2024.01 # image defines the Docker image to use as the build environment
pipelines: # pipelines is a special keyword that defines the build configuration
  default: # pipeline definition for all branches, to run it automatically when a release is created you can use the branches : release/* or tags instead of default
    - step: # step to build Android debug application
        name: Build Android Debug Application
        caches: # caching speed up subsequent execution
          - gradle
        script:
          - ./gradlew assembleRelease appDistributionUploadRelease # ./gradlew assembleDebug appDistributionUploadDebug for debug build
        artifacts:
          - app/build/outputs/**   # artifacts are files that are produced by a step
    - step:
        name: Deploy to Firebase App Distribution
        deployment: test # deployment allows you to define a deployment environment
        script:
          - pipe: atlassian/firebase-deploy:2.0.0 # pipe that allows you to use a Docker image as a service
            variables:
              KEY_FILE: $KEY_FILE # KEY_FILE is generated from google cloud console service account and encrypted using bitbucket pipelines environment variables
              FIREBASE_TOKEN: $FIREBASE_TOKEN # FIREBASE TOKEN is generated from firebase login:ci
              PROJECT_ID: $PROJECT_ID # PROJECT_ID is the firebase project id

Step 6: Add Environment Variables to Bitbucket

  1. Go to your Bitbucket repository.
  2. Go to Repository settings > Repository variables.
  3. Make sure to check the Secured checkbox for the KEY_FILE and FIREBASE_TOKEN variables.
  4. Add the following environment variables:
    • KEY_FILE: Paste the base64-encoded service account key.
    • FIREBASE_TOKEN: Paste the Firebase token generated in Step 4.
    • PROJECT_ID: Your Firebase project ID.
  5. Click on Add to save the variables.
  6. Click on Save to apply the changes.

Step 7: Gradle Configuration

  1. In your project-level Gradle file, add the App Distribution plugin as a buildscript dependency.
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath 'com.google.firebase:firebase-appdistribution-gradle:3.0.3'
    }
}
  1. In your app-level Gradle file, apply the App Distribution plugin.
apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.appdistribution'
  1. Sync your project with the Gradle files.

Step 8: Add Testers And Release Notes

  1. Create two files in the root of your repository:

    • /app/src/release/release-notes.txt: Add release notes for the release build.
    • /app/src/release/testers.txt: Add a list of testers' email addresses for the release build.
    • here is an example of the content of the files:
        // Release Notes for Release Build
        ## Version: [Version Number]
    
        ### Release Date: [Release Date]
    
        ## New Features
    
        - [Feature 1]: [Brief description of the feature]
        - [Feature 2]: [Brief description of the feature]
    
        ## Improvements
    
        - [Improvement 1]: [Brief description of the improvement]
        - [Improvement 2]: [Brief description of the improvement]
    
        ## Bug Fixes
    
        - [Bug 1]: [Brief description of the bug fix]
        - [Bug 2]: [Brief description of the bug fix]
    
        ## Known Issues
    
        - [Issue 1]: [Brief description of the known issue]
        - [Issue 2]: [Brief description of the known issue]
    
        ## Deprecated Features
    
        - [Deprecated Feature 1]: [Brief description of the deprecated feature]
        - [Deprecated Feature 2]: [Brief description of the deprecated feature]
  2. Now Add the firebaseAppDistribution block to your app-level Gradle file to configure the release notes and testers.

buildTypes {

    debug {
        minifyEnabled false
        firebaseAppDistribution {
            releaseNotesFile = file('debug/releasenotes.txt')
            testersFile = file('debug/testers.txt')
        }
    }

    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        firebaseAppDistribution {
            releaseNotesFile = file('release/releasenotes.txt')
            testersFile = file('release/testers.txt')
        }
    }
}
  1. Sync your project with the Gradle files.

Step 9: Push Your Changes

  1. Commit and push your changes to the repository following MTK standards.
  2. Go to your Bitbucket repository.
  3. Navigate to Pipelines to see the build progress.
  4. Once the build is successful, you will see the app uploaded to Firebase App Distribution.
  5. You can now distribute the app to testers and view the release notes.
  6. Congratulations! You have successfully set up Bitbucket Pipelines and Firebase App Distribution for your Android project.
  7. You can now automate the build and deployment process for your Android app.
  8. You can also customize the pipeline to include linting, unit tests, and other checks as needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment