Skip to content

Instantly share code, notes, and snippets.

@divadsn
Last active December 9, 2018 10:23
Show Gist options
  • Save divadsn/fa30eb0b2112320bfc78baac0f4c3b29 to your computer and use it in GitHub Desktop.
Save divadsn/fa30eb0b2112320bfc78baac0f4c3b29 to your computer and use it in GitHub Desktop.
Building Android using a pre-built Docker image on Linux or macOS (or even Windows).

Introduction

In this guide, I will show you how to deploy and run a pre-built build environment using Docker.
This guide is tested to run on Linux and macOS, but it should also work on Windows.

You are a pr0 thug dev and you only need the docker instruction? Click here.

Prerequisites

Your build machine should at least follow the minimum specs for building Android:

  • CPU: 4 cores with 2 GHz
  • Memory: 8 GB (recommended 12 GB if running with graphical environment)
  • Disk space: 170 GB (~50 GB for sync + 120 GB for ccache)
  • System type: x86_64

Additionally, you must make sure that virtualization is enabled on your machine.

IMPORTANT! Linux users running with 4.19 kernel won't be able to use Docker due to a bug with overlayfs, please downgrade to 4.18 for now or skip if the issue has been already fixed.

Install Docker

Linux

Alternatively, you can try your luck by using the auto install script:
curl -sSL https://get.docker.com/ | CHANNEL=stable sh

If your distro isn't recognized, try searching for docker in your package manager.

macOS

Download the .pkg file and open it with Finder to install Docker on macOS:
https://docs.docker.com/docker-for-mac/install/

Windows

Download and open the .exe file, follow the installer instructions on your screen:
https://docs.docker.com/docker-for-windows/install/

Setting up the build environment

Now it's time to get our build environment ready!

Persistent data

Docker containers have one big advantage: Once we are done with building Android, we can easily dispose of them. But this also means that our synchronized local repository and ccache data will be lost. To prevent this, we will use so-called docker volumes. They allow us to keep our data persistent between the container and the host.

Warning! You might consider using bind mounts instead volumes if your host doesn't allow you to access data of volumes.

To create them, we simply enter those two commands:

docker volume create build_repo
docker volume create build_ccache

Getting the pre-built image

The image I built and created was made with building Android Pie in mind. It was designed to be as slim as possible to reduce the image size. Therefore it can lack some common tools like a text editor, ssh client or wget. You can either make use of the sudo access and install missing packages using sudo apt-get install or clone my repo and customize the Dockerfile to fit your needs.

To pull the latest image from Docker Hub, simply enter:

docker pull divadsn/android-build:latest

If it fails, you can try building the image on your own:

git clone https://github.com/divadsn/docker-android-build && cd docker-android-build/
sudo make latest

Deploying and running our container

To create and run our container, we can use the following command syntax:

docker run -it --name <container_name> --hostname <hostname> -v <local_repo>:/repo -v <local_ccache>:/tmp/ccache --rm divadsn/android-build:latest

Now you are probably wondering what those arguments mean, right? Let me explain.

Argument Description
-it Allows docker to allocate a tty for the container process
--name <container_name> Identification name for our container
--hostname <hostname> Sets the specified name as containers hostname
-v src:dest Mounts src path on host or volume to dest path on container
--rm Cleans up the container and removes after exit

Example when using volumes:

docker run -it --name build --hostname buildbot -v build_repo:/repo -v build_ccache:/tmp/ccache --rm divadsn/android-build:latest

Example when using bind mounts:

docker run -it --name build --hostname buildbot -v /home/${USER}/build/repo:/repo -v /home/${USER}/build/ccache:/tmp/ccache --rm divadsn/android-build:latest

After the container has been created, you should be greeted with a message about sudo access and you should be able to execute commands as user docker in directory /repo. Now you can go ahead and initialize your local repository using repo init and sync the sources, everything is setup to be ready to use.

What's next?

Congratulations, you have now an environment for building Android on your local machine. You can do anything you would do normally, but with the difference of having it running separately as a container.

Keep in mind that if you have finished building, exiting the container by entering exit will cause Docker to delete the container when stopped. This also means that any additionally installed packages and modified files outside our volumes will no longer be available at the next start, and you will probably need to reinstall them.

Troubleshooting

Contact me on Telegram (https://t.me/divadsn) or write a comment. If you need a managment tool for Docker, then I suggest you to install Portainer, a easy to use web UI where you can delete unused containers or volumes and manage your local images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment