Building a native Android app

How to build an Android app with codemagic.yaml

Setting up an Android project

The apps you have available on Codemagic are listed on the Applications page. Click Add application to add a new app.

  1. On the Applications page, click Set up build next to the app you want to start building.
  2. On the popup, select Android App as the project type and click Continue.
  3. Create a codemagic.yaml file and add in it the commands to build, test and publish your project. See the full Android workflow example below.
  4. Commit the configuration file to the root of your repository.
  5. Back in app settings in Codemagic, scan for the codemagic.yaml file by selecting a branch to scan and clicking the Check for configuration file button at the top of the page. Note that you can have different configuration files in different branches.
  6. If a codemagic.yaml file is found in that branch, you can click Start your first build and select the branch and workflow to build.
  7. Finally, click Start new build to build the app.


Note that you need to set up a webhook for automatic build triggering. Click the Create webhook button on the right sidebar in app settings to add a webhook (not available for apps added via SSH/HTTP/HTTPS).

Building an Android app

The necessary command for building native Android application goes under scripts in the overall architecture in the codemagic.yaml file. For Android (built with gradle), the script looks like this:

- ./gradlew build

Testing, code signing and publishing an Android app

To test, code sign and publish an Android app:

  • The code for testing an Android app also goes under scripts. A few examples of testing can be found here.
  • All Android applications need to be signed before release, see how to do that here.
  • All generated artifacts can be published to external services. Script examples are available under the Publishing section.

Android workflow example

You can find an up-to-date codemagic.yaml Android workflow in Codemagic Sample Projects.

The following example shows how to set up a workflow that builds your app and publishes it to a Google Play internal track.

    name: Android Workflow
    max_build_duration: 60
    instance_type: mac_mini
        - keystore_credentials # <-- Includes - CM_KEYSTORE, CM_KEYSTORE_PASSWORD, CM_KEY_PASSWORD, CM_KEY_ALIAS
        - google_play # <-- Includes - GCLOUD_SERVICE_ACCOUNT_CREDENTIALS
        - other
      # Add the group environment variables in Codemagic UI (either in Application/Team variables) -
        CM_KEYSTORE_PATH: /tmp/keystore.keystore
      node: latest
        - push
        - tag
        - pull_request
        - pattern: release
          include: true
          source: true
      - name: Set up local properties
        script: echo "sdk.dir=$ANDROID_SDK_ROOT" > "$CM_BUILD_DIR/"
      - name: Set up file for code signing
        script: |
          echo $CM_KEYSTORE | base64 --decode > $CM_KEYSTORE_PATH
          cat >> "$CM_BUILD_DIR/android/" <<EOF
      - name: Build Android app
          ./gradlew bundleRelease  # To generate an .apk use--> ./gradlew assembleRelease
      - app/build/outputs/**/**/*.aab
      - app/build/outputs/**/**/*.apk
        track: internal
Note that you should incremenet the versionCode in android/app/build.gradle.

Incrementing the version code can be done as follows:

android {
    def appVersionCode = Integer.valueOf(System.env.BUILD_NUMBER ?: 0)
    defaultConfig {
        versionCode appVersionCode