In this tutorial, you'll learn how to streamline your Android app development process using GitLab's powerful Mobile DevOps features. By the end of this guide, you'll have a pipeline that automates building, signing, and distributing your apps to Google Play Store.
Setting Up Your Build Environment
Before we dive into the code, 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
To get started, choose between using GitLab-hosted runners or setting up self-managed runners for complete control over your build environment. Android builds utilize Docker images, offering multiple Android API versions.
Create a .gitlab-ci.yml file in your repository root and add the following:
`yaml
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, follow these steps:
- Create 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
`
- Put the keystore configuration in the
release-keystore.propertiesfile:
`properties
storeFile=.secure_files/release-keystore.jks
keyAlias=release
keyPassword=password
storePassword=password
`
- Upload both files as Secure Files in your project settings and add them to your
.gitignorefile so they aren't committed to version control. - Configure Gradle to use the newly created keystore by adding the following code to your app's
build.gradlefile:
`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
signingConfigto 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 Mobile DevOps Distribution integrations. To set up distribution:
- Create a Google service account in Google Cloud Platform and grant that account access to your project in Google Play.
- Enable the Google Play integration:
+ Select Settings > Integrations
+ Select Google Play
+ Under Enable integration, select the Active checkbox
+ In Package name, enter the package name of your app (e.g., com.gitlab.app_name)
+ In Service account key (.JSON) drag or upload your key file
+ Select Save changes
Add a release step to your pipeline:
`yaml
beta:
image: fabernovel/android:api-33-v1.7.0
stage: beta
script:
- fastlane beta
`
For an overview, see the Google Play integration demo.
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
- See the Mobile DevOps Android Demo project for a complete build, sign, and release pipeline example for Android.
- For additional reference materials, see the DevSecOps section of the GitLab blog.