Step-by-Step Guide Setting Up Docker for Android Development

Step 1: Update System packages

sudo apt-get update

The reason for updating is that sometimes some packages of Docker are not working well, so for that reason we update them.

APT is stands for Advanced Packaging Tool.

Step 2: Docker installation

sudo apt-get install -y docker.io

Here we use -y means automatically assign yes permission for automation instead of manually assigning.

Step 3: start and enable setup for run

sudo systemctl start docker
sudo systemctl enable docker

Step 4: Verify docker version

docker --version

Step 5: Create a directory for android project

mkdir android-docker-setup
cd android-docker-setup

Step 6: create a new Dockerfile

nano Dockerfile

Step 7: Add configuration scripts in the Dockerfile

Note: You need to change configuration as per your dependencies, Required JDK, Required SDK etc. in the below script.

# Use the official OpenJDK image from Docker Hub
FROM openjdk:11-jdk

# Install necessary packages and dependencies
RUN apt-get update && apt-get install -y \
wget \
unzip \
git \
&& rm -rf /var/lib/apt/lists/*

# Set environment variables
ENV ANDROID_SDK_ROOT /opt/android-sdk
ENV PATH ${PATH}:${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin:${ANDROID_SDK_ROOT}/platform-tools

# Download and install Android SDK command-line tools
RUN mkdir -p ${ANDROID_SDK_ROOT}/cmdline-tools \
&& cd ${ANDROID_SDK_ROOT}/cmdline-tools \
&& wget https://dl.google.com/android/repository/commandlinetools-linux-8512546_latest.zip -O commandlinetools.zip \
&& unzip commandlinetools.zip -d ${ANDROID_SDK_ROOT}/cmdline-tools \
&& mv ${ANDROID_SDK_ROOT}/cmdline-tools/cmdline-tools ${ANDROID_SDK_ROOT}/cmdline-tools/latest \
&& rm commandlinetools.zip

# Install SDK packages
RUN yes | sdkmanager --licenses \
&& sdkmanager "platform-tools" "platforms;android-32" "build-tools;32.0.0"

# Clone your Android project into the container
RUN git clone https://github.com/yourusername/your-android-project.git /usr/src/app

# Set working directory
WORKDIR /usr/src/app

# Make the gradlew script executable
RUN chmod +x ./gradlew

# Pre-install project dependencies (Optional, but speeds up first build)
RUN ./gradlew build --no-daemon || true

# Default command to run when starting the container
CMD ["./gradlew", "assembleDebug"]

Here, From Baseimage is the first stage in the Dockerfile. And we use OpenJDK images from DockerHub, which contains many free images. We only need to use them directly.

‘\’ is a continuation character. that basically split long commands into multiple lines that were tailored for readability.

‘wget’ is a command line tool for downloading packages from the internet.

unzip‘ is a command line tool used to extracting files from compressed zip archive.

git‘ is a command line tool is used for keep tracking and managing code. And it is famous version control tool.

&&‘ is logical expression that says ‘and then’.

rm -rf /var/lib/apt/lists/*’ this commands removes all the files from /var/lib/apt/lists/ directory. Basically, this directory stores information about available software packages.

‘r’ stands for recursive.

The ‘f’ flag stands for force.

‘-rf’ is a very strong combination that basically tells the ‘rm’ to forcefully remove the directory and files.

Here we use WORKDIR for path assignment.

If you do not write this RUN chmod +x./gradlew line at that time, it will build successfully, but it will not run. and chmod +x that are tailored for assigning executable permission.

The CMD command is always used after the RUN command.

After writing above scripts then Save and Exit nano:

  • Press Ctrl + O to write out (save) the file.
  • Press Enter to confirm the filename.
  • Press Ctrl + X to exit nano.

Step 8: Build and Run the Docker Image

docker build -t android-project-image
docker run -it android-project-image

Here, (‘-t’) stands for tag, which basically allows you to give a name or tag with a Docker image. In this scenario, android-project-image is the tag or name that we want to build. and

(‘-i’) stands for interactive and basically allows us to interact with command-line. It keeps STDIN (standard input) open even if not attached.

If we combine (‘-it’), that means that it allows you to run a tagged or named container with an interactive command interface.

Once you write the build command, the image will be created, and through this image, a Docker container will run.

You can check your container working status through ‘Docker -ps‘ command.

Step 9: Finally, share and run the docker image.

export the docker image

docker save android-project-image > android-project-image.tar

Here, you download the android-project-image.tar file on your local machine and share it with others.

import and run the docker image

docker load -i android-project-image.tar

Here, on your local machine, import the Docker image from the tar file and run it using the below command.

docker run -it android-project-image

Setting Up Docker for Android Development

Imagine that one IT solution company creates projects for Android development. After a long time, another developer joins the company, and at that time, the developer manually needs to setup the Android project environment configuration, which is more time-consuming and requires more human effort. But imagine that if they have the same configuration image that performs the entire setup tasks automatically, that is amazing! The developer has only one main task, which is the Docker image that sets up the entire configuration.

Similar Reads

Primary Terminology

Docker is a platform that allows you to put all applications and their dependencies in standardized units called containers. These containers are lightweight and portable, allowing for continuity across environments (operating systems and hardware)....

Step-by-Step Guide Setting Up Docker for Android Development

Step 1: Update System packages...

Conclusion

Forget about manual setup planning! This guide shows how Docker simplifies Android development. Using a Docker image, new developers can skip the hassle and jump right into coding, as well as solve the famous quote that “code runs on my machine only.” The image creates a consistent environment for everyone, facilitating collaboration and shared work. Now, building Android apps is faster and more efficient!...

Docker with Android Development – FAQs

Can we use Docker for other scenarios?...