Build, test, & deploy Android apps

Azure DevOps Services

You can set up pipelines to automatically build, test, and deploy Android applications.

Prerequisites

You must have the following items:

Set up pipeline

Do the following tasks to set up a pipeline for a sample Android application.

  1. Fork the following repository to your GitHub account to get the code for a simple Android application.

    https://github.com/MicrosoftDocs/pipelines-android
    
  2. Sign in to your Azure DevOps organization and go to your project.

  3. Select Pipelines > Create pipeline or New pipeline.

  4. Select GitHub as the location of your source code.

    Screenshot showing list of repositories to select from.

    You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.

  5. Select the -android repository that you previously forked.

  6. Select Approve and install on the screen that follows.

    Azure Pipelines generates a YAML file for your pipeline.

  7. Select Run.

  8. Commit directly to the main branch, and then choose Run again.

  9. Wait for the run to finish.

You have a working YAML file (azure-pipelines.yml) in your repository that's ready for you to customize.

Tip

To make changes to the YAML file, select the pipeline in the Pipelines page, and then Edit the azure-pipelines.yml file.

Build with Gradle

Gradle is a common build tool used for building Android projects. For more information about your options, see the Gradle task.

# https://learn.microsoft.com/azure/devops/pipelines/ecosystems/android
pool:
  vmImage: 'macOS-latest'

steps:
- task: Gradle@2
  inputs:
    workingDirectory: ''
    gradleWrapperFile: 'gradlew'
    gradleOptions: '-Xmx3072m'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'
    tasks: 'assembleDebug'

Adjust the build path

  • Adjust the workingDirectory value if your gradlew file isn't in the root of the repository. The directory value should be similar to the root of the repository, such as AndroidApps/MyApp or $(system.defaultWorkingDirectory)/AndroidApps/MyApp.

  • Adjust the gradleWrapperFile value if your gradlew file isn't in the root of the repository. The file path value should be similar to the root of the repository, such as AndroidApps/MyApp/gradlew or $(system.defaultWorkingDirectory)/AndroidApps/MyApp/gradlew.

Adjust Gradle tasks

Adjust the tasks value for the build variant you prefer, such as assembleDebug or assembleRelease. For more information, see the following Google Android development documentation:

Sign and align an Android Package (APK)

If your build doesn't already sign and zipalign the APK, add the Android Signing task to the YAML. An APK must be signed to run on a device instead of an emulator. Zipaligning reduces the RAM consumed by the application.

Important

We recommend storing each of the following passwords in a secret variable.

- task: AndroidSigning@2
  inputs:
    apkFiles: '**/*.apk'
    jarsign: true
    jarsignerKeystoreFile: 'pathToYourKeystoreFile'
    jarsignerKeystorePassword: '$(jarsignerKeystorePassword)'
    jarsignerKeystoreAlias: 'yourKeystoreAlias'
    jarsignerKeyPassword: '$(jarsignerKeyPassword)'
    zipalign: true

Test

Test on the Android Emulator

Note

The Android Emulator is currently available only on the Hosted macOS agent.

Create the Bash task and copy paste the code below in order to install and run the emulator. Don't forget to arrange the emulator parameters to fit your testing environment. The emulator starts as a background process and is available in later tasks.

#!/usr/bin/env bash

# Install AVD files
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'system-images;android-27;google_apis;x86'

# Create emulator
echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n xamarin_android_emulator -k 'system-images;android-27;google_apis;x86' --force

$ANDROID_HOME/emulator/emulator -list-avds

echo "Starting emulator"

# Start emulator in background
nohup $ANDROID_HOME/emulator/emulator -avd xamarin_android_emulator -no-snapshot > /dev/null 2>&1 &
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d '\r') ]]; do sleep 1; done; input keyevent 82'

$ANDROID_HOME/platform-tools/adb devices

echo "Emulator started"

Test on Azure-hosted devices

Add the App Center Test task to test the application in a hosted lab of iOS and Android devices. An App Center free trial is required, which must later be converted to paid.

Sign up with App Center first.

# App Center test
# Test app packages with Visual Studio App Center
- task: AppCenterTest@1
  inputs:
    appFile: 
    #artifactsDirectory: '$(Build.ArtifactStagingDirectory)/AppCenterTest' 
    #prepareTests: true # Optional
    #frameworkOption: 'appium' # Required when prepareTests == True# Options: appium, espresso, calabash, uitest, xcuitest
    #appiumBuildDirectory: # Required when prepareTests == True && Framework == Appium
    #espressoBuildDirectory: # Optional
    #espressoTestApkFile: # Optional
    #calabashProjectDirectory: # Required when prepareTests == True && Framework == Calabash
    #calabashConfigFile: # Optional
    #calabashProfile: # Optional
    #calabashSkipConfigCheck: # Optional
    #uiTestBuildDirectory: # Required when prepareTests == True && Framework == Uitest
    #uitestStorePath: # Optional
    #uiTestStorePassword: # Optional
    #uitestKeyAlias: # Optional
    #uiTestKeyPassword: # Optional
    #uiTestToolsDirectory: # Optional
    #signInfo: # Optional
    #xcUITestBuildDirectory: # Optional
    #xcUITestIpaFile: # Optional
    #prepareOptions: # Optional
    #runTests: true # Optional
    #credentialsOption: 'serviceEndpoint' # Required when runTests == True# Options: serviceEndpoint, inputs
    #serverEndpoint: # Required when runTests == True && CredsType == ServiceEndpoint
    #username: # Required when runTests == True && CredsType == Inputs
    #password: # Required when runTests == True && CredsType == Inputs
    #appSlug: # Required when runTests == True
    #devices: # Required when runTests == True
    #series: 'master' # Optional
    #dsymDirectory: # Optional
    #localeOption: 'en_US' # Required when runTests == True# Options: da_DK, nl_NL, en_GB, en_US, fr_FR, de_DE, ja_JP, ru_RU, es_MX, es_ES, user
    #userDefinedLocale: # Optional
    #loginOptions: # Optional
    #runOptions: # Optional
    #skipWaitingForResults: # Optional
    #cliFile: # Optional
    #showDebugOutput: # Optional

