sampingantech / sampingan-android-docker

An optimized docker image includes Android & Kotlin sdk.
https://hub.docker.com/r/sampingan/android
MIT License
1 stars 0 forks source link

Docker Android Build Box

docker icon Docker Image CI

Introduction

An optimized docker image includes Android & Kotlin SDK.

Weekly image build to get the latest base image and updated packages.

What Is Inside

It includes the following components:

Pull Docker Image

The docker image is publicly automated build on Docker Hub based on the Dockerfile in this repo, so there is no hidden stuff in it. To pull the latest docker image:

docker pull sampingan/android:latest

Hint: You can use a tag to a specific stable version, rather than latest of docker image, to avoid breaking your build. e.g. sampingan/android:1.22.0. Checkout Tags (bottom of this page) to see all the available tags.

Usage

Use the image to build an Android project

You can use this docker image to build your Android project with a single docker command:

cd <android project directory>  # change working directory to your project root directory.
docker run --rm -v `pwd`:/project sampingan/android bash -c 'cd /project; ./gradlew build'

To build .aab bundle release, use ./gradlew bundleRelease:

cd <android project directory>  # change working directory to your project root directory.
docker run --rm -v `pwd`:/project sampingan/android bash -c 'cd /project; ./gradlew bundleRelease'

Run docker image with interactive bash shell:

docker run -v `pwd`:/project -it sampingan/android bash

Add the following arguments to the docker command to cache the home gradle folder:

-v "$HOME/.dockercache/gradle":"/root/.gradle"

e.g.

docker run --rm -v `pwd`:/project  -v "$HOME/.dockercache/gradle":"/root/.gradle"   sampingan/android bash -c 'cd /project; ./gradlew build'

Build an Android project with Bitbucket Pipelines

If you have an Android project in a Bitbucket repository and want to use the pipeline feature to build it, you can simply specify this docker image. Here is an example of bitbucket-pipelines.yml:

image: sampingan/android:latest

pipelines:
  default:
    - step:
        caches:
          - gradle
          - gradle-wrapper
          - android-emulator
        script:
          - bash ./gradlew assemble
definitions:
  caches:
    gradle-wrapper: ~/.gradle/wrapper
    android-emulator: $ANDROID_HOME/system-images/android-21

The caches are used to store downloaded dependencies from previous builds, to speed up the next builds.

Run an Android emulator in the Docker build machine

Using guidelines from...

...You can write a script to create and launch an ARM emulator, which can be used for running integration tests or instrumentation tests or unit tests:

#!/bin/bash

# Arm emulators can be quite slow. For this reason it is convenient
# to increase the adb timeout to avoid errors.
export ADB_INSTALL_TIMEOUT=30

# Download an ARM system image to create an ARM emulator.
sdkmanager "system-images;android-16;default;armeabi-v7a"

# Create an ARM AVD emulator, with a 100 MB SD card storage space. Echo "no"
# because it will ask if you want to use a custom hardware profile, and you don't.
# https://medium.com/@AndreSand/android-emulator-on-docker-container-f20c49b129ef
echo "no" | avdmanager create avd \
    -n Android_4.1_API_16 \
    -k "system-images;android-16;default;armeabi-v7a" \
    -c 100M \
    --force

# Launch the emulator in the background
$ANDROID_HOME/emulator/emulator -avd Android_4.1_API_16 -no-skin -no-audio -no-window -no-boot-anim -gpu off &

# Note: You will have to add a suitable time delay, to wait for the emulator to launch.

Note that x86_64 emulators are not currently supported. See Issue #18 for details.

Choose Java Version

Both Java 1.8 and Java 11 are installed, jenv can be used to switch different version of java. The default java version is 11.

The following example set java version to 8:

. $HOME/.bash_profile   # Enable jenv
jenv local 8            # Set current Java env to 8

Docker Build Image

If you want to build the docker image by yourself, you can use following command. The image itself is around 5 GB, so check your free disk space before building it.

docker build -t android .

Tags

You can use a tag to a specific stable version, rather than latest of docker image, to avoid breaking your build. For example sampingan/android:1.22.0

Note: versions 1.0.0 up to 1.17.0 included every single Build Tool version and every Android Platform version available. This generated large Docker images, around 5 GB. Newer versions of android only include a subset of the newest Android Tools, so the Docker images are smaller.

2.0.0

BREAKING CHANGES

1.23.1

1.23.0

NOTE: missed this tag in DockerHub due to a github action error, should use 1.23.1 instead.

1.22.0

1.21.1

1.21.0

1.20.0

1.19.0

1.18.0

1.17.0

1.16.0

1.15.0

1.14.0

1.13.0

1.12.0

1.11.2

1.11.1

1.11.0

1.10.0

1.9.0

1.8.0

1.7.0

1.6.0

1.5.1

1.5.0

1.4.0

1.3.0

1.2.0

1.1.2

1.1.1

1.1.0

1.0.0

Contribution

If you want to enhance this docker image or fix something, feel free to send pull request.

References