Skip to content

Commit

Permalink
feat: try out new emulators
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Aug 23, 2024
1 parent 387e490 commit 48d64b9
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 0 deletions.
74 changes: 74 additions & 0 deletions .github/workflows/emulator_setup/Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
106 changes: 106 additions & 0 deletions .github/workflows/emulator_setup/start_emu_headless.sh
Original file line number Diff line number Diff line change
@@ -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
76 changes: 76 additions & 0 deletions .github/workflows/test_action.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 48d64b9

Please sign in to comment.