Menu

Language Guide: Android

Tutorials & 2.0 Demo Apps > Language Guide: Android

This document describes how to set up an Android project on CircleCI in the following sections.

Overview

This guide provides an introduction to Android development on CircleCI. If you are looking for a .circleci/config.yml template for Android, see the Sample Configuration section of this document.

Note: Running the Android emulator is not supported by the type of virtualization CircleCI uses on Linux. To run emulator tests from a job, consider using an external service like Firebase Test Lab. For more details, see the Testing With Firebase Test Lab section below.

Prerequisites

This guide assumes the following:

  • You are using Gradle to build your Android project. Gradle is the default build tool for projects created with Android Studio.
  • Your project is located in the root of your VCS repository.
  • The project’s application is located in a subfolder named app.

Sample Configuration

version: 2
jobs:
  build:
    working_directory: ~/code
    docker:
      - image: circleci/android:api-25-alpha
    environment:
      JVM_OPTS: -Xmx3200m
    steps:
      - checkout
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
#      - run:
#         name: Chmod permissions #if permission for Gradlew Dependencies fail, use this.
#         command: sudo chmod +x ./gradlew
      - run:
          name: Download Dependencies
          command: ./gradlew androidDependencies
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
      - run:
          name: Run Tests
          command: ./gradlew lint test
      - store_artifacts:
          path: app/build/reports
          destination: reports
      - store_test_results:
          path: app/build/test-results
      # See https://circleci.com/docs/2.0/deployment-integrations/ for deploy examples

Config Walkthrough

We always start with the version.

version: 2

Next, we have a jobs key. Each job represents a phase in your Build-Test-Deploy process. Our sample app only needs a build job, so everything else is going to live under that key.

In each job, we have the option of specifying a working_directory. This is the directory into which our code will be checked out, and this path will be used as the default working directory for the rest of the job unless otherwise specified.

jobs:
  build:
    working_directory: ~/code

Directly beneath working_directory, we can specify container images under a docker key.

    docker:
      - image: circleci/android:api-25-alpha

We use the CircleCI-provided Android image with the api-25-alpha tag. See Docker Images below for more information about what images are available.

Now we’ll add several steps within the build job.

We start with checkout so we can operate on the codebase.

Next we pull down the cache, if present. If this is your first run, or if you’ve changed either of your build.gradle files, this won’t do anything. We run ./gradlew androidDependencies next to pull down the project’s dependencies. Normally you never call this task directly since it’s done automatically when it’s needed, but calling it directly allows us to insert a save_cache step that will store the dependencies in order to speed things up for next time.

Then ./gradlew lint test runs the unit tests, and runs the built in linting tools to check your code for style issues.

We then upload the build reports as job artifacts, and we upload the test metadata (XML) for CircleCI to process.

Docker Images

For convenience, CircleCI provides a set of Docker images for building Android apps. These pre-built images are available in the CircleCI org on Docker Hub. The source code and Dockerfiles for these images are available in this GitHub repository.

The CircleCI Android image is based on the openjdk:8-jdk official Docker image, which is based on buildpack-deps. The base OS is Debian Jessie, and builds run as the circleci user, which has full access to passwordless sudo.

API Levels

We have a different Docker image for each Android API level. To use API level 24 (Nougat 7.0) in a job, you should select circleci/android:api-24-alpha.

Alpha Tag

Our Android Docker images are currently tagged with the suffix -alpha. This is to indicate the images are currently under development and might change in backwards incompatible ways from week to week.

Customizing the Images

We welcome contributions on our GitHub repo for the Android image. Our goal is provide a base image that has most of the tools you need; we do not plan to provide every tool that you might need.

To customize the image, create a Dockerfile that builds FROM the circleci/android image. See Using Custom-Built Docker Images for instructions.

React Native projects

React Native projects can be built on CircleCI 2.0 using Linux, Android and macOS capabilities. Please check out this example React Native application on GitHub for a full example of a React Native project.

Testing With Firebase Test Lab

To use Firebase Test Lab with CircleCI, first complete the following steps.

  1. Create a Firebase project. Follow the instructions in the Firebase documentation.

  2. Install and authorize the Google Cloud SDK. Follow the instructions in the Authorizing the Google Cloud SDK document.

    Note: Instead of google/cloud-sdk, consider using an Android convenience image, which includes gcloud and Android-specific tools.

  3. Enable required APIs. Using the service account you created, log into Google and go to the Google Developers Console API Library page. Enable the Google Cloud Testing API and the Cloud Tool Results API by typing their names into the search box at the top of the console and clicking Enable API.

In your .circleci/config.yml file, add the following run steps.

  1. Build the debug APK and test APK. Use Gradle to build two APKs. To improve build performance, consider disabling pre-dexing.

  2. Store the service account. Store the service account you created in a local JSON file.

  3. Authorize gcloud. Authorize the gcloud tool and set the default project.

  4. Use gcloud to test with Firebase Test Lab. Adjust the paths to the APK files to correspond to your project.

  5. Install crcmod and use gsutil to copy test results data. crcmod is required to use gsutil. Use gsutil to download the newest files in the bucket to the CircleCI artifacts folder. Be sure to replace BUCKET_NAME and OBJECT_NAME with project-specific names.

version: 2
jobs:
  test:
    docker:
      - image: circleci/android:api-28-alpha  # gcloud is baked into this image
    steps:
      - run:
          name: Build debug APK and release APK
          command: |
            ./gradlew :app:assembleDebug
            ./gradlew :app:assembleDebugAndroidTest
      - run:
          name: Store Google Service Account
          command: echo $GCLOUD_SERVICE_KEY > ${HOME}/gcloud-service-key.json
      - run:
          name: Authorize gcloud and set config defaults
          command: |
            sudo gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json
            sudo gcloud --quiet config set project ${GOOGLE_PROJECT_ID}
      - run:
          name: Test with Firebase Test Lab
          command: >
            sudo gcloud firebase test android run
              --app <local_server_path>/<app_apk>.apk
              --test <local_server_path>/<app_test_apk>.apk
              --results-bucket cloud-test-${GOOGLE_PROJECT_ID}
      - run:
          name: Install gsutil dependency and copy test results data
          command: |
            sudo pip install -U crcmod
            sudo gsutil -m cp -r -U `sudo gsutil ls gs://[BUCKET_NAME]/[OBJECT_NAME] | tail -1` ${CIRCLE_ARTIFACTS}/ | true

For more details on using gcloud to run Firebase, see the official documentation.

Disabling Pre-Dexing to Improve Build Performance

Pre-dexing dependencies has no benefit on CircleCI. To disable pre-dexing, refer to this blog post.

By default, the Gradle Android plugin pre-dexes dependencies. Pre-dexing speeds up development by converting Java bytecode into Android bytecode, allowing incremental dexing as you change code. CircleCI runs clean builds, so pre-dexing actually increases compilation time and may also increase memory usage.

Deployment

See the Deploy document for examples of deploy target configurations.