In this post we’ll see a real world scenario where Fastlane can help us automate releasing an Android app to internal testers and publishing on Google Play as well. If you are new to Fastlane, I suggest you to read my previous posts on Fastlane for Android:
- [Setup and Basics]({{< ref “fastlane-android-1” >}})
- [Flavors and Tests]({{< ref “fastlane-android-2” >}})
- [Publishing on Play Store]({{< ref “fastlane-android-3” >}})
Build an APK and upload to Slack
Let’s see a Fastlane script for automating Android APK building and uploading to a Slack channel for internal testing purposes.
Snippet Breakdown
1. Ensure Git Status is Clean
This step verifies that the working directory is clean, with no uncommitted changes. It’s a safeguard to ensure that all changes are accounted for in version control before proceeding with a build.
For workflows where minor, uncommitted changes are expected (e.g., autogenerated files), consider adding conditions to exclude specific files from this check or to commit them automatically. If certain files are expected to change and you want to automatically commit those changes as part of your Fastlane lane, you can script this as follows.
First, identify and commit specific files automatically before proceeding with the clean status check:
After committing the specified autogenerated files, you can then safely use the ensure_git_status_clean
action.
2. Get the Latest Uploaded Build Number
This snippt retrieves the version code of the most recent APK from a specific Google Play track. This ensures that each new release increments from the latest version, maintaining version continuity.
This section can be improved by adding an error handling block. We can use Ruby’s begin-rescue block to encapsulate the logic for fetching the latest uploaded build number. In case of an error (e.g., network issues, authentication errors, unexpected API responses), the rescue block can catch the exception and handle it accordingly.
3. Increment and Set New Version Code
Updates the version code in the project’s build.gradle
file, ensuring the new build is correctly versioned.
4. Execute Gradle Tasks
Clean, test, and assemble tasks are executed in sequence.
These steps ensure the APK is built from a clean state, passes all tests, and is assembled for release. The snippet specifically builds the “Dev” flavor of the app, indicating that this script is tailored for development or testing purposes rather than a production release. This is evident from the tasks executed: clean, testDevDebug, and assembleDevRelease. The following synthax is also supported:
In the case of an app without any flavors, the build process simplifies as there’s no need to specify a flavor when running Gradle tasks. Here’s how the script would be adjusted to handle a project without flavors:
or
If the root of Android source code is different from the root of the project, use the project_dir
parameter to pass the path to the root:
5. Upload APK to Slack
Collects and formats essential information about the release, such as the app version, branch, and commit hash, for transparency and traceability.
You can include more details in the message, such as the build date/time and a link to the commit on a version control platform (e.g., GitHub) for easy access to the change log.
This step uploads the built APK to a designated Slack channel, providing team members with immediate access to the new release, accompanied by the release information message.
Build an AAB and upload to Google Play
This snippet builds an AAB and upload it to Google Play, targeting users in the ‘Beta test track’.
Snippet Breakdown
We talked about ensure_git_status_clean
and gradle
actions before. Let’s check the new stuff.
Bundle the Production Release
This command generates an Android App Bundle (AAB) for the production release build variant (proRelease
). AAB is the recommended publishing format on Google Play, offering benefits like smaller app sizes and streamlined deployment across various device configurations.
Upload to Google Play Store
This step uploads the generated AAB to the Google Play Store, specifically targeting the ‘Beta test track’. This allows for testing the app with a limited user base before a wider rollout. The skip_upload_apk
option is set to true
to indicate that only the AAB file is being uploaded, aligning with Google Play’s preference for AAB over APK files.
Publish App on Google Play Store
The final snippet will help us promoting this version of the app from ‘Beta test track’ to production.
Snippet Breakdown
Retrieve the Latest Uploaded Build Number
This command fetches the version code of the latest app version uploaded to the ‘Beta test track’. It’s crucial for identifying which version of the app is currently in beta and ready to be promoted to production.
Promote the Beta Version to Production
This part of the script is where the promotion happens. Let’s break down the key parameters:
package_name
: Specifies the unique application ID of the app being promoted.version_code
: The version code of the app version to promote, fetched from the beta track.track
: The current track of the app version, in this case, ‘Beta test track’.track_promote_to
: The target track for promotion, here specified as ‘production’, indicating the app version is being moved to the full release stage.rollout
: Sets the percentage of users who should receive the update initially. ‘0.5’ means 50% of users. It’s a strategy used to gradually roll out updates to monitor performance and catch potential issues early.skip_upload_metadata
,skip_upload_changelogs
,skip_upload_images
,skip_upload_screenshots
: These parameters control whether to upload metadata, changelogs, images, and screenshots. In this script, metadata, images, and screenshots uploads are skipped, but changelogs are not, indicating an update note or changelog will be provided for the new production release.
Wrapping up
In sharing these Fastlane snippets, my aim was to present a real-world use case that illustrates the practical application of Fastlane in automating Android app development workflows.
To run these lanes from the command line, you would navigate to your project’s root directory and use the following syntax:
or using bundler (recommended):
While we’ve covered some specific and highly useful functionalities of Fastlane here, it’s important to note that Fastlane offers a vast array of features beyond what we’ve discussed. To fully leverage the power of Fastlane in your Android projects, we highly recommend consulting the official Fastlane documentation.