Deploy Android applications
On This Page
- Introduction
- 1. Configure Fastlane for deployment to Google Play Store
- a. Configure tasks
- b. Update Google Play Store lane
- 2. Prepare your app for deployment
- a. Add signingConfigs snippet
- b. Update buildTypes
- c. Add snippet before Android section
- d. Update properties in defaultConfig
- 3. Set up CircleCI deployment
- a. Convert your keystore to base64
- b. Set environment variables
- c. Import keystore properties
- 4. Set up your configuration
- a. Set up configuration with the Android orb
- b. Set up configuration without the Android orb
- Next steps
In this how-to guide, you will learn how to automatically deploy your Android app through CircleCI with Fastlane.
Introduction
To complete this tutorial, you will need the following:
-
An Android project, built using Gradle.
-
Your Android project set up in CircleCI. If this is your first time setting up a project in CircleCI, refer to our Quickstart guide for a walkthrough.
-
Fastlane installed and granted access to Google Play Store using credentials from your Google Developers service account. You may follow the instructions to install, set up, and configure Fastlane supply in the Fastlane Android setup documentation.
In the Fastlane setup, you will download a credentials file from your Google Developers service account. This tutorial assumes that you have saved this file in the fastlane folder as api.json . |
This tutorial also walks you through how to use the Android orb for deployment. You are not required to be familiar with the Android orb for this tutorial, but we recommend reading the Orb introduction page if you have not used CircleCI orbs before. Alternatively, this tutorial also shows you how to deploy your project without using the orb.
1. Configure Fastlane for deployment to Google Play Store
To configure Fastlane for deployment, you will need to configure tasks and update the Play Store lane.
a. Configure tasks
Navigate to your fastlane
directory and open Fastfile
. This file is used to configure the tasks you can execute with Fastlane. If you open the file, you will observe four different blocks:
-
before_all
: This is where you specify instructions to be performed before a lane is executed. -
lane
: This is where you define an actual task you want to perform, such as deploying to the Play Store. You can define as many lanes as you want. -
after_all
: This block is called when the executed lane was successful. -
error
: This block will be called if an error occurs in any of the other blocks.
b. Update Google Play Store lane
The Fastfile already comes with a playstore
lane that generates a release build and deploys it to the Play Store. You will modify this lane. In the Fastfile, update the playstore
lane like so:
lane :playstore do
gradle(task: "bundle")
upload_to_play_store(
track: 'internal',
release_status: 'draft'
)
end
This lane will run gradle bundle
and generate an app-release.aab
(Android App Bundle), and then deploy the bundle to the internal track of your project in the Google Play Console.
2. Prepare your app for deployment
To prepare your app for deployment, you will need to add a release signing configuration to your app-module build.gradle
file. This enables Fastlane to use the same keystore you used in generating earlier releases of your app to also generate subsequent releases.
a. Add signingConfigs snippet
Add the following signingConfigs
snippet to your app-module build.gradle
file:
android {
signingConfigs {
release {
keyAlias keyStoreProperties['releaseKeyAlias']
keyPassword keyStoreProperties['releaseKeyPassword']
storeFile file(keyStoreProperties['releaseKeyStore'])
storePassword keyStoreProperties['releaseStorePassword']
}
}
...
}
For this tutorial, you will be generating a keystore and keystore.properties at runtime. You can replace the fields with your actual passwords and information for local deployment, but we strongly advise to not upload these credentials to a remote repository. If you need help generating a keystore for your application, you may refer to the Android user guide. |
b. Update buildTypes
Update the buildTypes
section of your build.gradle
file:
buildTypes {
release {
signingConfig signingConfigs.release
...
}
}
With this, you have configured the app to use a specific keystore. Next, you will create functions in your build.gradle
file to help you generate build numbers for your app versions.
c. Add snippet before Android section
Add the following snippet just before the android
section of your app-module build.gradle
file:
ext.versionMajor = 1
ext.versionMinor = 0
ext.versionPatch = 1
ext.versionClassifier = null
ext.isSnapShot = false
ext.minSdkVersion = 21
private Integer generateVersionCode() {
return ext.minSdkVersion * 10000000 + ext.versionMajor * 10000 +
ext.versionMinor * 100 + ext.versionPatch
}
private String generateVersionName() {
String versionName = "${ext.versionMajor}.${ext.versionMinor}.${ext.versionPatch}"
if (ext.versionClassifier == null) {
if (ext.isSnapShot) {
ext.versionClassifier = "SNAPSHOT"
}
}
if (ext.versionClassifier != null) {
versionName += "-" + ext.versionClassifer
}
return versionName
}
In this snippet, you added variables that hold your app version values. You then added two methods, generateVersionCode
and generateVersionName
to generate the version code and version name based on how the app version value changes. This helps to give your app a unique and progressive way of generating your version code when you modify your app versions.
Note that you will need to update at least one parameter of the version for every deployment. Fastlane fails if a version code is reused.
d. Update properties in defaultConfig
Update the following properties in the defaultConfig
section of the build.gradle
file:
defaultConfig {
versionName generateVersionName()
versionCode generateVersionCode()
// ... Leave others as is
}
Now your Android app is able to be bundled and deployed on your local machine.
3. Set up CircleCI deployment
To set up CircleCI deployment, you will need to take a few security measures.
a. Convert your keystore to base64
You need to convert your keystore to base64 to safely access it in CircleCI. You can do this conversion in the terminal using the following command:
$ base64 your_key_store
Save the output. You will need it in the next step.
b. Set environment variables
Next, you need to set environment variables for deployment through CircleCI.
Open your Android project in the CircleCI web app and select Project Settings. Navigate to Environment Variables and add the following variables:
-
$BASE64_KEYSTORE
- Your base64 keystore, generated in the previous step -
$GOOGLE_PLAY_KEY
- The contents of yourapi.json
file, generated from the Fastlane install before starting this tutorial -
$RELEASE_KEY_ALIAS
- Your key alias -
$RELEASE_KEY_PASSWORD
- Your key password -
$RELEASE_STORE_PASSWORD
- Your keystore password
c. Import keystore properties
Add the following snippet to your build.gradle
file. This allows you to import your keystore properties from a keystore.properties
that will be generated at runtime.
def keyStorePropertiesFile = rootProject.file("keystore.properties")
def keyStoreProperties = new Properties()
keyStoreProperties.load(new FileInputStream(keyStorePropertiesFile))
android {
...
}
You can also create a Use the following snippet to create this file: |
Now, you need to configure .circleci/config.yml
to decrypt your keystore, generate keystore.properties
, and create the Google Play API key at runtime.
If you have not already done so, create a .circleci
folder in the root of your project repository. Inside the .circleci
folder, create a config.yml
file.
4. Set up your configuration
You can set up your configuration with the Android orb, or without the Android orb.
a. Set up configuration with the Android orb
Using the Android orb gives you two options for deploying to the Google Play Store. You can either use the deploy-to-play-store
job from the orb, or run each command individually in a job.
b. Set up configuration without the Android orb
If you would prefer to not use the Android orb, you can follow the configuration directions below.
-
Decrypt keystore from base64
Add the following command to your deployment job in
.circleci/config.yml
to decrypt your keystore from the base64 environment variable set earlier ($BASE64_KEYSTORE
).run: name: Decode Android key store command: echo $BASE64_KEYSTORE | base64 -d | tee keystore android/app/keystore > /dev/null
-
Generate a
keystore.properties
fileNext, you need to generate a
keystore.properties
file in order to publish your work to the Google Play Store.To do so, you need to create another environment variable named
$RELEASE_KEYSTORE
, that points to the location of the decrypted keystore.Add the following command to your deployment job:
run: name: Create keystore.properties command: cd android && printf 'releaseKeyAlias=%s\nreleaseKeyPassword=%s\nreleaseKeyStore=%s\nreleaseStorePassword=%s' \ $RELEASE_KEY_ALIAS $RELEASE_KEY_PASSWORD $RELEASE_KEYSTORE $RELEASE_STORE_PASSWORD > keystore.properties
-
Create Google Play API key
Finally, you need to create your Google Play API key from your
api.json
You have already saved the value with an environment variable$GOOGLE_PLAY_KEY
, so you can refer to this variable and write the api.json file at runtime, instead of uploading it to a remote repository.Add the following command to the job:
run: name: Create Google Play key command: echo $GOOGLE_PLAY_KEY > google-play-key.json
-
Deploy your app
To now deploy your app, you need to add your Fastlane steps. To do this you only need to add a command that runs
fastlane my_deployment_lane
; in this case, the command looks like this:run: fastlane playstore
You may need to install Fastlane on the image you are running your pipeline on. To do this, run sudo gem install fastlane
.Fastlane recommends using Bundler in this step. If you choose to use Bundler, you will need to add another step to install Bundler:
run: sudo gem install fastlane
Then, replace the
run: fastlane playstore
step withrun: bundle exec fastlane playstore
.