After successfully reducing iOS beta distribution to a single command, I finally got around to doing a tutorial for the Android version. Once again I’ll be using Fastlane and the growing Android toolset to distribute to beta and complete other useful tasks.

As described in the previous post, Fastlane is:

‘[A] set of tools created by Felix Kraus, as a side project, who then got employed by Twitter to build cool dev tools full time. Fastlane is now a part of the toolset that is Twitter’s Fabric and has been growing steadily to include Android tools and continuous improvements.”

An update since then is that Fabric and subsequently Fastlane has been acquired by Google and incorporated into the Firebase toolset. While the Android tools in the Fastlane set aren’t as extensive as the iOS ones, they are nonetheless very useful, and I look forward to seeing further inclusions under the Firebase banner. The relevant Android tools are:

1. Supply
2. Screengrab (out of scope for this tutorial)

This tutorial will cover the full set up process for Android, including build and signing tasks and then distribute it to Google Play, while issuing Slack notifications about progress. Later tutorials will build on this to include all of the screengrab and testing actions one might want. The following assumes you have an existing project that you want to add Fastlane to.

Setting Up Fastlane

In general, the best set of instructions on how to do this is the quickstart guide: https://docs.fastlane.tools/getting-started/android/setup/ . However, here are a few peculiarities to note with Fastlane for Android:
Confirm that you have the correct version of Ruby (v2) installed by opening terminal and entering the following command:
ruby -v
If not you will need to upgrade it before you continue on.
Make sure you have the latest JRE installed. If you’ve installed the full JDK there will be a JRE with it. If later when you run commands you’re getting the prompt to install java command line tools, download and install the full JDK.
If you’ve got that all sorted, go ahead and install Fastlane using the quickstart guide. However, don’t initialise Fastlane in your project until after you’ve sorted the credentials side of things…

Setting Up Scan

For the next part, you will need to access the Google Developer Console. Down the side of the screen, select the tab for settings and then select API Access. Proceed to obtain the JSON file for Google API access according to the guide here.
Rename the JSON file to something easier to reference/shorter like “GoogleKey.json” and move it into an easy to reference location.

Setting up Slack Integration

Let’s set up a slack webhook:

https://api.slack.com/incoming-webhooks

Go to the above page and make sure you’re signed into the team you want the message to go to up the top right-hand corner. Then, pick or create a new channel.

You will have the URL you need now and you can leave it at that. However, you can also customise a few other things if you scroll down. For example, I’ve added the following details to customise the notifications that get produced in my slack channel.

Screen Shot 2017-02-18 at 4.46.39 pm

Fastfile Configuration

Now you have everything you will need to initialise Fastlane. Navigate to the home directory for your app and type:

Fastlane init

It will ask you to put in your package name “com.example.app”.

Then it’ll ask you for the Google Key file location. If you’ve put it in the home directory of the app you would jut type:

GoogleKey.json

N.B. do not use quotes around it — this differs from if you are manually entering it into the Appfile yourself. The benefit of doing it during set up is that it downloads all the current metadata from Google Play for you and you don’t have to do the separate Supply set up later.

If you answered yes to the question “Do you plan on uploading metadata, screenshots and builds to Google Play using
FastLane? (y/n) “ it will then find the app for you on your Google Play dashboard and download existing assets.

After you’ve initialised Fastlane in an app directory, a Fastfile will have been created for you (in the Fastlane folder). Open up the Fastfile in a text editor and we can start adding configurations, or ‘lanes’.

The beginning of the file should look like this (uncomment anything you need to):

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.8.0"
gradle_path = '/Applications/Android Studio.app/Contents/gradle/gradle-2.14.1’
default_platform :android

platform :android do
before_all do
ENV[“SLACK_URL”] = “YOUR SLACK WEBHOOK URL”
end

Now we can create a beta lane:

desc "Submit a new Beta Build to Google Play Beta"
lane :beta do
  gradle(task: 'clean')
  gradle(task: "assemble”, build_type: 'Release')
  # Generate the changelog based on commit messages since your last tag
  changelog_from_git_commits
  supply(track: “beta”, apk: "app/app-release.apk")
  slack(message: 'Successfully distributed a new beta build')
end

This is doing a few things. Firstly, to avoid issues, we made sure we clean the project before we build it. Then we build and specify the Release type. We create a changelog from git commits since the last build, and then we supply the APK to the beta release track. It’s important to make sure that you specify the location where the signed release APKs end up (which as of the most recent version of Android studio is as above) to avoid it defaulting to the unsigned version.

Setting up Signing Correctly

We’re not quite ready to use the “fastlane” command yet. There’s some setup we need to do inside Android Studio to ensure that everything runs correctly. This part you won’t find in the documentation.

The first thing we want to do is ensure that Gradle knows what key to sign the APK with. Go to File>Project Structure> app>signing and make sure all the credentials are entered for your Keystore and key:

Screen Shot 2017-02-19 at 1.43.46 pm

Save this and check that these signing credentials have appeared in the build.gradle file.

Now we need to make some further changes to ensure that the version code and build number increment. In the app level project navigator create a file called version.properties. In that file, enter the following line:

VERSION_CODE=1

No back in build.gradle, modify it so that the application part looks like this:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

def versionPropsFile = file(‘version.properties’)

if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()

versionProps.load(new FileInputStream(versionPropsFile))

def code = versionProps[‘VERSION_CODE’].toInteger() + 1

versionProps[‘VERSION_CODE’]=code.toString()
versionProps.store(versionPropsFile.newWriter(), null)

defaultConfig {

applicationId “com.miccn.surveykit.app”
minSdkVersion 16
targetSdkVersion 23
versionCode code
versionName “1.” + code
multiDexEnabled true
}
}
else {
throw new GradleException(“Could not read version.properties!”)
}

defaultConfig {

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
}

signingConfigs {
debug {
storeFile file(‘/[directory]/keystore.jks’)
storePassword “[YOUR PASSWORD]”
keyAlias “[YOUR ALIAS]”
keyPassword “[YOUR PASSWORD]”
}
release {
storeFile file(‘/[directory]/keystore.jks’)
storePassword “[YOUR PASSWORD]”
keyAlias “[YOUR ALIAS]”
keyPassword “[YOUR PASSWORD]”
}
}

}

As with iOS, there are many more actions that you can add to a beta lane and more info is here.

All Systems Go…

Ok, are you ready?

Now you type this into your terminal:

fastlane beta
Screen Shot 2017-02-19 at 6.12.46 pm

Other Gotchas

If you get the permissions error, you will need to run this command in the root project directory:

chmod +x gradlew

Conclusion

This showed the process from end to end for creating a lane to distribution with Google Play. At the moment the APK promotion process is being changed by google and so that part of the API is broken. At a later stage, I’ll be including how to promote a beta APK to production via the command line, and how to do snapshots and auto upload image assets.

Originally published at Coder Bec.