Front-End Web & Mobile

Build a CI/CD Pipeline for your Android App with AWS Services

Continuously building, testing, and delivering updates for your app to your users helps you release new features sooner and with fewer bugs. In an earlier blog post “Automatically build your Android app with AWS CodeBuild,” Adrian Hall explains how you can use AWS CodeBuild to compile an Android app and publish the APK to Amazon S3.

In this blog post, we’ll be adding a continuous integration and continuous delivery (CI/CD) pipeline by using AWS CloudFormation, AWS CodePipeline, AWS CodeBuild, and AWS Device Farm. After you’ve completed the steps in this blog, you’ll have a working pipeline that will build your app, test it on real physical devices, and then deliver the packaged APK file to an S3 bucket each time a change is committed and pushed to the source code repository .

Create an Android project using Android Studio

Let’s start by creating a new project using Android Studio. If you already have a project you want to use, then you can skip this step. Otherwise, follow the steps on the Android Studio website to create a project. When you select the devices to target, choose Phone and Tablet. By default, a module named ‘app’ is created.

Create the CI/CD pipeline using AWS CloudFormation

Our pipeline uses multiple AWS services. We use an AWS CloudFormation template to provision all of those resources for us automatically. The pipeline consists of the following:

  • A CodeCommit repository to store the Android app’s code.
  • A CodeBuild project to compile, package, and deliver the Android app.
  • A Device Farm project to test the Android app on real physical devices.
  • An S3 bucket to store build artifacts.
  • A CodePipeline pipeline to monitor changes to the CodeCommit repository.  Then it triggers a build using CodeBuild and tests using Device Darm.
  • AWS Lambda functions to invoke the Device Farm test from CodePipeline, and also to provision the Device Darm project from AWS CloudFormation

Create the CloudFormation stack by following these steps:

  1. Open the AWS CloudFormation console.
  2. Click the Create Stack button.
  3. Download the Cloudformation template from https://github.com/aws-samples/aws-code-snippets/blob/master/CloudFormation/DeviceFarm.yaml.
  4. Select Upload a template to Amazon S3, then click on Choose File and select the template you downloaded in the previous step
  5. Type a stack name (for example, myapp-cicd-pipeline).
  6. Review the default parameters, and then click Next.  (Note that if you app’s module is not named ‘app’, you need to change the AppModuleName parameter).
  7. (Optional) Set any options that you need in Options, and then click Next.
  8. Select I acknowledge that AWS CloudFormation might create IAM resources.  (The stack uses IAM roles for Lambda, CodeBuild, and CodePipeline.)
  9. Click the Create button.

AWS CloudFormation creates your stack. After it’s finished, the stack is in the ‘CREATE_COMPLETE’ status. Click the stack in the list of CloudFormation stacks, and then click the Outputs tab on the bottom half of the screen. The stack outputs two values, which we use in the upcoming steps. They are CodeRepoCloneUrlHttp, which contains the Git URL that we push code to, and OutputApkUrl, which contains the URL to the latest built and tested APK file.

Configure the project to use CodeCommit

If your development environment isn’t already configured for AWS CodeCommit, you need to follow the setup steps in the AWS CodeCommit User Guide to install the Git client, grant CodeCommit permissions to your IAM user, and set up the credential helper. You can skip step 4 because we’re configuring an existing project to use the Git repository.

To configure your Android project to use CodeCommit, you need to add the CodeCommit repository that was created by the CloudFormation stack as a Git remote. Use the CodeRepoCloneUrlHttp output value from the previous step, and run the following command from your project’s workspace:

git remote add codecommit <CodeRepoCloneUrlHttp from the stack output>

Push the code to CodeCommit

We’re now ready to push the source code for your Android app to the CodeCommit repository. Doing this triggers an execution on the CodePipeline pipeline. A couple minutes after you’ve pushed the code, you’ll have a fully tested APK file in the output S3 bucket.

From your Android app’s workspace, you need to make sure that all of the files have been checked into the Git repository.

Add all of the files in your app’s workspace:

git add -A

Commit the changes to your local Git repository:

git commit -a -m "Initial checkin of the code"

Push the changes to the CodeCommit repository:

git push codecommit

View the build

To view the progress of the CodePipeline execution, you need to open the CodePipeline console. Find the pipeline that has the stack name of the CloudFormation stack that you created as the prefix and click the name. From here, you can see the progress of the pipeline execution.

The execution goes through four stages (Source, Build, Test, and Deliver).

  • Source: The latest code is taken from the CodeCommit repository and packaged as a ZIP file for use by other stages.
  • Build: CodePipeline triggers a CodeBuild build to compile and package the app as an APK file.
  • Test: CodePipeline invokes a Lambda function that uploads the APK file to Device Farm and runs a test on real devices.
  • Deliver: The built and tested APK file is delivered to the specified S3 bucket

You can view the Device Farm test results from the Device Farm console. Click the project named demo-app-devicefarm if you used the default settings when you created the stack. Otherwise, click the name of the project that you specified in the CloudFormation stack parameters. Under the Automated tests tab, you see a test if it’s currently running or has finished. If you don’t see a test, the Test stage might not yet have been executed by CodePipeline. After the test has finished, you can view the results and also screenshots from the test.

The final stage of the pipeline is to deliver the artifact to Amazon S3. Open the Amazon S3 console  and find the artifact bucket. If your stack’s prefix is ‘myapp-cicd-pipeline‘, then the artifact bucket has a prefix of ‘myapp-cicd-pipeline-artifactbucket-‘. After the pipeline completes the execution, if the build and test phases completed successfully, an APK file is delivered in this S3 bucket.

The CloudFormation stack output has an output with a key named OutputApkUrl. The value contains a URL that you can use to download the APK file directly. You can also use the URL to install the APK file on an Android device.

Next Steps

We’ve automated the building, testing, and delivery for an Android app by using AWS services. With a little work, you can modify the AWS CloudFormation template to also sign the app using your certificate, and deliver it directly to the Google Play store. To automate the delivery to the Google Play store, you can use tools such as fastlane. You can invoke these tools from CodeBuild.

About the Author

This post was contributed by Vinay Selvaraj. Vinay is a Consultant with AWS Professional Services who specializes in helping customers migrate to the cloud. You can contact him on Twitter at @vinayselvaraj.