In this comprehensive guide, we'll walk you through the process of building, signing, and distributing your Android app using GitLab CI/CD. With the rise of mobile DevOps, it's essential to streamline your development pipeline to ensure faster time-to-market and improved collaboration.
Before You Begin
To get started, make sure you have:
- A GitLab account with access to CI/CD pipelines
- Your mobile app code in a GitLab repository
- A Google Play developer account
- Fastlane installed locally
Setting Up Your Build Environment
To set up your build environment, you can use either GitLab-hosted runners or self-managed runners for complete control. Android builds utilize Docker images, offering multiple Android API versions.
Creating a .gitlab-ci.yml File
In the root of your repository, create a new file named .gitlab-ci.yml. This file will serve as the configuration file for your CI/CD pipeline. Add a Docker image from Fabernovel:
`yaml
test:
image: fabernovel/android:api-33-v1.7.0
stage: test
script:
- fastlane test
`
Configuring Code Signing with Fastlane and Gradle
To set up code signing for Android, you'll need to generate a keystore using the following command:
`bash
keytool -genkey -v -keystore release-keystore.jks -storepass password -alias release -keypass password -keyalg RSA -keysize 2048 -validity 10000
`
Upload both files as Secure Files in your project settings and add them to your .gitignore file so they aren't committed to version control.
Configuring Gradle to Use the Newly Created Keystore
In your app's build.gradle file, immediately after the plugins section, add:
`groovy
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('.secure_files/release-keystore.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
`
Add the signingConfig to the release build type:
`groovy
signingConfig signingConfigs.release
`
Setting Up Android Apps Distribution with Google Play Integration and Fastlane
Signed builds can be uploaded to the Google Play Store using the Mobile DevOps Distribution integrations. To set up distribution, follow these steps:
- Create a Google service account in Google Cloud Platform and grant that account access to the project in Google Play.
- Enable the Google Play integration:
+ On the top bar, select Search or go to and find your project.
+ Select Settings > Integrations.
+ Select Google Play.
+ Under Enable integration, select the Active checkbox.
+ In Package name, enter the package name of the app. For example, com.gitlab.app_name.
+ In Service account key (.JSON) drag or upload your key file.
+ Select Save changes.
Add the release step to your pipeline:
`yaml
beta:
image: fabernovel/android:api-33-v1.7.0
stage: beta
script:
- fastlane beta
`
Conclusion
Congratulations! Your app is now set up for automated building, signing, and distribution. Try creating a merge request to trigger your first pipeline.
Related Topics
For additional reference materials, see the DevOps section of the GitLab blog.