构建Android编译环境(CentOS 8)

备注

我最初想通过CentOS 8来作为构建Android开发编译环境,不过参考LineageOS的官方文档,社区主要采用Ubuntu,所以我近期实践采用 构建Android编译环境(Ubuntu)

准备工作

我采用Android的编译环境是在 Docker Atlas 部署,通过 从Dockerfile构建Docker镜像 创建一个CentOS 8纯净环境,然后从初始环境安装必要的编译工具链来构建Android编译环境。

  • 在本地创建一个 studio 目录,进入这个目录,然后 - 如果你只是编译android系统,则存放以下名为 android-build 的Dockerfile - 如果你想开发android系统,则存放以下名为 android-studio 的Dockerfile

android-build
# Build Android Studio image with ssh:
# ------------------------------------
# docker build - < android-studio

# create container:
# -----------------
# docker run -itd --hostname android-studio --name android-studio local:android-studio

# create container with volume:
# -----------------------------
# docker volume create data
# docker run -itd --hostname android-studio --name android-studio -v data:/data local:android-studio

# create container with volume and static ip:
# -------------------------------------------
# docker volume create data
# docker network create --subnet=172.18.0.0/16 data-net
# docker run -itd --hostname android-studio --name android-studio -v data:/data \
#   --net data-net --ip 172.18.0.252 local:android-studio

# create container with volume and static ip, then limit resource and map port:
# -----------------------------------------------------------------------------
# docker volume create data
# docker network create --subnet=172.18.0.0/16 data-net
# docker run -itd --hostname android-studio --name android-studio -v data:/data \
#   --net data-net --ip 172.18.0.252 --memory=2048M --cpus="1.5" \
#   -p 2222:22 -p 8080:80 local:android-studio

FROM docker.io/centos:8
MAINTAINER vincent huatai <vincent@huatai.me>

RUN dnf clean all
RUN dnf install epel-release
RUN dnf install dnf-plugins-core
RUN dnf config-manager --set-enabled PowerTools
RUN dnf -y update
RUN dnf -y install which sudo openssh-clients openssh-server initscripts nmon nmap-ncat mlocate net-tools rsyslog file wget tar bzip2 screen sysstat unzip nfs-utils parted lsof man bind-utils gcc gcc-c++ make telnet flex autoconf automake ncurses-devel crontabs zlib-devel git

# Prepare sshd host key
RUN ssh-keygen -A

# add account "admin" and give sudo privilege
RUN groupadd -g 505 admin
RUN useradd -g 505 -u 505 -d /home/admin -m admin
RUN usermod -aG wheel admin
RUN echo "%wheel        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers

# Add ssh public key for login
RUN mkdir -p /home/admin/.ssh
COPY authorized_keys /home/admin/.ssh/authorized_keys
RUN chown -R admin:admin /home/admin/.ssh
RUN chmod 600 /home/admin/.ssh/authorized_keys
RUN chmod 700 /home/admin/.ssh
RUN mv /var/run/nologin /var/run/nologin.bak

# run service when container started - sshd
EXPOSE 22:2222
#CMD ["/usr/sbin/sshd", "-D"]

# ----------
# WANT run sshd and get a bash
# ENTRYPOINT will not be override by commandline
# ----------
ENTRYPOINT /usr/sbin/sshd && /bin/bash
android-studio
# Build Android Studio image with ssh:
# ------------------------------------
# docker build - < android-studio

# create container:
# -----------------
# docker run -itd --hostname android-studio --name android-studio local:android-studio

# create container with volume:
# -----------------------------
# docker volume create data
# docker run -itd --hostname android-studio --name android-studio -v data:/data local:android-studio

# create container with volume and static ip:
# -------------------------------------------
# docker volume create data
# docker network create --subnet=172.18.0.0/16 data-net
# docker run -itd --hostname android-studio --name android-studio -v data:/data \
#   --net data-net --ip 172.18.0.252 local:android-studio

# create container with volume and static ip, then limit resource and map port:
# -----------------------------------------------------------------------------
# docker volume create data
# docker network create --subnet=172.18.0.0/16 data-net
# docker run -itd --hostname android-studio --name android-studio -v data:/data \
#   --net data-net --ip 172.18.0.252 --memory=2048M --cpus="1.5" \
#   -p 2222:22 -p 8080:80 local:android-studio

FROM docker.io/centos:8
MAINTAINER vincent huatai <vincent@huatai.me>

RUN dnf clean all
RUN dnf install epel-release
RUN dnf install dnf-plugins-core
RUN dnf config-manager --set-enabled PowerTools
RUN dnf -y update
RUN dnf -y install which sudo openssh-clients openssh-server initscripts nmon nmap-ncat mlocate net-tools rsyslog file wget tar bzip2 screen sysstat unzip nfs-utils parted lsof man bind-utils gcc gcc-c++ make telnet flex autoconf automake ncurses-devel crontabs zlib-devel git

# Prepare sshd host key
RUN ssh-keygen -A

# add account "admin" and give sudo privilege
RUN groupadd -g 505 admin
RUN useradd -g 505 -u 505 -d /home/admin -m admin
RUN usermod -aG wheel admin
RUN echo "%wheel        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers

# Add ssh public key for login
RUN mkdir -p /home/admin/.ssh
COPY authorized_keys /home/admin/.ssh/authorized_keys
RUN chown -R admin:admin /home/admin/.ssh
RUN chmod 600 /home/admin/.ssh/authorized_keys
RUN chmod 700 /home/admin/.ssh
RUN mv /var/run/nologin /var/run/nologin.bak

# run service when container started - sshd
EXPOSE 22:2222
#CMD ["/usr/sbin/sshd", "-D"]

# ----------
# WANT run sshd and get a bash
# ENTRYPOINT will not be override by commandline
# ----------
ENTRYPOINT /usr/sbin/sshd && /bin/bash