Keep artifacts with the build record

Add the Copy Files and Publish Build Artifacts tasks. Your APK gets stored with the build record or test, and gets deployed in later pipelines. For more information, see Artifacts.

- task: CopyFiles@2
  inputs:
    contents: '**/*.apk'
    targetFolder: '$(build.artifactStagingDirectory)'
- task: PublishBuildArtifacts@1

Deploy

Add App Center

Add the App Center Distribute task to distribute an application to a group of testers or beta users, or promote the application to Intune or Google Play. A free App Center account is required (no payment is necessary).

# App Center distribute
# Distribute app builds to testers and users via Visual Studio App Center
- task: AppCenterDistribute@1
  inputs:
    serverEndpoint: 
    appSlug: 
    appFile: 
    #symbolsOption: 'Apple' # Optional. Options: apple
    #symbolsPath: # Optional
    #symbolsPdbFiles: '**/*.pdb' # Optional
    #symbolsDsymFiles: # Optional
    #symbolsMappingTxtFile: # Optional
    #symbolsIncludeParentDirectory: # Optional
    #releaseNotesOption: 'input' # Options: input, file
    #releaseNotesInput: # Required when releaseNotesOption == Input
    #releaseNotesFile: # Required when releaseNotesOption == File
    #isMandatory: false # Optional
    #distributionGroupId: # Optional

Install Google Play

Install the Google Play extension and use the following tasks to automate interaction with Google Play. By default, these tasks authenticate to Google Play using a service connection that you configure.

Release

Add the Google Play Release task to release a new Android app version to the Google Play store.

- task: GooglePlayRelease@4
  inputs:
    apkFile: '**/*.apk'
    serviceEndpoint: 'yourGooglePlayServiceConnectionName'
    track: 'internal'

Promote

Add the Google Play Promote task to promote a previously released Android application update from one track to another, such as alphabeta.

- task: GooglePlayPromote@3
  inputs:
    packageName: 'com.yourCompany.appPackageName'
    serviceEndpoint: 'yourGooglePlayServiceConnectionName'
    sourceTrack: 'internal'
    destinationTrack: 'alpha'

Increase rollout

Add the Google Play Increase Rollout task to increase the rollout percentage of an application that was previously released to the rollout track.

- task: GooglePlayIncreaseRollout@2
  inputs:
    packageName: 'com.yourCompany.appPackageName'
    serviceEndpoint: 'yourGooglePlayServiceConnectionName'
    userFraction: '0.5' # 0.0 to 1.0 (0% to 100%)

Status update

Add the Google Play Status Update task to update the rollout status for the application that was previously released to the rollout track.

  - task: GooglePlayStatusUpdate@2
    inputs:
      authType: ServiceEndpoint
      packageName: 'com.yourCompany.appPackageName'
      serviceEndpoint: 'yourGooglePlayServiceConnectionName'
      status: 'inProgress' # draft | inProgress | halted | completed

FAQ

Q: How do I create app bundles?

A: You can build and sign your app bundle with an inline script and a secure file. To do so, first download your keystore and store it as a secure file in the Library. Then, create variables for keystore.password, key.alias, and key.password in a variable group.

Next, use the Download Secure File and Bash tasks to download your keystore and build and sign your app bundle.

In this YAML file, download an app.keystore secure file and use a bash script to generate an app bundle. Then, use Copy Files to copy the app bundle. From there, create and save an artifact with Publish Build Artifact or use the Google Play extension to publish.

- task: DownloadSecureFile@1
  name: keyStore
  displayName: "Download keystore from secure files"
  inputs:
    secureFile: app.keystore

- task: Bash@3
  displayName: "Build and sign App Bundle"
  inputs:
    targetType: "inline"
    script: |
      msbuild -restore $(Build.SourcesDirectory)/myAndroidApp/*.csproj -t:SignAndroidPackage -p:AndroidPackageFormat=aab -p:Configuration=$(buildConfiguration) -p:AndroidKeyStore=True -p:AndroidSigningKeyStore=$(keyStore.secureFilePath) -p:AndroidSigningStorePass=$(keystore.password) -p:AndroidSigningKeyAlias=$(key.alias) -p:AndroidSigningKeyPass=$(key.password)

- task: CopyFiles@2
  displayName: 'Copy deliverables'
  inputs:
    SourceFolder: '$(Build.SourcesDirectory)/myAndroidApp/bin/$(buildConfiguration)'
    Contents: '*.aab'
    TargetFolder: 'drop'