Tutorial: Build Android apps with GitLab Mobile DevOps

In this tutorial, you’ll create a pipeline by using GitLab CI/CD that builds your Android mobile app, signs it with your credentials, and distributes it to app stores.

To set up mobile DevOps:

  1. Set up your build environment
  2. Configure code signing with fastlane and Gradle
  3. Set up Android apps distribution with Google Play integration and fastlane

Before you begin

Before you start this tutorial, 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

Set up your build environment

Use GitLab-hosted runners, or set up self-managed runners for complete control over the build environment.

Android builds use Docker images, offering multiple Android API versions.

  1. Create a .gitlab-ci.yml file in your repository root.
  2. Add a Docker image from Fabernovel:

    test:
      image: fabernovel/android:api-33-v1.7.0
      stage: test
      script:
        - fastlane test
    

Configure code signing with fastlane and Gradle

To set up code signing for Android:

  1. Create a keystore:

    1. Run the following command to generate a keystore file:

      keytool -genkey -v -keystore release-keystore.jks -storepass password -alias release -keypass password \
      -keyalg RSA -keysize 2048 -validity 10000
      
    2. Put the keystore configuration in the release-keystore.properties file:

      storeFile=.secure_files/release-keystore.jks
      keyAlias=release
      keyPassword=password
      storePassword=password
      
    3. Upload both files as Secure Files in your project settings.
    4. Add both files to your .gitignore file so they aren’t committed to version control.
  2. Configure Gradle to use the newly created keystore. In the app’s build.gradle file:

    1. Immediately after the plugins section, add:

      def keystoreProperties = new Properties()
      def keystorePropertiesFile = rootProject.file('.secure_files/release-keystore.properties')
      if (keystorePropertiesFile.exists()) {
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
      }
      
    2. Anywhere in the android block, add:

      signingConfigs {
        release {
          keyAlias keystoreProperties['keyAlias']
          keyPassword keystoreProperties['keyPassword']
          storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
          storePassword keystoreProperties['storePassword']
        }
      }
      
    3. Add the signingConfig to the release build type:

      signingConfig signingConfigs.release
      

The following are sample fastlane/Fastfile and .gitlab-ci.yml files with this configuration:

  • fastlane/Fastfile:

    default_platform(:android)
    
    platform :android do
      desc "Create and sign a new build"
      lane :build do
        gradle(tasks: ["clean", "assembleRelease", "bundleRelease"])
      end
    end
    
  • .gitlab-ci.yml:

    build:
      image: fabernovel/android:api-33-v1.7.0
      stage: build
      script:
        - apt update -y && apt install -y curl
        - curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
        - fastlane build
    

Set up Android apps distribution with Google Play integration and fastlane

Signed builds can be uploaded to the Google Play Store by using the Mobile DevOps Distribution integrations.

  1. Create a Google service account in Google Cloud Platform and grant that account access to the project in Google Play.
  2. Enable the Google Play integration:
    1. On the left sidebar, select Search or go to and find your project.
    2. Select Settings > Integrations.
    3. Select Google Play.
    4. Under Enable integration, select the Active checkbox.
    5. In Package name, enter the package name of the app. For example, com.gitlab.app_name.
    6. In Service account key (.JSON) drag or upload your key file.
    7. Select Save changes.
  3. Add the release step to your pipeline.

The following is a sample fastlane/Fastfile:

default_platform(:android)

platform :android do
  desc "Submit a new Beta build to the Google Play store"
  lane :beta do
    upload_to_play_store(
      track: 'internal',
      aab: 'app/build/outputs/bundle/release/app-release.aab',
      release_status: 'draft'
    )
  end
end

The following is a sample .gitlab-ci.yml:

beta:
  image: fabernovel/android:api-33-v1.7.0
  stage: beta
  script:
    - fastlane beta

For an overview, see 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.

See the Mobile DevOps Android Demo project for a complete build, sign, and release pipeline example for Android.

For additional reference materials, see the DevOps section of the GitLab blog.