使用 Android 模拟器容器来实现持续集成测试
Android 模拟器容器
https://github.com/google/android-emulator-container-scripts/blob/master/REGISTRY.MD
Android 模拟器下载以及 Docker 镜像生成的脚本
https://android-developers.googleblog.com/2019/10/continuous-testing-with-new-android.html
Android 模拟器容器
https://github.com/google/android-emulator-container-scripts/blob/master/REGISTRY.MD
adb
https://github.com/google/android-emulator-container-scripts#communicating-with-the-emulator-in-the-container
web
https://github.com/google/android-emulator-container-scripts#make-the-emulator-accessible-on-the-web
Linux KVM
https://en.wikipedia.org/wiki/Kernel-based_Virtual_Machine
文档
https://github.com/google/android-emulator-container-scripts/blob/master/REGISTRY.MD#requirements-and-recommendations
#!/bin/bash
# 这个是我们将要运行的远程镜像
# 如果需要,Docker 会帮我们获取该镜像
DOCKER_IMAGE=us-docker.pkg.dev/android-emulator-268719/images/r-google-x64:30.0.23
# 这个是转发端口。最好选用高位端口,这样就不会干扰到 adb 扫描模拟器。
PORT=15555
# 这里会在后台启动容器
container_id=$(docker run -d \
-e "ADBKEY=$(cat ~/.android/adbkey)" --device /dev/kvm --publish \
8554:8554/tcp --publish $PORT:5555/tcp \
$DOCKER_IMAGE)
echo "The container is running with id: $container_id"
# 注意,您可能会碰到如下信息:
# failed to connect to localhost: 15555
# 这只不过表明容器还没有准备好
echo "Connecting to forwarded adb port."
adb connect localhost:$PORT
# 我们不得不等到 `docker ps` 显示为正常。
# 这一过程可能需要一段时间因为模拟器需要完全启动!
echo "Waiting until the device is ready"
adb wait-for-device
# 现在这个设备正在启动,或者接近启动完成。
# 我们只等 sys.boot_completed 的值被设置为1。
while [ "`adb shell getprop sys.boot_completed | tr -d '\r' `" != "1" ] ;
do
echo "Still waiting for boot.."
sleep 1;
done
# 现在你可以正常地使用模拟器了,举个例子:
# ./gradlew installDebug
# ./gradlew connectedAndroidTest
# 等等
echo "The device is ready"
echo "Run the following command to stop the container:"
echo "docker stop ${container_id}"
Android 模拟器容器的镜像拉取、运行以及端口转发的示例脚本
README https://github.com/google/android-emulator-container-scripts issue tracker https://github.com/google/android-emulator-container-scripts/issues
推荐阅读