Skip to main content

Android

You can build and test Android applications using a Linux or Mac platform on Harness Cloud, a self-managed Kubernetes cluster, or a local runner build infrastructure.

This guide assumes you've created a Harness CI pipeline.

Specify architecture

You can build and test Android apps on a Linux or Mac platform on Harness Cloud build infrastructure.

stages:
- stage:
name: Build
identifier: Build
type: CI
spec:
cloneCodebase: true
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}

Install dependencies

Many Android packages, such as command-line tools and an emulator, are already installed on Harness Cloud Linux machines. For more information about preinstalled tools and libraries, go to the Harness Cloud image specifications.

Use Run steps to install dependencies in the build environment.

- step:
type: Run
identifier: dependencies
name: dependencies
spec:
shell: Sh
command: |-
bundle update

Cache dependencies

Add caching to your stage.

Use Cache Intelligence by adding caching to your stage.spec:

- stage:
spec:
caching:
enabled: true
paths:
- YOUR_CACHE_PATH
sharedPaths:
- YOUR_CACHE_PATH
tip

Cache Intelligence supports Gradle (as well as additional build tools). If you're using Gradle and your dependencies are stored in the default location for Gradle, you don't need to include paths or sharedPath. For example:

- stage:
spec:
caching:
enabled: true

Build and run tests

You can use Run or Test steps to run tests in Harness CI.

If you're using Kotlin, you can improve your unit test times with Harness' Test Intelligence feature.

Harness CI also supports test splitting (parallelism) for both Run and Test steps.

- step:
type: Run
name: build and test
identifier: build_and_test
spec:
shell: Sh
command: |-
./gradlew test assemble -YOUR_PROJECT
./gradlew testDebug

If you want to view test results in Harness, make sure your test commands produce reports in JUnit XML format and that your steps include the reports specification.

- step:
type: Run
name: build and test
identifier: build_and_test
spec:
shell: Sh
command: |-
./gradlew test assemble -YOUR_PROJECT
./gradlew testDebug
reports:
type: JUnit
spec:
paths:
- "*/build/test-results/.*xml"

Specify version

Android packages, including Android SDK tools and and fastlane, are pre-installed on Harness Cloud machines. For details about all available tools and versions, go to Platforms and image specifications.

- step:
type: Run
name: android version
identifier: android_version
spec:
shell: Sh
command: |-
fastlane init
fastlane tests

If you need to install additional versions, use a Run step. These examples use faberNovel/docker-android and fastlane.

Use one version
- step:
type: Run
name: android version
identifier: android_version
spec:
connectorRef: account.harnessImage
image: fabernovel/android:api-30-v1.7.0
shell: Sh
command: |-
fastlane init
fastlane tests
Use multiple versions
  1. Add the matrix looping strategy configuration to your stage.
- stage:
strategy:
matrix:
androidVersion:
- 30
- 31
- 33
  1. Reference the matrix variable in the image field of your steps.
- step:
type: Run
name: android Version
identifier: android_version
spec:
connectorRef: account.harnessImage
image: fabernovel/android:api-<+ stage.matrix.androidVersion >-v1.7.0
shell: Sh
command: |-
fastlane init
fastlane tests

Deploy to the Google Play Store

The following examples use fastlane to deploy an app to the Google Play Store. These are intended as examples only. They do not provide complete firebase configuration or app distribution requirements. To learn more about app distribution, go to the Google documentation on Firebase App Distribution and the fastlane documentation on Deploying to Google Play using fastlane.

- step:
type: Run
name: fastlane_deploy
identifier: fastlane_deploy
spec:
shell: Sh
command: |-
fastlane init
fastlane android deploy
fastlane action upload_to_play_store

Full pipeline examples

The following pipeline examples install dependencies, cache dependencies, and build and test an Android app.

This pipeline uses Harness Cloud build infrastructure and Cache Intelligence.

If you copy this example, replace the placeholder values with appropriate values for your code repo connector, repository name, and other applicable values. Depending on your project and organization, you may also need to replace projectIdentifier and orgIdentifier.

pipeline:
name: default
identifier: default
projectIdentifier: default
orgIdentifier: default
properties:
ci:
codebase:
connectorRef: YOUR_CODE_REPO_CONNECTOR_ID
repoName: YOUR_REPO_NAME
build: <+input>
tags: {}
stages:
- stage:
name: build
identifier: build
description: ""
type: CI
spec:
cloneCodebase: true
caching:
enabled: true
paths:
- "YOUR_CACHE_PATH"
execution:
steps:
- step:
type: Run
identifier: dependencies
name: dependencies
spec:
shell: Sh
command: |-
bundle update
- step:
type: Run
name: Test
identifier: test
spec:
shell: Sh
command: |-
./gradlew test assemble -YOUR_PROJECT
./gradlew testDebug
reports:
type: JUnit
spec:
paths:
- "*/build/test-results/.*xml"
- step:
type: Run
name: fastlane_deploy
identifier: fastlane_deploy
spec:
shell: Sh
command: |-
fastlane init
fastlane android deploy
fastlane action upload_to_play_store
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}
sharedPaths:
- YOUR_CACHE_PATH

Next steps

Now that you have created a pipeline that builds and tests an Android app, you could: