amrsa1 / Android-Emulator-image

The use of this Docker image simplifies the process of running an Android emulator within a Docker container
MIT License
113 stars 52 forks source link

Adding Nativescript to docker and not having success connecting to avd that is created #7

Closed cah-dante-taylor closed 1 year ago

cah-dante-taylor commented 1 year ago

Hello, I wanted to utilize the emulator to run tests on my nativescript application that I am working on. I am not having success with getting the emulator to start still after doing all steps required, It seems that the nexus avd is created and recognized, but will not boot up when prompted. Below I will post source code that is being used, a screenshot of the docker container configurations and the result I get when trying to run nativescript.

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

==============================

ENV ANDROID_ARCH="x86_64" ENV ANDROID_TARGET="google_apis_playstore" ENV API_LEVEL="33" ENV BUILD_TOOLS="33.0.1" ARG ANDROID_ARCH=$ANDROID_ARCH ARG ANDROID_TARGET=$ANDROID_TARGET ARG ANDROID_API_LEVEL="android-$API_LEVEL" ARG ANDROID_APIS="$ANDROID_TARGET;$ANDROID_ARCH" ARG ANDROID_BUILD_TOOLS_VERSION=$BUILD_TOOLS ARG ANDROID_EMULATOR_PACKAGE="system-images;${ANDROID_API_LEVEL};${ANDROID_APIS}" ARG ANDROID_PLATFORM_VERSION="platforms;${ANDROID_API_LEVEL}" ARG ANDROID_BUILD_TOOLS="build-tools;${ANDROID_BUILD_TOOLS_VERSION}" ARG ANDROID_CMD_VERSION="commandlinetools-linux-8092744_latest.zip" ARG ANDROID_SDK_PACKAGES="${ANDROID_EMULATOR_PACKAGE} {ANDROID_BUILD_TOOLS} platform-tools"

==============================

Set JAVA_HOME - SDK

==============================

ENV ANDROID_SDK_ROOT=/opt/android ENV ANDROID_HOME=/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/${ANDROID_BUILD_TOOLS_VERSION}" ENV DOCKER="true"

============================================

Install required Android CMD-line tools

============================================

RUN wget https://dl.google.com/android/repository/${ANDROID_CMD_VERSION} -P /tmp && unzip -d $ANDROID_SDK_ROOT /tmp/$ANDROID_CMD_VERSION && 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 EMULATOR_DEVICE_NAME=$EMULATOR_DEVICE RUN echo "no" | avdmanager --verbose create avd --force --name "${EMULATOR_NAME}" --device "${EMULATOR_DEVICE}" --package "${ANDROID_EMULATOR_PACKAGE}"

====================================

Install latest nodejs, npm & appium

====================================

RUN curl -sL https://deb.nodesource.com/setup_18.x | bash && apt-get -qqy install nodejs && npm install -g npm && npm i -g appium@next --unsafe-perm=true --allow-root && appium driver install uiautomator2 && exit 0 && npm cache clean && apt-get remove --purge -y npm && \ apt-get autoremove --purge -y && apt-get clean && rm -Rf /tmp/ && rm -Rf /var/lib/apt/lists/

Installs nativescript RUN npm install -g nativescript && tns usage-reporting disable && tns error-reporting disable

===================

Alias

===================

ENV START_EMU=./start_emu.sh ENV START_EMU_HEADLESS=./start_emu_headless.sh ENV START_VNC=./start_vnc.sh ENV START_APPIUM=./start_appium.sh

===================

Ports

===================

ENV APPIUM_PORT=4723

=========================

Copying Scripts to root

=========================

COPY . /

RUN chmod a+x start_vnc.sh && chmod a+x start_emu.sh && chmod a+x start_appium.sh && chmod a+x start_emu_headless.sh

=======================

framework entry point

=======================

CMD [ "/bin/bash" ] image imageimage

amrsa1 commented 1 year ago

