Automate your development process
Written by Souvik Biswas
Learn from this article how to configure the codemagic.yaml file for building Native Android projects on Codemagic CI/CD. Since the introduction of the codemagic.yaml
file, it has been a lot easier to keep your build configurations more organized and manage all your workflows with just a single file. This file can be committed to version control and gets automatically detected during build from the Codemagic UI.
Though Codemagic does not come with support for building native Android and iOS apps directly from the UI, you can easily configure the codemagic.yaml
file for building native apps. In this article, you will learn how to configure the codemagic.yaml
file for building native Android projects on Codemagic.
It is recommended to go through the article for better understanding but if you already have experience with using
codemagic.yaml
file, you can get the YAML template for a native Android project here.
Android continuous integration: Getting started with Codemagic
Make sure that your native Android project is uploaded to a code hosting platform (like GitHub, Bitbucket or GitLab) using a version control system.
Log in to Codemagic.
In the Applications overview, search for your project and click Set up build.
Select Android App as the starter workflow.
You can download the YAML template for native Android by clicking on the download button.
Now, open the
codemagic.yaml
file using any code editor. The YAML file contents will look like this:
# Workflow setup for building Native Android project
workflows:
android-app: # workflow ID
name: Android app # workflow name
scripts:
- ./gradlew assembleDebug
artifacts:
- build/outputs/**/**/*.apk
Android workflow and a default YAML file
You will see that a default workflow ID and workflow name is specified, but you can change them however you wish.
The default Android workflow specified in the codemagic.yaml
file is very simple and will help you get started if you want to generate a debug build of your Android app.
Deep dive into YAML
Let’s take a closer look at the codemagic.yaml
file and understand the real power of it.
Scripts
In order to generate a debug build, you will need a debug.keystore
file. The following is the code for generating the file:
keytool -genkeypair \
-alias androiddebugkey \
-keypass android \
-keystore ~/.android/debug.keystore \
-storepass android \
-dname 'CN=Android Debug,O=Android,C=US' \
-keyalg 'RSA' \
-keysize 2048 \
-validity 10000
Running local unit tests:
./gradlew test
Running instrumented unit tests:
./gradlew connectedAndroidTest
You will also need to start up an Android emulator in order to run the instrumented unit tests. Add the following command:
emulator -avd emulator > /dev/null 2>&1 &
The &
is used in the end of this command to run it in the background, otherwise the emulator
command blocks the terminal and prevents any further commands from execution. > /dev/null 2>&1
is used to ignore all outputs of this command.
TIPS: It is better to add this command at the beginning of the script, as the emulator takes some time to start up.
Building the app in debug mode:
./gradlew assembleDebug
Artifacts
You can get the generated .apk file by adding its path under the artifacts
. Normally, the path is like this:
artifacts:
- app/build/**/outputs/**/*.apk
Publishing
To get a report of the build along with the generated artifacts in your email, specify the following:
publishing:
email:
recipients:
- name@example.com # enter your email id here
For more information about publishing, refer to this link.
Setting up your Android project for a release build
First of all, you have to set up code signing for generating release build.
Generating a keystore
If you don’t already have a keystore created, you can use the following command from your Terminal to generate one.
keytool -genkey -v -keystore keystore_name.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000
Configuring your project
Follow the steps below to prepare your project for a release build:
Open your project using Android Studio.
Switch to the Project View.
Inside the root project folder, create a new file called
key.properties
.Add the following to this file:
storePassword=<enter keystore password> keyPassword=<enter key alias password> keyAlias=<enter key alias name> storeFile=<enter .keystore file path>
Replace the angle brackets and its content with the appropriate values.Save the file and add it to
.gitignore
, so that you do not commit this file by mistake.Now, switch to the Android View, and go to the app level
build.gradle
.Add the following to this file:
def keystorePropertiesFile= rootProject.file("key.properties") def keystoreProperties = new Properties() keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) // Add above this line android {
Add the signingConfigs:
signingConfigs { config { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile file(keystoreProperties['storeFile']) storePassword keystoreProperties['storePassword'] } }
Inside buildTypes release, add the following:
buildTypes { release { //... signingConfig signingConfigs.config } }
Modifying YAML for code signing
You will need the keystore file to sign your build. To get access to it, you will also need the key.properties
file.
As you have added the key.properties
file to .gitignore
, it won’t be available during build on Codemagic. So you can encrypt all the sensitive information and add it to the codemagic.yaml
file for generating the key.properties
file which can be used in turn to unlock the .keystore
file.
Follow the steps below to encrypt your sensitive information:
Go to your project Settings on Codemagic.
Click Encrypt environment variables.
You can drag and drop files here or enter the value to generate an encrypted version of them.
Now, create a new workflow called release-workflow
for the release build and add the following:
release-workflow:
name: Native Android
max_build_duration: 60
environment:
vars:
CM_KEYSTORE: # Add encrypted keystore file
CM_KEYSTORE_PASSWORD: # Add encrypted keystore password
CM_KEY_ALIAS_PASSWORD: # Add encrypted alias password
CM_KEY_ALIAS_USERNAME: # Add alias name
scripts: # Add the build configurations here
artifacts:
- app/build/**/outputs/**/*.apk
publishing:
email:
recipients:
- sbis1999@gmail.com
Let’s define the build script now.
First of all, you have to decrypt the .keystore
file and generate the key.properties
file.
scripts:
# ...
- |
# set up release keystore
echo $CM_KEYSTORE | base64 --decode > /tmp/keystore.keystore
cat >> "$FCI_BUILD_DIR/key.properties" <<EOF
storePassword=$CM_KEYSTORE_PASSWORD
keyPassword=$CM_KEY_ALIAS_PASSWORD
keyAlias=$CM_KEY_ALIAS_USERNAME
storeFile=/tmp/keystore.keystore
EOF
Then, you can add tests as usual and use the following command for generating a release build:
./gradlew assembleRelease
You have successfully completed the setup for generating a release build of your app.
Building an Android project on Codemagic
Before starting a build on Codemagic, make sure that you have the codemagic.yaml
file committed to the version control system.
Follow the steps below to start a build:
In the Applications dashboard, click Finish build setup.
Click Check for configuration file.
Then, click Start your first build.
Select the workflow from the YAML file and click Start new build.
This will start a new build for your native Android project.

Conclusion
Although Codemagic started as an official CI/CD solution dedicated just for Flutter apps, it now welcomes all mobile projects to the fastest CI/CD. You can easily build, test, and publish your Android, iOS, React Native and Flutter apps with Codemagic CI/CD.
As you can see, building native Android apps on Codemagic using the codemagic.yaml
file is really simple. There is also an added advantage, as YAML makes it a lot easier to manage your workflows and keep your build configurations more organized. Also, it gets automatically detected from the version control system when starting any build on Codemagic.
Useful links and references
More information about Android code signing is available here.
The GitHub repo of the sample project is here.
Souvik Biswas is a passionate Mobile App Developer (Android and Flutter). He has worked on a number of mobile apps throughout his journey. Loves open source contribution on GitHub. He is currently pursuing a B.Tech degree in Computer Science and Engineering from Indian Institute of Information Technology Kalyani. He also writes Flutter articles on Medium - Flutter Community.