From 48d64b90132027404c820dcb3a3ead7a259599cc Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Fri, 23 Aug 2024 15:10:20 +0300 Subject: [PATCH 1/5] feat: try out new emulators --- .github/workflows/emulator_setup/Dockerfile | 74 ++++++++++++ .../emulator_setup/start_emu_headless.sh | 106 ++++++++++++++++++ .github/workflows/test_action.yml | 76 +++++++++++++ 3 files changed, 256 insertions(+) create mode 100644 .github/workflows/emulator_setup/Dockerfile create mode 100644 .github/workflows/emulator_setup/start_emu_headless.sh create mode 100644 .github/workflows/test_action.yml diff --git a/.github/workflows/emulator_setup/Dockerfile b/.github/workflows/emulator_setup/Dockerfile new file mode 100644 index 000000000..cdc7538de --- /dev/null +++ b/.github/workflows/emulator_setup/Dockerfile @@ -0,0 +1,74 @@ +FROM openjdk:18-jdk-slim + +ENV DEBIAN_FRONTEND noninteractive + +WORKDIR / +#============================= +# Install Dependenices +#============================= +SHELL ["/bin/bash", "-c"] + +RUN apt update && apt install -y curl sudo wget unzip bzip2 libdrm-dev libxkbcommon-dev libgbm-dev libasound-dev libnss3 libxcursor1 libpulse-dev libxshmfence-dev xauth xvfb x11vnc fluxbox wmctrl libdbus-glib-1-2 + +#============================== +# Android SDK ARGS +#============================== +ARG ARCH="x86_64" +ARG TARGET="google_apis_playstore" +ARG API_LEVEL="34" +ARG BUILD_TOOLS="34.0.0" +ARG ANDROID_ARCH=${ANDROID_ARCH_DEFAULT} +ARG ANDROID_API_LEVEL="android-${API_LEVEL}" +ARG ANDROID_APIS="${TARGET};${ARCH}" +ARG EMULATOR_PACKAGE="system-images;${ANDROID_API_LEVEL};${ANDROID_APIS}" +ARG PLATFORM_VERSION="platforms;${ANDROID_API_LEVEL}" +ARG BUILD_TOOL="build-tools;${BUILD_TOOLS}" +ARG ANDROID_CMD="commandlinetools-linux-11076708_latest.zip" +ARG ANDROID_SDK_PACKAGES="${EMULATOR_PACKAGE} ${PLATFORM_VERSION} ${BUILD_TOOL} platform-tools emulator" + +#============================== +# Set JAVA_HOME - SDK +#============================== +ENV ANDROID_SDK_ROOT=/opt/android +ENV PATH "$PATH:$ANDROID_SDK_ROOT/cmdline-tools/tools:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin:$ANDROID_SDK_ROOT/emulator:$ANDROID_SDK_ROOT/tools/bin:$ANDROID_SDK_ROOT/platform-tools:$ANDROID_SDK_ROOT/build-tools/${BUILD_TOOLS}" +ENV DOCKER="true" + +#============================================ +# Install required Android CMD-line tools +#============================================ +RUN wget https://dl.google.com/android/repository/${ANDROID_CMD} -P /tmp && \ + unzip -d $ANDROID_SDK_ROOT /tmp/$ANDROID_CMD && \ + mkdir -p $ANDROID_SDK_ROOT/cmdline-tools/tools && cd $ANDROID_SDK_ROOT/cmdline-tools && mv NOTICE.txt source.properties bin lib tools/ && \ + cd $ANDROID_SDK_ROOT/cmdline-tools/tools && ls + +#============================================ +# Install required package using SDK manager +#============================================ +RUN yes Y | sdkmanager --licenses +RUN yes Y | sdkmanager --verbose --no_https ${ANDROID_SDK_PACKAGES} + +#============================================ +# Create required emulator +#============================================ +ARG EMULATOR_NAME="nexus" +ARG EMULATOR_DEVICE="Nexus 6" +ENV EMULATOR_NAME=$EMULATOR_NAME +ENV DEVICE_NAME=$EMULATOR_DEVICE +RUN echo "no" | avdmanager --verbose create avd --force --name "${EMULATOR_NAME}" --device "${EMULATOR_DEVICE}" --package "${EMULATOR_PACKAGE}" + +#=================== +# Alias +#=================== +ENV EMU_HEADLESS=./start_emu_headless.sh + +#========================= +# Copying Scripts to root +#========================= +COPY . / + +RUN chmod a+x start_emu_headless.sh + +#======================= +# framework entry point +#======================= +CMD [ "/bin/bash" ] \ No newline at end of file diff --git a/.github/workflows/emulator_setup/start_emu_headless.sh b/.github/workflows/emulator_setup/start_emu_headless.sh new file mode 100644 index 000000000..d63c925f3 --- /dev/null +++ b/.github/workflows/emulator_setup/start_emu_headless.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +BL='\033[0;34m' +G='\033[0;32m' +RED='\033[0;31m' +YE='\033[1;33m' +NC='\033[0m' # No Color + +emulator_name=${EMULATOR_NAME} + +function check_hardware_acceleration() { + if [[ "$HW_ACCEL_OVERRIDE" != "" ]]; then + hw_accel_flag="$HW_ACCEL_OVERRIDE" + else + if [[ "$OSTYPE" == "darwin"* ]]; then + # macOS-specific hardware acceleration check + HW_ACCEL_SUPPORT=$(sysctl -a | grep -E -c '(vmx|svm)') + else + # generic Linux hardware acceleration check + HW_ACCEL_SUPPORT=$(grep -E -c '(vmx|svm)' /proc/cpuinfo) + fi + + if [[ $HW_ACCEL_SUPPORT == 0 ]]; then + hw_accel_flag="-accel off" + else + hw_accel_flag="-accel on" + fi + fi + + echo "$hw_accel_flag" +} + + +hw_accel_flag=$(check_hardware_acceleration) + +function launch_emulator () { + adb devices | grep emulator | cut -f1 | xargs -I {} adb -s "{}" emu kill + options="@${emulator_name} -no-window -no-snapshot -noaudio -no-boot-anim -memory 2048 ${hw_accel_flag} -camera-back none" + if [[ "$OSTYPE" == *linux* ]]; then + echo "${OSTYPE}: emulator ${options} -gpu off" + nohup emulator $options -gpu off & + fi + if [[ "$OSTYPE" == *darwin* ]] || [[ "$OSTYPE" == *macos* ]]; then + echo "${OSTYPE}: emulator ${options} -gpu swiftshader_indirect" + nohup emulator $options -gpu swiftshader_indirect & + fi + + if [ $? -ne 0 ]; then + echo "Error launching emulator" + return 1 + fi +} + + +function check_emulator_status () { + printf "${G}==> ${BL}Checking emulator booting up status 🧐${NC}\n" + start_time=$(date +%s) + spinner=( "⠹" "⠺" "⠼" "⠶" "⠦" "⠧" "⠇" "⠏" ) + i=0 + # Get the timeout value from the environment variable or use the default value of 300 seconds (5 minutes) + timeout=${EMULATOR_TIMEOUT:-300} + + while true; do + result=$(adb shell getprop sys.boot_completed 2>&1) + + if [ "$result" == "1" ]; then + printf "\e[K${G}==> \u2713 Emulator is ready : '$result' ${NC}\n" + adb devices -l + adb shell input keyevent 82 + break + elif [ "$result" == "" ]; then + printf "${YE}==> Emulator is partially Booted! 😕 ${spinner[$i]} ${NC}\r" + else + printf "${RED}==> $result, please wait ${spinner[$i]} ${NC}\r" + i=$(( (i+1) % 8 )) + fi + + current_time=$(date +%s) + elapsed_time=$((current_time - start_time)) + if [ $elapsed_time -gt $timeout ]; then + printf "${RED}==> Timeout after ${timeout} seconds elapsed 🕛.. ${NC}\n" + break + fi + sleep 4 + done +}; + + +function disable_animation() { + adb shell "settings put global window_animation_scale 0.0" + adb shell "settings put global transition_animation_scale 0.0" + adb shell "settings put global animator_duration_scale 0.0" +}; + +function hidden_policy() { + adb shell "settings put global hidden_api_policy_pre_p_apps 1;settings put global hidden_api_policy_p_apps 1;settings put global hidden_api_policy 1" +}; + +launch_emulator +sleep 2 +check_emulator_status +sleep 1 +disable_animation +sleep 1 +hidden_policy +sleep 1 \ No newline at end of file diff --git a/.github/workflows/test_action.yml b/.github/workflows/test_action.yml new file mode 100644 index 000000000..821e99e7e --- /dev/null +++ b/.github/workflows/test_action.yml @@ -0,0 +1,76 @@ +name: Test Action + +env: + EMULATOR_REPO: us-docker.pkg.dev/android-emulator-268719/images/28-playstore-x64:30.1.2 + JAVA_V: 11 + JAVA_DIST: corretto + +permissions: + checks: write + pull-requests: write + +on: + push: + branches: + - master + - staging + pull_request: + branches: + - master + - staging + +jobs: + setup: + runs-on: ubuntu-latest + name: Build and Test the SDK + + steps: + - name: Install Docker to the Runner + run: sudo apt-get install docker + + - name: Install Docker to the Runner + run: cd .github/workflows/emulator_setup && docker build --build-arg TARGET=google_apis_playstore --build-arg API_LEVEL=31 --build-arg BUILD_TOOLS=31.0.0 -t android-emulator . + + - name: Pull Emulator from the Repo + run: docker pull ${{ env.EMULATOR_REPO }} + + - name: Checkout SDK code + uses: actions/checkout@v4 + + - name: Setup Java + uses: actions/setup-java@v3 + with: + java-version: ${{ env.JAVA_V }} + distribution: ${{ env.JAVA_DIST }} + + - name: Setup Android SDK + uses: android-actions/setup-android@v2.0.10 + + - name: Start ADB Server + run: adb start-server + + - name: Run Android Emulator Container + run: docker run -it --privileged -d --publish 5555:5555/tcp --name androidContainer android-emulator + + - name: Run emulator in headless mode + run: docker exec --privileged -it -e EMULATOR_TIMEOUT=300 androidContainer bash -c "./start_emu_headless.sh" + + - name: Connect to the Emulator + run: adb connect localhost:5555 + + - name: Build the SDK + if: always() + run: ./gradlew sdk:build + + - name: Run the Android Unit Tests + if: always() + run: ./gradlew sdk:connectedAndroidTest + + - name: Publish Test Results to PR + uses: EnricoMi/publish-unit-test-result-action@v2 + if: always() + with: + check_name: "Unit Test Results :rocket:" + comment_title: "Unit Test Results :rocket:" + files: | + sdk/build/outputs/androidTest-results/**/*.xml From 1a20d5c23c8b63eac2e3df3d0a53d6af23d81d55 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Fri, 23 Aug 2024 15:12:35 +0300 Subject: [PATCH 2/5] fix: see directory --- .github/workflows/test_action.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test_action.yml b/.github/workflows/test_action.yml index 821e99e7e..3612023b7 100644 --- a/.github/workflows/test_action.yml +++ b/.github/workflows/test_action.yml @@ -28,14 +28,17 @@ jobs: - name: Install Docker to the Runner run: sudo apt-get install docker + - name: Checkout SDK code + uses: actions/checkout@v4 + + - name: LS + run: ls + - name: Install Docker to the Runner run: cd .github/workflows/emulator_setup && docker build --build-arg TARGET=google_apis_playstore --build-arg API_LEVEL=31 --build-arg BUILD_TOOLS=31.0.0 -t android-emulator . - - - name: Pull Emulator from the Repo - run: docker pull ${{ env.EMULATOR_REPO }} - - name: Checkout SDK code - uses: actions/checkout@v4 + - name: Go back to home directory + run: cd ~ - name: Setup Java uses: actions/setup-java@v3 From 5533a5011bbdd89f27f075493a73dfb298c91668 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Fri, 23 Aug 2024 15:18:38 +0300 Subject: [PATCH 3/5] fix: tweak connected devices --- .github/workflows/emulator_setup/Dockerfile | 6 +----- .github/workflows/test_action.yml | 6 ------ 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/.github/workflows/emulator_setup/Dockerfile b/.github/workflows/emulator_setup/Dockerfile index cdc7538de..4990f8584 100644 --- a/.github/workflows/emulator_setup/Dockerfile +++ b/.github/workflows/emulator_setup/Dockerfile @@ -56,17 +56,13 @@ ENV EMULATOR_NAME=$EMULATOR_NAME ENV DEVICE_NAME=$EMULATOR_DEVICE RUN echo "no" | avdmanager --verbose create avd --force --name "${EMULATOR_NAME}" --device "${EMULATOR_DEVICE}" --package "${EMULATOR_PACKAGE}" -#=================== -# Alias -#=================== -ENV EMU_HEADLESS=./start_emu_headless.sh - #========================= # Copying Scripts to root #========================= COPY . / RUN chmod a+x start_emu_headless.sh +RUN sh start_emu_headless.sh #======================= # framework entry point diff --git a/.github/workflows/test_action.yml b/.github/workflows/test_action.yml index 3612023b7..5c83c3004 100644 --- a/.github/workflows/test_action.yml +++ b/.github/workflows/test_action.yml @@ -31,9 +31,6 @@ jobs: - name: Checkout SDK code uses: actions/checkout@v4 - - name: LS - run: ls - - name: Install Docker to the Runner run: cd .github/workflows/emulator_setup && docker build --build-arg TARGET=google_apis_playstore --build-arg API_LEVEL=31 --build-arg BUILD_TOOLS=31.0.0 -t android-emulator . @@ -54,9 +51,6 @@ jobs: - name: Run Android Emulator Container run: docker run -it --privileged -d --publish 5555:5555/tcp --name androidContainer android-emulator - - - name: Run emulator in headless mode - run: docker exec --privileged -it -e EMULATOR_TIMEOUT=300 androidContainer bash -c "./start_emu_headless.sh" - name: Connect to the Emulator run: adb connect localhost:5555 From c52bbaca8a62c11d4c72de94b1e78dfb817ff779 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Fri, 23 Aug 2024 15:24:26 +0300 Subject: [PATCH 4/5] fix: add test run --- .github/workflows/emulator_setup/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/emulator_setup/Dockerfile b/.github/workflows/emulator_setup/Dockerfile index 4990f8584..265397356 100644 --- a/.github/workflows/emulator_setup/Dockerfile +++ b/.github/workflows/emulator_setup/Dockerfile @@ -62,7 +62,7 @@ RUN echo "no" | avdmanager --verbose create avd --force --name "${EMULATOR_NAME} COPY . / RUN chmod a+x start_emu_headless.sh -RUN sh start_emu_headless.sh +RUN ./start_emu_headless.sh #======================= # framework entry point From 89f63f37cfed8bd1d3536759400670ac73dc206b Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Fri, 23 Aug 2024 15:32:08 +0300 Subject: [PATCH 5/5] feat: accel on --- .../emulator_setup/start_emu_headless.sh | 27 +------------------ 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/.github/workflows/emulator_setup/start_emu_headless.sh b/.github/workflows/emulator_setup/start_emu_headless.sh index d63c925f3..86958b083 100644 --- a/.github/workflows/emulator_setup/start_emu_headless.sh +++ b/.github/workflows/emulator_setup/start_emu_headless.sh @@ -8,34 +8,9 @@ NC='\033[0m' # No Color emulator_name=${EMULATOR_NAME} -function check_hardware_acceleration() { - if [[ "$HW_ACCEL_OVERRIDE" != "" ]]; then - hw_accel_flag="$HW_ACCEL_OVERRIDE" - else - if [[ "$OSTYPE" == "darwin"* ]]; then - # macOS-specific hardware acceleration check - HW_ACCEL_SUPPORT=$(sysctl -a | grep -E -c '(vmx|svm)') - else - # generic Linux hardware acceleration check - HW_ACCEL_SUPPORT=$(grep -E -c '(vmx|svm)' /proc/cpuinfo) - fi - - if [[ $HW_ACCEL_SUPPORT == 0 ]]; then - hw_accel_flag="-accel off" - else - hw_accel_flag="-accel on" - fi - fi - - echo "$hw_accel_flag" -} - - -hw_accel_flag=$(check_hardware_acceleration) - function launch_emulator () { adb devices | grep emulator | cut -f1 | xargs -I {} adb -s "{}" emu kill - options="@${emulator_name} -no-window -no-snapshot -noaudio -no-boot-anim -memory 2048 ${hw_accel_flag} -camera-back none" + options="@${emulator_name} -no-window -no-snapshot -noaudio -no-boot-anim -memory 2048 -accel on -camera-back none" if [[ "$OSTYPE" == *linux* ]]; then echo "${OSTYPE}: emulator ${options} -gpu off" nohup emulator $options -gpu off &