From c78029b719c25bc8f4729f473ae63825e5fa9c6c Mon Sep 17 00:00:00 2001 From: Sebastian Tschan Date: Sun, 29 Sep 2019 16:24:19 +0900 Subject: [PATCH 1/6] Add GitHub Test workflow. --- .github/workflows/test.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..6fe9188 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,11 @@ +name: Test + +on: [push] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: shellcheck + run: shellcheck bin/*.sh From ea5e7c7268e424bec5b93582d1c918e183e0f337 Mon Sep 17 00:00:00 2001 From: Sebastian Tschan Date: Sun, 20 Oct 2019 15:36:38 +0900 Subject: [PATCH 2/6] Run workflow on push and pull_request. --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6fe9188..0fb7cc6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,6 @@ name: Test -on: [push] +on: [push, pull_request] jobs: lint: From a07deaf0f9b778525889281e6bf8c62bb291a800 Mon Sep 17 00:00:00 2001 From: Sebastian Tschan Date: Sun, 3 Nov 2019 10:34:44 +0900 Subject: [PATCH 3/6] Add GitHub Sponsors config. --- .github/FUNDING.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..048b1cf --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: [blueimp] From fae78f1fbe0ea211196ed3ef0496760d7711b74c Mon Sep 17 00:00:00 2001 From: Sebastian Tschan Date: Mon, 30 Dec 2019 04:57:40 +0900 Subject: [PATCH 4/6] Create Android Virtual Device if none exists. Provide -return option to return control to the caller when the emulator is ready. --- bin/android-emulator.sh | 109 +++++++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 25 deletions(-) diff --git a/bin/android-emulator.sh b/bin/android-emulator.sh index e9c301b..cf3152c 100755 --- a/bin/android-emulator.sh +++ b/bin/android-emulator.sh @@ -1,12 +1,15 @@ #!/bin/sh # -# Starts the Android virtual device with a writeable filesystem. +# Starts an Android virtual device with a writeable filesystem. # If the -hosts option is provided, replaces /etc/hosts on the device with the # given hosts file. -# If no -avd option is given, starts the first AVD in the list. +# If the -return option is given, returns to the caller when the emulator is +# ready, otherwise waits for the emulator process to stop. +# If no emulator -avd option is given, starts the first AVD in the list. +# If no existing AVD is available, creates a new one. # -# Usage: ./android-emulator.sh [-hosts file] [emulator options] +# Usage: ./android-emulator.sh [-hosts file] [-return] [-- emulator options] # # Copyright 2019, Sebastian Tschan # https://blueimp.net @@ -17,67 +20,123 @@ set -e +DEVICE_ID='pixel' +SYSTEM_IMAGE='system-images;android-[0-9]*;google_apis;x86' +SDCARD='512M' + if [ -z "$ANDROID_HOME" ]; then echo 'Error: ANDROID_HOME is not defined.' >&2 exit 1 fi -# shellcheck disable=SC2139 -alias emulator="$ANDROID_HOME/emulator/emulator" -# shellcheck disable=SC2139 -alias adb="$ANDROID_HOME/platform-tools/adb" +adb() { + "$ANDROID_HOME/platform-tools/adb" "$@" +} + +emulator() { + "$ANDROID_HOME/emulator/emulator" "$@" +} + +avdmanager() { + "$ANDROID_HOME/tools/bin/avdmanager" "$@" +} + +sdkmanager() { + "$ANDROID_HOME/tools/bin/sdkmanager" "$@" +} + +normalize() { + echo "$1" | sed 's/[^a-z A-Z 0-9._-]/-/g' +} -# Echos first AVD listed -avd() { +get_avd() { emulator -list-avds | head -n 1 } -is_boot_completed() { - test "$(adb shell getprop sys.boot_completed | tr -d '\r')" = 1 +get_image() { + sdkmanager --list | grep -o "$SYSTEM_IMAGE" | tail -1 +} + +download_image() { + sdkmanager "$1" } -has_avd_arg() { +create_avd() { + echo 'Downloading system image ...' + download_image "$1" + echo 'System image downloaded.' + echo 'Creating Android Virtual Device ...' + avdmanager create avd \ + --name "$(normalize "$DEVICE_ID-${1#*;}")" \ + --package "$1" \ + --device "$DEVICE_ID" \ + --sdcard "$SDCARD" + echo 'Virtual Device created.' +} + +has_arg() { while test $# -gt 0; do - test "$1" = -avd && return 0 + test "$1" = "$ARG" && return 0 shift done return 1 } -update_hosts_file() { +has_system_prop() { + test "$(adb shell getprop "$1" | tr -d '\r')" = "$2" +} + +wait_for_device() { echo 'Waiting for device to be ready ...' adb wait-for-device - while ! is_boot_completed; do + while ! has_system_prop sys.boot_completed 1; do sleep 1 done + echo 'Device ready.' +} + +update_hosts_file() { adb root adb remount adb push "$1" /etc/hosts adb unroot } -shutdown() { - kill "$PID" -} - -# Initiate a shutdown on SIGINT and SIGTERM: -trap 'shutdown; exit' INT TERM - if [ "$1" = -hosts ]; then HOSTS_FILE=$2 shift 2 fi -if ! has_avd_arg "$@"; then - set -- -avd "$(avd)" "$@" +if [ "$1" = -return ]; then + RETURN=true + shift +fi + +if [ "$1" = -- ]; then + shift +fi + +if ! ARG=-avd has_arg "$@"; then + if [ -z "$(get_avd)" ]; then + create_avd "$(get_image)" + fi + set -- -avd "$(get_avd)" "$@" fi -set -- -writable-system "$@" +if [ -n "$HOSTS_FILE" ]; then + set -- -writable-system "$@" +fi emulator "$@" & PID=$! +wait_for_device + if [ -n "$HOSTS_FILE" ]; then update_hosts_file "$HOSTS_FILE" fi +if [ "$RETURN" = true ]; then + exit +fi + wait "$PID" From 4b9523deaae2ae7568070f2f414d17d314db4729 Mon Sep 17 00:00:00 2001 From: Sebastian Tschan Date: Sun, 26 Sep 2021 03:21:18 +0900 Subject: [PATCH 5/6] Fix automatic AVD creation. Use a writable system image if hosts file is set. Improve android-emulator script stability. --- bin/android-emulator.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bin/android-emulator.sh b/bin/android-emulator.sh index cf3152c..6d78302 100755 --- a/bin/android-emulator.sh +++ b/bin/android-emulator.sh @@ -21,7 +21,8 @@ set -e DEVICE_ID='pixel' -SYSTEM_IMAGE='system-images;android-[0-9]*;google_apis;x86' +SYSTEM_IMAGE_REGEXP='system-images;android-[0-9]*;google_apis;x86\>' +WRITABLE_SYSTEM_IMAGE='system-images;android-28;google_apis;x86' SDCARD='512M' if [ -z "$ANDROID_HOME" ]; then @@ -38,11 +39,11 @@ emulator() { } avdmanager() { - "$ANDROID_HOME/tools/bin/avdmanager" "$@" + "$ANDROID_HOME/cmdline-tools/latest/bin/avdmanager" "$@" } sdkmanager() { - "$ANDROID_HOME/tools/bin/sdkmanager" "$@" + "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "$@" } normalize() { @@ -54,7 +55,11 @@ get_avd() { } get_image() { - sdkmanager --list | grep -o "$SYSTEM_IMAGE" | tail -1 + if [ -n "$HOSTS_FILE" ]; then + echo "$WRITABLE_SYSTEM_IMAGE" + else + sdkmanager --list | grep -o "$SYSTEM_IMAGE_REGEXP" | tail -1 + fi } download_image() { @@ -97,9 +102,11 @@ wait_for_device() { update_hosts_file() { adb root + wait_for_device adb remount adb push "$1" /etc/hosts adb unroot + wait_for_device } if [ "$1" = -hosts ]; then From 88917c89621bb3959a37d36cfcad6d9f257c5709 Mon Sep 17 00:00:00 2001 From: Sebastian Tschan Date: Sun, 26 Sep 2021 03:21:49 +0900 Subject: [PATCH 6/6] Update test workflow. --- .github/workflows/test.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0fb7cc6..e44eb9e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,6 +6,5 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - - name: shellcheck - run: shellcheck bin/*.sh + - uses: actions/checkout@v2 + - run: shellcheck bin/*.sh