Android Fastlane Tutorial
Automating an Application Deployment Process
Let’s not waste any time, if you’re an Android developer with an Apple Mac development machine, and want to automate the build, testing and deployment process using the Fastlane framework, you’re at the right place.
This tutorial as an addendum to the existing setup process available at the official Fastlane documentation page. If you’re experiencing issues with your Fastlane setup, you can use this article as a checklist of sorts or go directly to the troubleshooting section of this article for the more common issues when running Fastlane deploy.
Fastlane Setup Prerequisites
First things first, visit the following page and make sure that you’ve obtained and secured the needed *.json API file that is tied to your existing Android Developer account:
https://docs.fastlane.tools/getting-started/android/setup/
- You have already created the project for your app on the Google Play Console
- You have generated the release signing credentials for your app (keystore *.jks file and password, alias, alias password)
- On a Mac machine, make sure you are in the right shell and that JAVA_HOME is defined (best use the same one as in your Android Studio Environment)
Android Project Setup
- Create a new folder in the root directory of your app and call it ‘secure’
- Add the secure folder to .gitignore rules: /secure/*
- Copy your already created *.jks and Google Developer Cloud API *.json (created in the Fastlane setup section ) files into the newly created secure root app directory
- In your local.properties add the Android app-signing information in the key-value format (make sure that the information you enter here is valid):
KEYSTORE_PATH=PATH_TO_KEYSTORE_ON_LOCAL_MACHINE
KEY_ALIAS=APP_ALIAS
KEY_PASSWORD=APP_ALIAS_KEY_PASSWORD
STORE_PASSWORD=APP_STORE_PASSWORD
We will create a new release signing configuration. In your app module’s build.gradle file, inside the android closure, add the following code at the beginning:
signingConfigs {
release {
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('local.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
storeFile file(keystoreProperties['KEYSTORE_PATH'])
keyAlias keystoreProperties['KEY_ALIAS']
keyPassword keystoreProperties['KEY_PASSWORD']
storePassword keystoreProperties['STORE_PASSWORD']
}
}
6. Edit the buildTypes closure inside the same android closure block and add the following line inside the release block:
signingConfig signingConfigs.release
7. Initialize Fastlane for the project and follow the setup console prompts to complete the Fastlane project setup. Run the following command inside the root directory of your project:
fastlane init
8. In the newly created fastlane directory we first need to edit AppFile and provide/validate the information for the json API key file path and app package name:
json_key_file("./secure/DEV_API.json")
package_name("your.app.package.name")
9. Next we define the steps for Fastlane to take when we run the script. For the sake of brevity, I’m going to only showcase the final deployment step for the bundle release deployment:
platform :android do
desc "Deploy a new version to the Google Play"
lane :deploy do
gradle(
task: 'clean bundle',
build_type: 'Release'
)
upload_to_play_store # Uploads the AAB built in the gradle step above and releases it to all production users
end
end
10. Check if Java runtime is exported in the JAVA_HOME environment value (see below for troubleshooting options). You can run the following command in the terminal to validate if Java is installed (double dash):
java -version
11. Continue with executing the Fastlane deployment. When running the first time, it can take some time until all of the dependencies and metadata information are downloaded and the build and deployment steps commence. This step largely depends on your build machine performance and the speed of your local internet connection
Run Fastlane Deploy
After the steps above have been completed, you can finally run Fastlane deploy with the following command:
bundle exec fastlane deploy
Based on the Bundler configuration on you machine, you might need sudo privileges. Run this instead:
sudo bundle exec fastlane deploy
Troubleshooting
Java Runtime Not Found
In case you’re getting java runtime not found, try these steps in the terminal:
/usr/libexec/java_home -V | grep jdk
You should get something like this in the output:
Matching Java Virtual Machines (1):
1.8.301.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Let’s add or update the JAVA_HOME path using vim :
vim .zshrc
Proceeding with adding the following information (make sure that the correct Java JDK path is entered, below is just an example):
export JAVA_HOME=/Users/your_device_account/Library/Java/JavaVirtualMachines/java_version/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
Make sure you replace the JAVA_HOME path with the one identified with which java
Depending on the profile you are using for the shell, it should work for zsh too.
To apply the new changes run (or .barshrc, or .zsh, etc.):
source .zshrc
This would probably not persists and when you restart the terminal, you’ll face the same problems.
Gradlew Permission Denied
In case if you’re getting permission denied, run the following command to give write permission to gradlew:
chmod +x gradlew
Gradle Deprecated or Unrecognized VM Options
In you are experiencing build issues with Gradle, make sure that you are not using deprecated gradle jvmargs or similar:
Unrecognized VM option ‘MaxPermSize=512m’
Remove or replace the arguments with ones that are supported by the new versions of the build system.
Final Thoughts
The steps provided in this article are not perfect and are simply my own way of getting Fastlane quickly up and running for new and existing projects. I’m sure that most of things described and showcased can be further improved upon and streamlined for an overall better developer experience.
If you found this tutorial useful, feel free to leave a comment and feedback.
Thank you for reading, and happy coding!
References
Fastlane official documentation: https://docs.fastlane.tools/