VirtualBox - Run Android emulator in a virtual environment for testing an Android project

Introduction

In this article, I will try to describe an example of an infrastructure for Android applications autotests (mobile automation), namely, an environment for conducting UI autotests on an Android device emulator in a virtual environment.





Requirements:

Android emulator requires Intel Virtualization Technology or AMD Virtualization support . Therefore, a tester is often faced with the need to run tests only in a native PC environment with direct access to the central processor.





In this case, the scheme is as follows:









Difficulties:

  1. There is no way to easily recreate the emulator environment.





  2. The environment is not created before testing, and after testing is not deleted, so the environment can affect the application under test.





  3. It takes a long time to fix and configure the environment.





The suggested solution in this article:

  1. Create a VM using the capabilities of nested virtualization VirtualBox (a more detailed description of the technology in this article).





  2. Forward Intel-VT or KVM support inside the created virtual machine.





  3. VM Android .





  4. UI .





  5. VM.





:









:

  1. VM , . .





  2. , .





:

  • c: Intel i5-1035G1





  • : 12Gb





  • BIOS





  • OC: Ubuntu 20.4





1: OS

. VNC . , Linux, Windows, Mac ..





x11vnc

:





sudo apt-get update # 
sudo apt install x11vnc # x11vnc
sudo x11vnc -storepasswd <  > /etc/x11vnc.pass #   
sudo chmod ugo+r /etc/x11vnc.pass #    
      
      



:





x11vnc -nevershared -forever -dontdisconnect -many -noxfixes -rfbauth /etc/x11vnc.pass
      
      



VirtualBox

:





sudo apt-get update
sudo apt install gcc make linux-headers-$(uname -r) dkms
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -
sudo sh -c 'echo "deb http://download.virtualbox.org/virtualbox/debian $(lsb_release -sc) contrib" >> /etc/apt/sources.list.d/virtualbox.list'
sudo apt update # 
sudo apt install virtualbox-6.1
      
      



VM

VM VirtualBox . VM code-first





  • CPU - ( )





  • - 4Gb













Nested Virtualization :





VBoxManage modifyvm < VM> --nested-hw-virt on
      
      



VM.





2: VM

. Packer, VM .





Ubuntu .





KVM

egrep -c '(vmx|svm)' /proc/cpuinfo #     0 -       ,  1   -     KVM   
sudo apt-get update # 
sudo apt install qemu qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager # KVM   
sudo usermod -G libvirt -a ubuntu #  ubuntu   libvirt
sudo systemctl status libvirtd #   libvirt
sudo kvm-ok #  KVM
      
      



Android command line tools

sudo apt-get update # 
yes | sudo apt install android-sdk # Android SDK
sudo apt install unzip # unzip   
cd ~/Downloads #   Downloads
wget https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip #   command line tools    Google
sudo unzip commandlinetools-linux-6858069_latest.zip -d /usr/lib/android-sdk/cmdline-tools/ #
sudo mv /usr/lib/android-sdk/cmdline-tools/cmdline-tools /usr/lib/android-sdk/cmdline-tools/tools #   .   , Google     ,  SDK     
export ANDROID_SDK_ROOT=/usr/lib/android-sdk # 
export PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin #  Path
export PATH=$PATH:$ANDROID_SDK_ROOT/emulator #  Path
      
      



, sdkmanager Android SDK :





sdkmanager --version
      
      



Android tools

yes | sdkmanager --licenses # 
sudo chown $USER:$USER $ANDROID_SDK_ROOT -R #         ANDROID_SDK_ROOT
yes | sdkmanager "cmdline-tools;latest" # cmdline-tools
sdkmanager "build-tools;30.0.3" # build-tools
sdkmanager "platform-tools" # platform-tools
sdkmanager "platforms;android-30"
sdkmanager "sources;android-30"
sdkmanager "emulator" # AVD manager
emulator -accel-check #,    
yes | sdkmanager "system-images;android-23;google_apis;x86_64" #   
sdkmanager --list #   .   CI .
no | avdmanager create avd -n android-23_google_apis_x86_64 -k "system-images;android-23;google_apis;x86_64" #   
emulator -list-avds #   
      
      



Git

Android . . .





sudo apt update # 
yes | sudo apt install git # Git
git --version # 
mkdir ~/workspace #   
cd ~/workspace #    
git clone https://github.com/panarik/AndroidClearApp.git #   
cd ~/workspace/AndroidClearApp #   
      
      



3: Android

./gradlew assembleDebug --no-daemon # APK
emulator -avd android-23_google_apis_x86_64 -no-audio -no-window -verbose -gpu off -accel off #    
sleep 240 # ,      
adb get-state #,   ADB  .  ,   
      
      



ADB :





:





./gradlew connectedAndroidTest --no-daemon
      
      



! !









, , VM.





:





  • VirtualBox ( )





sudo apt purge virtualbox-6.1
      
      



  • VM CPU:













  • VM :





    • VBoxClient





    • KVM





№2 . . , ADB :





! ! :





PackageManager, :









Android . Packer ( ) VirtualBox. CI Jenkins. , , .





I will publish all the results as additions to this article.





Ideally, we should have a code-first infrastructure for UI testing and integration autotests for Android applications, which can be installed on any modern office PC that works autonomously, builds tests on native Android emulators and does not ask for food.





Thanks a lot for your attention!









P.S.





Can I ask you in the comments to give an example of your infrastructure using the Android emulator? For example, emulators in docker containers ( https://github.com/budtmo/docker-android ) might have some other interesting examples.








All Articles