Powered By GitBook
building-android-apk-with-cloud-build-gradle-docker-image
Set up a Cloud Build trigger that builds your Android app and uploads it to a Cloud Storage bucket.
Contributed by the Google Cloud community. Not official Google documentation.
In this tutorial, you set up a Cloud Build trigger that builds your Android application and uploads it to a Cloud Storage bucket. The builds of new APK bundles are automatically triggered after code is pushed to your repository.

Objectives

Before you begin

    1.
    ​Create a new Google Cloud project, or use an existing one.

Costs

Cloud Build is free for up to the first 120 build minutes per day. Check the Cloud Build pricing page for details.

Get the sample code

Get the sample code from GitHub Gist.

Set up Cloud Storage

In this section, you create a Cloud Storage bucket, where your project APK files will be stored.
    1.
    Go to the Browser page for Cloud Storage.
    2.
    Click Create bucket.
    3.
    On the Create a bucket page, provide a unique bucket name.
    4.
    Click the Create button.
    5.
      1.
      On the Bucket details page, on the Permissions tab, enable bucket-level permissions.
      2.
      Click Add members.
      3.
      In the New members field, enter allUsers.
      4.
      In the Role field, choose Storage Object Viewer, and then click Save.
      (Note: Your bucket is public and can be accessed by anyone on the internet.)
    6.
    Grant Cloud Build access to Cloud Storage (optional, if bucket is public):
      1.
      Open the IAM page​
      in the GCP Console.
      2.
      Select your project and click Continue.
      3.
      In the list of members, look for your Cloud Build service account named
      [PROJECT_ID]@cloudbuild.gserviceaccount.com, where [PROJECT_ID] is your GCP project ID.
      4.
      Click the pencil icon in that row.
      5.
      Click Add another role, select Storage Object Admin under Storage, and click Save.

Set up Cloud Build

You need to ensure that your application code and all necessary files needed for building an APK are available in a code repository. Cloud Build currently supports Cloud Source Repositories, GitHub, and Bitbucket. You can also make use of the sample code for this tutorial, here.
In your repository, create a build configuration file, cloudbuild.yaml, which contains instructions for Cloud Build. The configuration file for this tutorial is as follows:
1
# cloudbuild.yaml
2
steps:
3
# Set a persistent volume according to https://cloud.google.com/cloud-build/docs/build-config (search for volumes)
4
- name: 'ubuntu'
5
volumes:
6
- name: 'vol1'
7
path: '/persistent_volume'
8
args: ['cp', '-a', '.', '/persistent_volume']
9
​
10
# Build APK with Gradle Image from mounted /persistent_volume using name: vol1
11
- name: 'gcr.io/cloud-builders/docker'
12
volumes:
13
- name: 'vol1'
14
path: '/persistent_volume'
15
args: ['run', '-v', 'vol1:/home/app', '--rm', 'gcr.io/fullstackgcp/gradle', '/bin/sh', '-c', 'cd /home/app && ./gradlew clean assembleDebug']
16
​
17
# Push the APK Output from vol1 to your GCS Bucket with Short Commit SHA.
18
- name: 'gcr.io/cloud-builders/gsutil'
19
volumes:
20
- name: 'vol1'
21
path: '/persistent_volume'
22
args: ['cp', '/persistent_volume/app/build/outputs/apk/debug/app-debug.apk', 'gs://fullstackgcp-apk-builds/app-debug-$SHORT_SHA.apk']
23
​
24
timeout: 1200s
Copied!
In a nutshell, Cloud Build helps you run the following Docker command:
1
docker run -v $(pwd):/home/app --rm gcr.io/fullstackgcp/gradle /bin/bash -c 'cd /home/app && ./gradlew clean assembleDebug'`
Copied!
In the command, we specify -v, which mounts our current directory as the volume, and --rm, which removes the container on exit.
You can change the -c command on your cloudbuild.yaml file if you would like to use other Gradle commands.
Cloud Build also copies the output app-debug.apk into your Cloud Storage bucket as app-debug-$SHORT_SHA.apk, where $SHORT_SHA is the first seven characters of COMMIT_SHA of the commit that triggered Cloud Build; it is meant to tag the APK builds in your GCS bucket.

Set up a Cloud Build trigger

A Cloud Build trigger listens to changes in your code repository. Follow the steps below to create a Cloud Build trigger:
    1.
    click Create Trigger.
    2.
    Enter a name for your trigger.
    3.
    Select the repository event to start your trigger.
    4.
    Select the repository that contains your source code and build
    configuration file.
    5.
    Specify the regular expression for the branch or tag name that will start your
    trigger.
    6.
    Choose Cloud Build configuration (yaml or json) as your build
    configuration.
    7.
    Click Create to save your build trigger.
    8.
    (Optional) To manually test a build trigger, click Run trigger on your trigger's entry in the triggers list.
Great! You have just configured Cloud Build for your code repository. On code push, it builds your Android APK and uploads to your Cloud Storage bucket.

Cleaning up

To prevent unnecessary charges, clean up the resources created for this tutorial.
    1.
    Delete the project used (if you created a new project).
    2.
    Delete the Cloud Build trigger and Cloud Storage bucket.
You might choose to disable the Cloud Build trigger, rather than deleting it.
You might also choose to pause the Cloud Scheduler job.

Next steps

If you want to learn more about Cloud Build, check out the following resources:
Last modified 6mo ago