Skip to main content

Command Palette

Search for a command to run...

Setting Up an Android CI/CD Pipeline in Azure DevOps

Updated
4 min read
Setting Up an Android CI/CD Pipeline in Azure DevOps

In this blog, I'll guide you through setting up an Android Continuous Integration and Continuous Deployment (CI/CD) pipeline in Azure DevOps. This pipeline automates tasks like building the project using Gradle, signing the Android App Bundle (AAB), and publishing build artifacts. Here's the YAML configuration breakdown:

1. Pipeline Trigger

The pipeline starts automatically when code is pushed to the main branch. This is configured using the trigger directive.

trigger:
  branches:
    include:
      - main

Whenever a new change is pushed to the main branch, the pipeline will be triggered.

2. Pipeline Stages and Jobs

Azure DevOps pipelines are divided into stages, jobs, and steps. The following pipeline has a single stage with one job that performs a series of tasks like building, signing, and publishing the Android build artifacts.

stages:
  - stage: __default
    jobs:
      - job: Job
        pool:
          vmImage: 'ubuntu-latest'

The pipeline runs on an Ubuntu virtual machine using the latest image provided by Microsoft-hosted agents.

3. Building the Project with Gradle

Gradle is the build automation tool used for Android projects. In this step, we are using the Gradle task to build the release variant and the bundle of the Android project.

steps:
  - task: Gradle@3
    inputs:
      gradleWrapperFile: 'gradlew'
      workingDirectory: '.'
      publishJUnitResults: false
      javaHomeOption: 'JDKVersion'
      jdkVersionOption: '17'
      sonarQubeRunAnalysis: false
      spotBugsAnalysis: false
      gradleOptions: '-Xmx4072m'
      tasks: 'assembleRelease bundleRelease'
  • gradleWrapperFile: Specifies the Gradle wrapper file.

  • javaHomeOption and jdkVersionOption: Set the Java Development Kit (JDK) version to 17.

  • gradleOptions: Increases the maximum heap size to avoid memory issues during the build process.

  • tasks: assembleRelease and bundleRelease build both APK and AAB files, respectively.

4. Signing the Android App Bundle (AAB)

The AAB generated by the Gradle task needs to be signed before it can be uploaded to the Google Play Store. Here, we use the AndroidSigning@3 task to sign the AAB.

- task: AndroidSigning@3
  displayName: 'Sign AAB'
  inputs:
    apkFiles: '**/*.aab'
    apksignerKeystoreFile: 'lms-release-key.keystore'
    apksignerKeystorePassword: '$(keystorePassword)'
    apksignerKeystoreAlias: '$(keystoreAlias)'
    apksignerKeyPassword: '$(keyPassword)'
    apksignerArguments: '--min-sdk-version 23'
  • apkFiles: The task automatically locates all .aab files.

  • apksignerKeystoreFile: Specifies the keystore file for signing.

  • apksignerKeystorePassword, apksignerKeystoreAlias, apksignerKeyPassword: Sensitive information like keystore credentials is stored securely using pipeline variables.

  • apksignerArguments: The minimum SDK version is set to 23 using custom arguments.

5. Copying the Build Artifacts

After building and signing, the APK and AAB files need to be stored for later use, such as publishing to the Play Store or further testing.

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(System.DefaultWorkingDirectory)/app/build/'
    Contents: |
      outputs/apk/production/release/**/*.apk
      outputs/bundle/productionRelease/app-production-release.aab
    TargetFolder: '$(Build.ArtifactStagingDirectory)'
  • SourceFolder: Specifies the location of the APK and AAB files in the build directory.

  • Contents: Defines the specific APK and AAB files to copy.

  • TargetFolder: Specifies where to copy the files, in this case, to the $(Build.ArtifactStagingDirectory) for further use.

6. Publishing Build Artifacts

Once the build artifacts are copied, the PublishBuildArtifacts task publishes them to Azure DevOps, allowing them to be downloaded from the pipeline's summary page.

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: Container
  • PathtoPublish: Points to the directory where the APK and AAB files are stored.

  • ArtifactName: Provides a name for the published artifacts (drop in this case).

  • publishLocation: Publishes the files to an Azure DevOps container for easy retrieval.

Here’s the complete pipeline script for automating your Android build process in Azure DevOps. The script includes building the project using Gradle, signing the Android App Bundle (AAB), copying the necessary build artifacts, and publishing them.

trigger:
  branches:
    include:
      - main

stages:
  - stage: __default
    jobs:
      - job: Job
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          # Step 1: Build with Gradle
          - task: Gradle@3
            inputs:
              gradleWrapperFile: 'gradlew'
              workingDirectory: '.'
              publishJUnitResults: false
              javaHomeOption: 'JDKVersion'
              jdkVersionOption: '17'
              sonarQubeRunAnalysis: false
              spotBugsAnalysis: false
              gradleOptions: '-Xmx4072m'
              tasks: 'assembleRelease bundleRelease'

          # Step 2: Sign the AAB
          - task: AndroidSigning@3
            displayName: 'Sign AAB'
            inputs:
              apkFiles: '**/*.aab'
              apksignerKeystoreFile: 'lms-release-key.keystore'
              apksignerKeystorePassword: '$(keystorePassword)'
              apksignerKeystoreAlias: '$(keystoreAlias)'
              apksignerKeyPassword: '$(keyPassword)'
              apksignerArguments: '--min-sdk-version 23'

          # Step 3: Copy APK and AAB files
          - task: CopyFiles@2
            inputs:
              SourceFolder: '$(System.DefaultWorkingDirectory)/app/build/'
              Contents: |
                outputs/apk/production/release/**/*.apk
                outputs/bundle/productionRelease/app-production-release.aab
              TargetFolder: '$(Build.ArtifactStagingDirectory)'

          # Step 4: Publish the build artifacts
          - task: PublishBuildArtifacts@1
            inputs:
              PathtoPublish: '$(Build.ArtifactStagingDirectory)'
              ArtifactName: 'drop'
              publishLocation: Container

Key Sections:

  1. Trigger: Automatically triggers the pipeline when a change is made to the main branch.

  2. Gradle Build: Executes the Gradle tasks assembleRelease and bundleRelease, generating the APK and AAB files.

  3. Android Signing: Signs the AAB using the provided keystore and credentials.

  4. Copy Files: Copies the APK and AAB from the build folder to the artifact staging directory.

  5. Publish Artifacts: Publishes the build artifacts so they can be downloaded from the Azure DevOps pipeline summary.

More from this blog

DevOps Journey with M Hassan

174 posts

I am writing these blogs because I recently completed a comprehensive DevOps course where I gained in-depth knowledge of the topics mentioned. As I progressed through the course, I realized the importance of having a concise and accessible resource to revise and reinforce my understanding of each topic. Therefore, I decided to create cheat sheets in the form of blog posts. These cheat sheets will not only serve as a handy reference for myself but also benefit others who are also interested in mastering DevOps concepts. By documenting each topic and providing concise explanations, I aim to create a valuable resource that simplifies complex concepts and facilitates hands-on practice. This way, I can solidify my own understanding while helping others on their DevOps journey.