Fastlane + CircleCI for Android — Part 1

Chan Bo
Open Knowledge
Published in
4 min readMar 18, 2019

As I already mention already in my previous story that I will implement Fastlane with CircleCI for Android App Development. So in this story I will implement it as an example from steps by steps so you can keep it as references for your next project.

In this example, I’m going to implement CircleCI + Fastlane to android application. Instead of using CircleCI to build and test android app, I will use CircleCI to called Fastlane build, test and deploy when it is triggered build.

Workflows

I will separate android build variants to 3 different type:

  1. dev variant: With this variant I will do the jobs as:
  • Build Application
  • Run Unit Test
  • Run Instrumentation Test with Firebase Test Lab
  • Send debug or release apk to Slack

2. beta variant: With this variant I will do the jobs like in dev variant but to add:

  • Deploy to Crashlytics (Fabric)
  • Deploy to Google Play with Beta track

3. pro variant: With this variant I will do the jobs like in beta variant but change from Deploy to Google Play with Beta track to Deploy to Google Play with Production track.

I will have 3 branches in Github repository:

  • master (pro variant)
  • beta (beta variant)
  • dev (dev variant)

Getting Started

Let’s start create new android project and config CircleCI and Fastlane as I mention above.

Fastlane

We will start config with Fastfile first. If you are not clear about how to add fastlane into your project, you can look at this story.

Since we have different build variants so our build.gradle file should look like:

Now let’s start config Fastfile . We will create lane to run build, test and deploy. Then every lane should have parameters for build type (Debug, Release) and build flavor (dev, beta, pro).

Build App

we will create lane name assemble_build to build our app.

|options| is your lane parameters. For more about lane parameter, you look at Advance Lanes.

To run assemble_build lane with parameters above we can run:

$ bundle exec fastlane assemble_build build_flavor:"YourBuildFlavor" build_type:"YourBuildType"

Unit Test

We will create lane name unit_tests to run unit test of app

Instrumentation Test

We will create lane name instrumentation_test_testlab to run UI test. We will use Firebase Test Lab to run UI test. To use Firebase Test Lab in your project:

  1. Set up Firebase following this Instruction.
  2. Go to Google Cloud Console.
  3. Select your project
  4. Click on IAM & admin > Service accounts
  5. Click on CREAET SERVER ACCOUNT > fill in Servcie Account Name > CREATE

6. Select Project Owner > Continue

7. Click on CREATE KEY > Choose JOSN > Create

Now that we have JSON file from Google Cloud, then we just need to add and configure the Fastlane plugin.

  • Add the plugin: $ bundle exec fastlane add_plugin run_tests_firebase_testlab
  • Create new lane in Fastfile

You can see available devices here and if you want to check all the parameters that you can use, just take a look at the README file.

Now, to run the tests in Firebase TestLab, the plugin need three things: app apk, android test apk and account auth key.

Since we have created assemble_build , so we can build both app apk and android test apk.

Now let’s take a look at code below:

@app_apk and @android_test_apk : are variables to store apk after we build app

Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH] : It’s fastlane gradle output variable. We can get apk path from it. For more detail you can run: $ bundle exec fastlane action gradle

To run Firebase Test Lab with fastlane, we have another step to finish:

  • Enable Cloud tools result API for your project.
  • Install Google Cloud SDK on your computer (If you want to run it locally without CircleCI)
  • Provide Google Auth Key: run_tests_firebase_testlab plugin have created an environment variable name GCLOUD_SERVICE_KEY so we can use it in CircleCI environment variable. You will see soon when we implement CircleCI in this example, so don’t worry. But if you want to run it locally, you can create .env file in your root project and add GCLOUD_SERVICE_KEY='your_google_service_account_json_file_content' into .env file.

Note: Don’t commit your .env file to your repository.

So now you can run Instrumentation Test via: $ bundle exec fastlane instrumentation_tests_testlab .

When the tests finish running, you will have folder called firebase/ . 🎺

That’s all for now! In the next blog, I will continue implement this example by integrating fastlane to Slack so you can send message or your apk to slack. 😎 If you have any question or problem, please let me know! ⭐️ Thanks 👍

--

--