@cah-dante-taylor seems your machine doesn't support VT check this below:

Configure VM acceleration using Windows Hypervisor Platform
Before you can enable WHPX, your computer must meet the following requirements:

Intel processors: Support for Virtualization Technology (VT-x), Extended Page Tables (EPT), and Unrestricted Guest (UG) features. VT-x must be enabled in your computer's BIOS settings.
AMD processors: AMD Ryzen processor recommended. Virtualization or SVM must be enabled in your computer's BIOS settings.
[Android Studio 3.2 Beta 1 or higher](https://developer.android.com/studio/preview)
[Android Emulator version 27.3.8 or higher](https://developer.android.com/studio/intro/update#sdk-manager)
Windows 10 or higher
To install WHPX on Windows, follow these steps:

From the Windows desktop, right-click the Windows icon and select Apps and features.
Under Related settings, click Programs and Features.
Click Turns Windows Features on or off.
Select Windows Hypervisor Platform.

https://developer.android.com/studio/run/emulator-acceleration#vm-mac
cah-dante-taylor commented 1 year ago

Hello, I am using a macbook pro with virtualization enabled already. I did check to make sure it is running and still recieved the error. I have noticed when adding in the nativescript code that the emulator stopped working. But when I pull the repository and build it without any modifications the emulator does operate as intended. Could nativescript have anything to do with the emulator not operating as intended?

amrsa1 commented 1 year ago

Hello, I am using a macbook pro with virtualization enabled already. I did check to make sure it is running and still recieved the error. I have noticed when adding in the nativescript code that the emulator stopped working. But when I pull the repository and build it without any modifications the emulator does operate as intended. Could nativescript have anything to do with the emulator not operating as intended?

Not really sure, try to build the image locally without this library, and check if it will work or not

cah-dante-taylor commented 1 year ago

Hello here is the result without nativescript inside the source code. The emulator appears to run after the specified command is given.Screenshot 2023-03-02 at 1 29 37 PM

cah-kamal-balwani commented 1 year ago

docker pull amrka/android-emulator:nexus6_playstore-linux-latest is throwing error Error response from daemon: Head "https://registry-1.docker.io/v2/amrka/android-emulator/manifests/nexus6_playstore-linux-latest": Get "https://auth.docker.io/token?scope=repository%3Aamrka%2Fandroid-emulator%3Apull&service=registry.docker.io": EOF

cah-dante-taylor commented 1 year ago

Hello, in the screenshot below it is showing that the emulator is showing offline after running the specified command even without nativescript installed on the container.

Screenshot 2023-03-02 at 3 33 30 PM

cah-kamal-balwani commented 1 year ago

@amrsa1 So the issue is.. when Dante is pulling your image from docker hub using Docker Pull Command and creating container.. Emulator is running file without an error. But when He is pulling your code from Git and building the image and creating container, it is not able to start the container as you see in the image above.. Not sure if there is difference in Image which he is pulling from docker hub vs Image he is building from git. Appreciate if you provide any inputs here. If it was virtualization issue, it should give same error even when he is pulling docker image directly, right?

amrsa1 commented 1 year ago

Which OS you are using, I saw windows before, now I see macOs so I'm a bit confused.

Try the following :

docker build -t android-emulator .
docker run -it --privileged --name androidContainer android-emulator
./start_emu_headless.sh
cah-kamal-balwani commented 1 year ago

@amrsa1 He is using MacOS now.. I do not believe above steps worked and it was still showing Devices Offline and timing out. @cah-dante-taylor is trying again and will update here the results

cah-dante-taylor commented 1 year ago

Turns out the emulator takes awhile to boot (around 10 minutes from the initial command being ran)Screenshot 2023-03-03 at 12 13 59 PM this screenshot does confirm it to be up and running from the commands that you provided.

cah-dante-taylor commented 1 year ago

image Nativescript also has identified the emulator for unit testing. Closing this as resolved. Thank you!