avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

How I Automated Android Packaging

Wed Jan 08 2020

Why not get automated in generating an apk?

Though the official guide tells us how to generate our builds by clicking one and another menu options in Android Studio, but we don't always have to generate every release artifact manually, nowadays we can automate almost every kind of software artifact with docker.

A signed release apk or aab file can be easily generated from Linux commandline if you have configured your signing and packaging options in your build.gradle file. And there is no difference in doing it in a docker pipeline.

I've already automated my packaging procedure in two steps:

1. Prepare a docker image that installed all the env we need to build an android artifact.

# I prefer an enrironment based on ubuntu.
FROM ubuntu:bionic

MAINTAINER [email protected]

## Install openjdk and other tools that might be needed.
RUN apt-get update && apt-get upgrade -y && apt-get install git openjdk-8-jdk curl unzip -y

# Configure your target Android VERSIONS
ENV SDK_URL="https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip" \
    ANDROID_HOME="/usr/local/android-sdk" \
    ANDROID_VERSION=29 \
    ANDROID_BUILD_TOOLS_VERSION=29.0.2

# Download and install Android SDK
RUN mkdir "$ANDROID_HOME" .android \
    && cd "$ANDROID_HOME" \
    && curl -o sdk.zip $SDK_URL \
    && unzip sdk.zip \
    && rm sdk.zip \
    && yes | $ANDROID_HOME/tools/bin/sdkmanager --licenses \

# Install Android Build Tool and Libraries
&& $ANDROID_HOME/tools/bin/sdkmanager --update \
&& $ANDROID_HOME/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS_VERSION}" \
    "platforms;android-${ANDROID_VERSION}" \
    "platform-tools" \
    "ndk-bundle" \
&& mkdir /application

WORKDIR /application

2. Configure pipeline on Gitlab so that whenever I push code to the repo, the automation jobs get triggered

Edit .gitlab-ci.yml

image: shinchven/android-build:29 # Use the building env image we have prepared at step 1.

autoBuild:
  only:
    - master
  artifacts: # One the pipeline job is done, artifact will be archieved to GitLab.
    expire_in: 1 month # And it will cleaned if you setup an expiration time.
    paths:
    - /Path/To/Your/Apk/Or/Aab
  script:
    - chmod +x ./gradlew # the gradle script file might need execution permission.
    - ./gradlew lint # check your code style
    - ./gradlew bundle # generate artifact
    - ./gradlew build

Thanks to the automation, now I only need to focus on my coding. When I am done with coding, I just push my code to my private repo and leave all the testing and packaging jobs to the CI server. After a cup of coffee, the artifact is there for me to grab.

And one other advantage of automation

Is that if you lint, test and build your release apk/aab every time you commit your code, your face won't be punched by any lint and proguard errors when you need to generate a new release in a hurry.