50 lines · bash
1#!/usr/bin/env bash2# ===----------------------------------------------------------------------===##3#4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5# See https://llvm.org/LICENSE.txt for license information.6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7#8# ===----------------------------------------------------------------------===##9 10# This script is the entrypoint of an Android Emulator Docker container.11 12set -e13 14# The container's /dev/kvm has the same UID+GID as the host device. Changing the15# ownership inside the container doesn't affect the UID+GID on the host.16sudo chown emulator:emulator /dev/kvm17 18# Always use a copy of platform-tools provided by the host to ensure that the19# versions of adb match between the host and the emulator.20if [ ! -x /mnt/android-platform-tools/platform-tools/adb ]; then21 echo "error: This image requires platform-tools mounted at" \22 "/mnt/android-platform-tools containing platform-tools/adb" >&223 exit 124fi25sudo cp -r /mnt/android-platform-tools/platform-tools /opt/android/sdk26 27# Start an adb host server. `adb start-server` blocks until the port is ready.28# Use ADB_REJECT_KILL_SERVER=1 to ensure that an adb protocol version mismatch29# doesn't kill the adb server.30ADB_REJECT_KILL_SERVER=1 adb -a start-server31 32# This syntax (using an IP address of 127.0.0.1 rather than localhost) seems to33# prevent the adb client from ever spawning an adb host server.34export ADB_SERVER_SOCKET=tcp:127.0.0.1:503735 36# The AVD could already exist if the Docker container were stopped and then37# restarted.38if [ ! -d ~/.android/avd/emulator.avd ]; then39 # N.B. AVD creation takes a few seconds and creates a mostly-empty40 # multi-gigabyte userdata disk image. (It's not useful to create the AVDs in41 # advance.)42 avdmanager --verbose create avd --name emulator \43 --package "${EMU_PACKAGE_NAME}" --device pixel_544fi45 46# Use exec so that the emulator is PID 1, so that `docker stop` kills the47# emulator.48exec emulator @emulator -no-audio -no-window -no-metrics \49 -partition-size "${EMU_PARTITION_SIZE}"50