Windows + vmware + ubuntu+docker + docker-android实现Android模拟器构建和启动
文章目录
- 引言
- 编译启动过程
- 玩下adb
- 最后
引言
Windows + vmware + ubuntu+docker+docker-android实现Android模拟器启动
编译启动过程
#下载docker-android
git clone https://github.com/budtmo/docker-android.git
maqi@ubuntu:~/docker-android$ git remote -v
origin https://github.com/budtmo/docker-android.git (fetch)
origin https://github.com/budtmo/docker-android.git (push)
#进入
cd docker-android
#构建示例
下面为你针对 base, emulator, genymotion, pro-emulator, pro-emulator_headless 这几个项目类型,分别给出构建示例。
1. 构建 base 项目
base 项目不涉及 Android 版本,只需指定任务、项目和发布版本。
bash
./app.sh build base v1.0
build 是任务,表示进行构建操作。
base 是项目类型。
v1.0.0 是发布版本。
2. 构建 emulator 项目
emulator 项目需要指定 Android 版本。
bash
./app.sh build emulator v1.0 10.0
build 是任务。
emulator 是项目类型。
v1.0.0 是发布版本。
10.0 是 Android 版本。
3. 构建 genymotion 项目
genymotion 项目不涉及 Android 版本,只需指定任务、项目和发布版本。
bash
./app.sh build genymotion v1.0
build 是任务。
genymotion 是项目类型。
v1.0.0 是发布版本。
4. 构建 pro-emulator 项目
pro-emulator 项目需要指定 Android 版本。
bash
./app.sh build pro-emulator v1.0 11.0
build 是任务。
pro-emulator 是项目类型。
v1.0.0 是发布版本。
11.0 是 Android 版本。
5. 构建 pro-emulator_headless 项目
pro-emulator_headless 项目需要指定 Android 版本。
bash
./app.sh build pro-emulator_headless v1.0 12.0
build 是任务。
pro-emulator_headless 是项目类型。
v1.0.0 是发布版本。
12.0 是 Android 版本。
./app.sh build base v1.0
budtmo/docker-android:base_v1.0 or budtmo/docker-android:base
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
Install the buildx component to build images with BuildKit:
https://docs.docker.com/go/buildx/
Sending build context to Docker daemon 27.42MB
Step 1/8 : FROM appium/appium:v2.16.2-p0
v2.16.2-p0: Pulling from appium/appium
Digest: sha256:6e027f51040c5656eef82ba5a0670bf4f8d758160fefabec3bed3b044986fb43
Status: Downloaded newer image for appium/appium:v2.16.2-p0
---> cbd9e2a65aa8
Step 2/8 : LABEL maintainer "Budi Utomo <budtmo.os@gmail.com>"
---> Running in 27bf326d3e72
---> Removed intermediate container 27bf326d3e72
---> 0583bdaf2399
Step 3/8 : USER root
---> Running in 4ec6c4bcb706
---> Removed intermediate container 4ec6c4bcb706
---> 0ac4819be397
Step 4/8 : RUN apt-get -qqy update && apt-get -qqy install --no-install-recommends socat supervisor && apt autoremove -y && apt clean all && rm -rf /var/lib/apt/lists/*
---> Running in 207a65e0a692
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package python3-pkg-resources.
(Reading database ... 18485 files and directories currently installed.)
Preparing to unpack .../python3-pkg-resources_45.2.0-1ubuntu0.2_all.deb ...
Unpacking python3-pkg-resources (45.2.0-1ubuntu0.2) ...
Selecting previously unselected package supervisor.
Preparing to unpack .../supervisor_4.1.0-1ubuntu1_all.deb ...
Unpacking supervisor (4.1.0-1ubuntu1) ...
Selecting previously unselected package socat.
Preparing to unpack .../socat_1.7.3.3-2_amd64.deb ...
Unpacking socat (1.7.3.3-2) ...
Setting up python3-pkg-resources (45.2.0-1ubuntu0.2) ...
Setting up supervisor (4.1.0-1ubuntu1) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Setting up socat (1.7.3.3-2) ...
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 0 to remove and 28 not upgraded.
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
---> Removed intermediate container 207a65e0a692
---> 88e7bb12bd16
Step 5/8 : RUN apt-get -qqy update && apt-get -qqy --no-install-recommends install python3-pip && apt-get clean && rm -rf /var/lib/apt/lists/* && update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
---> Running in 591b85d4cd5b
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package python-pip-whl.
(Reading database ... 18687 files and directories currently installed.)
Preparing to unpack .../0-python-pip-whl_20.0.2-5ubuntu1.11_all.deb ...
Unpacking python-pip-whl (20.0.2-5ubuntu1.11) ...
Selecting previously unselected package python3-lib2to3.
Preparing to unpack .../1-python3-lib2to3_3.8.10-0ubuntu1~20.04_all.deb ...
Unpacking python3-lib2to3 (3.8.10-0ubuntu1~20.04) ...
Selecting previously unselected package python3-distutils.
Preparing to unpack .../2-python3-distutils_3.8.10-0ubuntu1~20.04_all.deb ...
Unpacking python3-distutils (3.8.10-0ubuntu1~20.04) ...
Selecting previously unselected package python3-setuptools.
Preparing to unpack .../3-python3-setuptools_45.2.0-1ubuntu0.2_all.deb ...
Unpacking python3-setuptools (45.2.0-1ubuntu0.2) ...
Selecting previously unselected package python3-wheel.
Preparing to unpack .../4-python3-wheel_0.34.2-1ubuntu0.1_all.deb ...
Unpacking python3-wheel (0.34.2-1ubuntu0.1) ...
Selecting previously unselected package python3-pip.
Preparing to unpack .../5-python3-pip_20.0.2-5ubuntu1.11_all.deb ...
Unpacking python3-pip (20.0.2-5ubuntu1.11) ...
Setting up python3-wheel (0.34.2-1ubuntu0.1) ...
Setting up python-pip-whl (20.0.2-5ubuntu1.11) ...
Setting up python3-lib2to3 (3.8.10-0ubuntu1~20.04) ...
Setting up python3-distutils (3.8.10-0ubuntu1~20.04) ...
Setting up python3-setuptools (45.2.0-1ubuntu0.2) ...
Setting up python3-pip (20.0.2-5ubuntu1.11) ...
update-alternatives: using /usr/bin/pip3 to provide /usr/bin/pip (pip) in auto mode
update-alternatives: warning: not replacing /usr/bin/pip with a link
---> Removed intermediate container 591b85d4cd5b
---> 7158387b34a4
Step 6/8 : ARG DOCKER_ANDROID_VERSION=test-version
---> Running in 893a0a103a3b
---> Removed intermediate container 893a0a103a3b
---> cfe4f73892fc
Step 7/8 : ENV DOCKER_ANDROID_VERSION=${DOCKER_ANDROID_VERSION}
---> Running in f8f65114fbeb
---> Removed intermediate container f8f65114fbeb
---> 078754b22380
Step 8/8 : EXPOSE 4723 5554 5555
---> Running in c1af0c39436e
---> Removed intermediate container c1af0c39436e
---> 193db025ab13
Successfully built 193db025ab13
Successfully tagged budtmo/docker-android:base_v1.0
#基础镜像构建成功
#开始自定义镜像
maqi@ubuntu:~/docker-android$ ./app.sh build emulator
Release Version (v2.0.0-p0|v2.0.0-p1|etc) : v1.0
Android Version (9.0|10.0|11.0|12.0|13.0|14.0) : 11.0
#构建成功
Step 27/29 : STOPSIGNAL SIGTERM
---> Running in e957f32ce917
---> Removed intermediate container e957f32ce917
---> 41cb968da678
Step 28/29 : ENV DEVICE_TYPE=emulator
---> Running in 5014ab25ed79
---> Removed intermediate container 5014ab25ed79
---> c4041a0a6967
Step 29/29 : ENTRYPOINT ["/home/androidusr/docker-android/mixins/scripts/run.sh"]
---> Running in c8cee0549c59
---> Removed intermediate container c8cee0549c59
---> 4321fb8d1d72
Successfully built 4321fb8d1d72
Successfully tagged budtmo/docker-android:emulator_11.0_v1.0
#查找镜像
docker images
budtmo/docker-android emulator_11.0 4321fb8d1d72 About a minute ago 7.37GB
budtmo/docker-android emulator_11.0_v1.0 4321fb8d1d72 About a minute ago 7.37GB
#镜像说明:
在 Docker 中,标签是用来标识镜像的不同版本或用途的。budtmo/docker-android:emulator_11.0_v1.0 表示这是 Android 11.0 版本的第 v1.0 次发布,而 budtmo/docker-android:emulator_11.0 则通常表示这个 Android 11.0 版本的最新镜像。通过这种方式,用户可以选择使用特定版本的镜像,也可以使用最新版本的镜像。
# 测试镜像
./app.sh test emulator
maqi@ubuntu:~/docker-android$ ./app.sh test emulator
Release Version (v2.0.0-p0|v2.0.0-p1|etc) : ^C
maqi@ubuntu:~/docker-android$ ./app.sh test emulator
Release Version (v2.0.0-p0|v2.0.0-p1|etc) : v1.0
Android Version (9.0|10.0|11.0|12.0|13.0|14.0) : 11.0
budtmo/docker-android:emulator_11.0_v1.0 or budtmo/docker-android:emulator_11.0
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
BuildKit is currently disabled; enable it by removing the DOCKER_BUILDKIT=0
environment-variable.
Sending build context to Docker daemon 27.42MB
Step 1/29 : ARG DOCKER_ANDROID_VERSION
Step 2/29 : FROM budtmo/docker-android:base_${DOCKER_ANDROID_VERSION}
---> 193db025ab13
Step 3/29 : ARG EMULATOR_ANDROID_VERSION
---> Using cache
---> a78f0a51dd7a
Step 4/29 : ARG EMULATOR_API_LEVEL
---> Using cache
---> cd3f832ebb38
Step 5/29 : ENV EMULATOR_ANDROID_VERSION=${EMULATOR_ANDROID_VERSION} EMULATOR_API_LEVEL=${EMULATOR_API_LEVEL} EMULATOR_SYS_IMG=x86_64 EMULATOR_IMG_TYPE=google_apis EMULATOR_BROWSER=chrome
---> Using cache
---> 383b46a5735d
Step 6/29 : ENV PATH ${PATH}:${ANDROID_HOME}/build-tools
---> Using cache
---> 92c8cb8d5ece
Step 7/29 : RUN yes | sdkmanager --licenses && sdkmanager "platforms;android-${EMULATOR_API_LEVEL}" "system-images;android-${EMULATOR_API_LEVEL};${EMULATOR_IMG_TYPE};${EMULATOR_SYS_IMG}" "emulator" && ln -s ${ANDROID_HOME}/emulator/emulator /usr/bin/
---> Using cache
---> 908ef403a384
Step 8/29 : RUN apt-get -qqy update && apt-get -qqy install --no-install-recommends ffmpeg feh libxcomposite-dev menu openbox x11vnc xterm && apt autoremove -y && apt clean all && rm -rf /var/lib/apt/lists/*
---> Using cache
---> ef64d5b391bc
Step 9/29 : ENV NOVNC_VERSION="1.4.0" WEBSOCKIFY_VERSION="0.11.0" OPT_PATH="/opt"
---> Using cache
---> 5e526f84a0ab
Step 10/29 : RUN wget -nv -O noVNC.zip "https://github.com/novnc/noVNC/archive/refs/tags/v${NOVNC_VERSION}.zip" && unzip -x noVNC.zip && rm noVNC.zip && mv noVNC-${NOVNC_VERSION} ${OPT_PATH}/noVNC && wget -nv -O websockify.zip "https://github.com/novnc/websockify/archive/refs/tags/v${WEBSOCKIFY_VERSION}.zip" && unzip -x websockify.zip && mv websockify-${WEBSOCKIFY_VERSION} ${OPT_PATH}/noVNC/utils/websockify && rm websockify.zip && ln ${OPT_PATH}/noVNC/vnc.html ${OPT_PATH}/noVNC/index.html
---> Using cache
---> 9b21318a92e6
Step 11/29 : ENV DISPLAY=:0 SCREEN_NUMBER=0 SCREEN_WIDTH=1600 SCREEN_HEIGHT=900 SCREEN_DEPTH=24+32 VNC_PORT=5900 WEB_VNC_PORT=6080
---> Using cache
---> b9737d729c07
Step 12/29 : EXPOSE 5900 6080
---> Using cache
---> 891c996f15f5
Step 13/29 : RUN rm -rf ${SCRIPT_PATH}
---> Using cache
---> 495412a21358
Step 14/29 : ENV SCRIPT_PATH="docker-android"
---> Using cache
---> 1d100718d943
Step 15/29 : ENV WORK_PATH="/home/androidusr"
---> Using cache
---> 5733c22096a4
Step 16/29 : ENV APP_PATH=${WORK_PATH}/${SCRIPT_PATH}
---> Using cache
---> 510bec1a654b
Step 17/29 : RUN mkdir -p ${APP_PATH}
---> Using cache
---> fa953bad2143
Step 18/29 : COPY mixins ${APP_PATH}/mixins
---> Using cache
---> a52257e25158
Step 19/29 : COPY cli ${APP_PATH}/cli
---> Using cache
---> a7ea9beed873
Step 20/29 : RUN chown -R 1300:1301 ${APP_PATH} && pip install --quiet -e ${APP_PATH}/cli
---> Using cache
---> f6eae1a3eafa
Step 21/29 : RUN echo ${APP_PATH}/mixins/configs/display/.fehbg >> /etc/xdg/openbox/autostart
---> Using cache
---> 98772b2eee10
Step 22/29 : USER 1300:1301
---> Using cache
---> 9574c1d6c7c5
Step 23/29 : ENV LOG_PATH=${WORK_PATH}/logs WEB_LOG_PORT=9000
---> Using cache
---> e9984bb40d56
Step 24/29 : EXPOSE 9000
---> Using cache
---> 4c7a521d7a17
Step 25/29 : RUN mkdir -p ${LOG_PATH}
---> Using cache
---> 46b19bdf90d0
Step 26/29 : RUN mkdir -p "${WORK_PATH}/.config/Android Open Source Project" && echo "[General]\nshowNestedWarning=false\n" > "${WORK_PATH}/.config/Android Open Source Project/Emulator.conf"
---> Using cache
---> 2b1da6ef9ba1
Step 27/29 : STOPSIGNAL SIGTERM
---> Using cache
---> 41cb968da678
Step 28/29 : ENV DEVICE_TYPE=emulator
---> Using cache
---> c4041a0a6967
Step 29/29 : ENTRYPOINT ["/home/androidusr/docker-android/mixins/scripts/run.sh"]
---> Using cache
---> 4321fb8d1d72
Successfully built 4321fb8d1d72
Successfully tagged budtmo/docker-android:emulator_11.0_v1.0
test_create_device (src.tests.device.test_device.TestDevice) ... ok
test_adb_name (src.tests.device.test_emulator.TestEmulator) ... ok
test_check_adb_command (src.tests.device.test_emulator.TestEmulator) ... ok
test_check_adb_command_out_of_attempts (src.tests.device.test_emulator.TestEmulator) ... ok
test_create_device_exists (src.tests.device.test_emulator.TestEmulator) ... ok
test_create_device_not_exist (src.tests.device.test_emulator.TestEmulator) ... ok
test_initialisation_config_not_exist (src.tests.device.test_emulator.TestEmulator) ... ok
test_initialisation_device_exists (src.tests.device.test_emulator.TestEmulator) ... ok
test_initialisation_device_not_exist (src.tests.device.test_emulator.TestEmulator) ... ok
test_invalid_android_version (src.tests.device.test_emulator.TestEmulator) ... ok
test_invalid_device (src.tests.device.test_emulator.TestEmulator) ... ok
test_boolean_converter_with_empty (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_boolean_converter_with_invalid_format (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_boolean_converter_with_invalid_str (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_boolean_converter_with_valid_str (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_get_env_value_from_invalid_key (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_get_env_value_from_valid_key (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_get_env_value_with_empty_string (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_get_env_value_with_invalid_format (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_symlink (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_symlink_already_exist (src.tests.helper.test_helper.TestHelperMethods) ... ok
test_symlink_file_not_exists (src.tests.helper.test_helper.TestHelperMethods) ... ok
----------------------------------------------------------------------
XML: /home/androidusr/docker-android/cli/test-results/xunit.xml
Name Stmts Miss Cover
-------------------------------------------------
src/__init__.py 0 0 100%
src/application/__init__.py 26 11 58%
src/constants/DEVICE.py 5 0 100%
src/constants/ENV.py 34 0 100%
src/constants/__init__.py 1 0 100%
src/device/__init__.py 117 56 52%
src/device/emulator.py 146 50 66%
src/helper/__init__.py 28 1 96%
src/logger/__init__.py 2 0 100%
-------------------------------------------------
TOTAL 359 118 67%
----------------------------------------------------------------------
Ran 22 tests in 0.392s
OK
# 推送镜像
# 如果你有推送权限,且镜像仓库等配置正确,可执行此步骤
./app.sh push emulator
# 启动镜像
docker run --rm -d -p 6080:6080 -p 5900:5900 -p 5555:5555 \
-e EMULATOR_DEVICE="Nexus 7" \
-e WEB_VNC=true \
--device /dev/kvm \
-e EMULATOR_ADDITIONAL_ARGS="-cores 4 -memory 8192" \
-e EMULATOR_DATA_PARTITION=9000m \
--name android-container budtmo/docker-android:emulator_11.0_v1.0
# 检查进度
docker exec -it android-container cat device_status
玩下adb
adb shell
generic_x86_64_arm64:/ $ unmae -r
/system/bin/sh: unmae: inaccessible or not found
127|generic_x86_64_arm64:/ $ uname -r
5.4.249-android11-2-00004-g212a7aaded26-ab10688362
generic_x86_64_arm64:/ $ ls
acct apex bugreports config data debug_ramdisk dev init linkerconfig metadata odm proc res storage system vendor
adb_keys bin cache d data_mirror default.prop etc init.environ.rc lost+found mnt oem product sdcard sys system_ext
最后
1.使用WSL+kernel内核kvm加速体验可能会更好。这种嵌套虚拟化的方式非常消耗性能不建议使用。
2.可以结合Gmsaas远程模拟器使用,实现更多模拟器自由
3.最终效果见顶部截图,访问localhost:6080或者vnc访问5900端口,