NixOS Android Studio development environment (Docker based)
An Android Studio development environment can be somewhat cumbersome to set up under NixOS, so a solution using a Docker container is described in this article.
Normally instructions at NixOS Android suffice, but I'm experiencing trouble with additional functionality (e.g. The Gradle Wrapper).
Hence this Docker based solution. Use the Makefile to build the container make rebuild
and subsequently start the container make up
and enter a development shell make shell
which has a ~/Development
binding (change to your path if needed). From within the development shell use /opt/android-studio/bin/studio.sh &
to start Android Studio (its GUI is displayed on your computer — beware: you'll probably need additional configuration if you're using Wayland).
Android Studio
Download the appropriate tarball from https://developer.android.com/studio#downloads and save it next to your Dockerfile
(in the same directory) and adjust the tarball's name in the Dockerfile
(currently android-studio-2022.2.1.20-linux.tar.gz
).
Dockerfile
section:
1COPY android-studio-2022.2.1.20-linux.tar.gz android-studio-2022.2.1.20-linux.tar.gz
2RUN mkdir -p /opt; \
3 tar xvzf android-studio-2022.2.1.20-linux.tar.gz -C /opt
Dockerfile
1# Android development environment.
2
3FROM debian:latest
4
5# Timezone is also in docker-compose file.
6ENV HOME /root
7ENV TZ Europe/Amsterdam
8ENV SHELL /bin/bash
9
10RUN apt-get update; \
11 apt-get upgrade -y; \
12 apt-get install -y procps sudo curl less vim-nox zip git libssl-dev bat exa fd-find; \
13 apt-get clean
14
15RUN sed -i "s#\smain\s*\$# main contrib non-free#" /etc/apt/sources.list
16RUN apt-get update
17RUN apt-get install -y openjdk-17-jdk android-sdk --no-install-recommends
18
19# Create a non-root account with your user's uid and guid.
20RUN useradd -ms /bin/bash --uid 1000 --gid 100 android
21# usermod -G audio,video android;
22
23RUN echo "android ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
24
25# https://wiki.debian.org/AndroidStudio
26# /opt/android-studio/bin/studio.sh &
27#
28COPY android-studio-2022.2.1.20-linux.tar.gz android-studio-2022.2.1.20-linux.tar.gz
29RUN mkdir -p /opt; \
30 tar xvzf android-studio-2022.2.1.20-linux.tar.gz -C /opt
31
32USER android
33WORKDIR /home/android
34ENV HOME /home/android
35
36# The DISPLAY variable is required to display on your desktop.
37ENV PS1='$ '
38ENV DISPLAY=":0"
39
40# For access to X-server use the following command:
41# xhost +LOCAL:
42#
43# RUN todo
44ENTRYPOINT ["/bin/bash"]
docker-compose.yaml
1version: "2.0"
2services:
3 "android":
4 image: android-dev
5 build: .
6 stdin_open: true
7 tty: true
8 privileged: true
9 ipc: host
10 environment:
11 - TZ=Europe/Amsterdam
12 network_mode: host
13 volumes:
14 - "/tmp/.X11-unix/:/tmp/.X11-unix/:ro"
15 - "~/Development:/home/android/Development:rw"
16 - "~/.Xauthority:/home/android/.Xauthority:rw"
Makefile
1# Brings up the Docker container, which automatically starts an Android
2# development environment. The attach can be used to connect to the
3# command prompt in the container, where e.g. a Ctrl-c can be used to
4# force a stop.
5#
6
7NAME="docker-android-1"
8
9all: help
10
11up:
12 xhost +LOCAL:
13 docker-compose up -d
14
15down:
16 sync
17 docker-compose down
18
19# If problems persist after a force-down then manually restart Docker daemon.
20force-down:
21 sync
22 docker rm -f $(NAME)
23
24ls:
25 docker ps -a
26
27rebuild:
28 xhost +LOCAL:
29 docker-compose build --no-cache
30
31build:
32 xhost +LOCAL:
33 docker-compose build
34
35attach:
36 xhost +LOCAL:
37 docker attach $(NAME)
38
39shell:
40 #xhost +LOCAL:
41 docker exec -it $(NAME) /bin/bash
42
43help:
44 @grep '^[^ #:]\+:' Makefile | sed -e 's/:[^:]*//g'
45 @echo "Use make -s for silent execution (e.g. make -s ls)"
46 @echo "To start Android studio use make shell for an interactive shell and type the following command:"
47 @echo "/opt/android-studio/bin/studio.sh &"
Demonstration
See the screendump below.