paint-brush
Setting Up CI/CD for a Real Android Project With GitHub Actions (2 Part)by@okyrcheuskaya
436 reads
436 reads

Setting Up CI/CD for a Real Android Project With GitHub Actions (2 Part)

by Volha KurcheuskayNovember 7th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this guide, we'll explore how to set up a CI/CD pipeline for Android projects using GitHub Actions. We'll create a comprehensive GitHub Actions workflow that automates the build, test, and deployment processes for your Android project. First, create a GitHub repository or use an existing one. Then, define the jobs that make up your workflow.
featured image - Setting Up CI/CD for a Real Android Project With GitHub Actions (2 Part)
Volha Kurcheuskay HackerNoon profile picture

In the first part of our guide, we discussed the fundamental steps to set up a CI/CD pipeline for Android projects using GitHub Actions. Now, let's explore how to apply these principles to a real Android project.

Step 1: Creating a GitHub Repository

Assuming you have an Android project ready, let's create a GitHub repository or use an existing one.


  1. Create a New Repository:
    • Log in to your GitHub account, and click on the "+" icon in the upper-right corner.

    • Select "New repository", and follow the prompts to create your repository. Initialize it with a README file, and select a license if needed.


  2. Cloning an Existing Repository:
    • If you're working with an existing repository, clone it to your local development environment using git clone.

Step 2: Configure Your Android Project

Ensure that your Android project is properly configured for CI/CD:

  • Make sure you have a valid build.gradle file that defines the necessary dependencies and build settings.


  • Create a keystore.properties file to store your keystore information securely. Do not include this file in your version control system.


storeFile=keystore.jks storePassword=your_keystore_password keyAlias=your_key_alias keyPassword=your_key_password


Add the keystore.properties file to your .gitignore to avoid accidentally committing it.

Step 3: Create GitHub Actions Workflow

Now, let's dive into creating a comprehensive GitHub Actions workflow that automates the build, test, and deployment processes for your Android project.


  1. Workflow Structure:

    Create a YAML file for your GitHub Actions workflow in your project's .github/workflows directory. In this example, we'll name it android-ci-cd.yml. Your workflow will consist of jobs and steps that define what actions need to be taken during the CI/CD process.


  2. Specify Workflow Name:

    Start by giving your workflow a name, which will appear in the GitHub Actions dashboard.


name: Android CI/CD


  1. Trigger Workflow on Push:

You can specify the conditions that trigger the workflow. In this case, we'll trigger the workflow on every push to the main branch.


on: push: branches: - main


  1. Define Workflow Jobs:

Next, define the jobs that make up your workflow. A typical Android CI/CD workflow might have the following jobs:


  • Build Job: This job compiles your Android app and runs unit tests.


  • Deployment Job: This job is responsible for deploying your app to a specific environment (e.g., Google Play).


Here's an example of a Build Job:


jobs: build: name: Build and Test runs-on: ubuntu-latest


  1. Specify Steps:

    Each job consists of a series of steps. In the case of the Build Job, you'll need to:

    • Check out your project's code.

    • Set up the Java Development Kit (JDK) to the appropriate version.

    • Build the Android app.

    • Run unit tests.


    Here's an example configuration for these steps:


steps:
- name: Checkout code
  uses: actions/checkout@v2

- name: Set up JDK 11
  uses: actions/setup-java@v2
  with:
    distribution: 'adopt'
    java-version: '11'

- name: Build and Test
  run: |
    chmod +x gradlew
    ./gradlew assembleRelease
    ./gradlew test


This configuration checks out your code, sets up the JDK, and then uses Gradle to build your Android app and run unit tests.


  1. Artifact Upload:

    You can also include a step to upload build artifacts. In this example, we're uploading the generated APK bundle as an artifact:

- name: Upload APK
  uses: actions/upload-artifact@v2
  with:
    name: app-release
    path: app/build/outputs/bundle/release/app-release.aab


Uploading artifacts is useful when you want to preserve build artifacts for later use or distribution.

Step 4: Configure Environment Secrets

To securely manage sensitive information, such as keystore information, API keys, and other secrets, you need to set up environment secrets in your GitHub repository. You can refer to these secrets in your workflow configuration.


  1. Go to your GitHub repository.


  2. Click on "Settings" > "Secrets."


  3. Click on "New repository secret", and add the secrets you need. For example, you can add the following secrets:

    • KEYSTORE: The contents of your keystore file (base64 encoded).

    • KEYSTORE_PASSWORD: The keystore password.

    • KEY_ALIAS: The key alias for the keystore.

    • KEY_PASSWORD: The password for the key alias.


You can use these secrets in your workflow steps to securely sign your Android app.

Step 5: Trigger the Pipeline

After you've created the workflow, any push to the main branch will automatically trigger the CI/CD pipeline. You can also manually trigger it from the GitHub Actions tab in your repository.

Step 6: Monitoring and Debugging

Monitor the progress of your CI/CD pipeline directly from the GitHub Actions tab in your repository. If any issues arise, check the logs for error messages, and adjust your configuration accordingly.

Conclusion:

By following this comprehensive example and the steps provided, you can set up a powerful CI/CD pipeline for your Android project using GitHub Actions. This level of automation can significantly enhance your development workflow, ensuring that your app is built, tested, and deployed reliably.


As you become more familiar with this setup, you can further customize and expand your pipeline to meet the specific needs of your Android project.