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.

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).

  • Lightweight: The Docker image basically takes up very little memory space on your computer. Imagine a small toolbox that contains necessary dependencies and configurations instead of entire workshop tools. 
  • Portable: Just like shipping containers between ships and trucks, Docker containers can be used on any system with Docker installed, regardless of the operating system (Windows, macOS, or Linux).

Docker Image is a blueprint for creating containers. It contains many instructions (codes, libraries) that are used for creating software environments. These images are portable and lightweight, and they can run consistently with any system through Docker.

Android Studio is a free and partially open-source platform. The name suggests that it is partially open-source because the core IDE is based on IntelliJ IDEA, under the Apache 2.0 license. But as we know, Android Studio contains additional functionalities and components that were developed by Google. For that reason, it might not be fully open-source. In another way, we say that this is one of the famous shops for delivering delicious applications.

GitHub: It is a free and open-source version control system which is developed by Microsoft. That provides,

  • Storage for our codes
  • Continuously track our code.
  • the capability of sharing with another
  • project collaboration support

ECR is a public registry, and anyone can search, download, and even push Docker images.

Docker Hub is a private registry, especially for AWS. It allows you to securely store and manage images within AWS. An unauthorized person cannot access and manage the images.

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

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?

Docker has a large scope, it is not only beneficial for Android development, but it can be used with various tech stacks like web development and deployment, microservice architecture, CI/CD, testing environments, and data processing pipelines.

How many components do dockers contain?

Docker Engine: This is the main component that manages the containers. By using Docker Engine, you can create Docker images and use them to build Docker containers that can efficiently run your application.

Docker Images: These are the blueprints, and they contain a set of instructions like code, libraries, etc. Those blueprints are static until they are updated. that read-only templates for containers.

Docker Containers: It is a lightweight and isolated environment built from Docker images. Container that contains the applications that run on it. Containers can be started, stopped, and recreated.

What is the main difference between ECS and Docker?

Docker provides tools and a platform for building and managing various containers. In which the developer uses the containerized application that is running inside it.

On the other hand, ECS is a fully managed and secure service that is offered by AWS. It provides features like auto-scaling, automated cluster management, load balancing, and integration with other AWS services.

Can we upload Docker images to the ECR or Docker-Hub instead of downloading them on a local machine?

The answer is no, because ECS itself doesn’t have any upload functions. In short, skip the process of downloading images. Push your private images to ECR (act as your own fridge) or use your public images directly from Docker-Hub (act like a public market) with ECS (act as your oven).

Here, we understand with the help of a pizza recipe. In the context of pizza recipes, ECR is a secret recipe (private project) that is used in your AWS kitchen. where Docker Hub is like a public market where anyone can share or grab (access) recipes (images).

  • Fridge is ECR
  • public market is Docker Hub
  • oven is ECS

How much time it will take to build Gradle Project?

can vary and depend on several factors, such as

  • project size and complexity
  • available resources
  • network speed
  • Gradle configuration

Typically, building time,

small project: a simple project that contains few dependencies and might take one or two minutes.

medium project: A moderate-level project might take several minutes.

large project: A complex project might take 10 minutes or more.