- Notifications
You must be signed in to change notification settings - Fork22
Run E2E Android Testing with Docker Container
License
phatnhse/android-container
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- No Android Studio/GUI applications required.
- Android emulator runs on a Docker container.
- Accelerates build speed and stabilize testing process, especially UI Tests.
- Performance boost with Gradle dependencies and distribution caching.
Change logs can be foundhere
- No additional ARG(s) need to be provided in order to run this image.
- Linux only. MacOS/Window or any solution which uses VirtualBox to embed Docker can't run x86 emulator becausenested virtualization is yet to support. In the contrary, ARM CPU is host machine independent, which can run anywhere, howeverit was deprecated and extremely slow to boot.
- In the scope of this repo, x86 Emulator is chosen as default startup emulator since it is10x faster than ARM.KVM & nested virtualization will be needed so Linux-based OS as host system is required, especially if you want to build a CI machine with this image.
We'll try to build and run E2E testing with projectSunflower.
Step 1: Build image with name & tag:android-container:sunflower
docker build -t android-container:sunflower.Step 2: Clone and go to top level directory ofsunflower
git clone https://github.com/android/sunflower&&cd sunflower/
Step 3: Run with privileged permission in order to boot emulator on the container, then run gradle tasks (build project and run test suite)
docker run --privileged -it \--rm -v$PWD:/data -v gradle-cache:/cache android-container:sunflower \bash -c'. /start.sh && /data/gradlew test -p /data'
If you want to run UI test, make sure KVM is enable and run this gradle taskconnectedAndroidTest (SeeEmulator)
/data/gradlewtest connectedAndroidTest -p /dataThese following components will be automatically downloaded and installed by default:
# adbplatform-tools# avdmanager and sdkmanagertools# emulator toolkitemulator# build toolsplatforms;android:30# android virtual devicesystem-images;android:30;google_apis;x86
To install additional components, specific them withANDROID_SDK_PACKGES_EXTRA when build with Dockerfile. Remember to add single space between components. Some typical filters:
ANDROID_SDK_PACKAGES_EXTRA="ndk;21.0.6113669 cmake;3.10.2.4988404"To get full list ofinstalled components, run:
sdkmanager --list| awk'/Installed packages/{flag=1; next} /Available Packages/{flag=0} flag'| awk'{ print $1 }'
The output will look like this:
Path-------build-tools;27.0.3build-tools;28.0.3cmake;3.10.2.4988404emulatorextras;android;m2repositoryextras;google;m2repositoryndk-bundlendk;21.0.6113669patcher;v4platform-tools...
You can either execute Gradle Wrapper or Local Installation but first option ismore preferable
In a nutshell you gain the following benefits:
- Standardizes a project on a given Gradle version, leading to more reliable and robust builds.
- Provisioning a new Gradle version to different users and execution environment (e.g. IDEs or Continuous Integration servers) is as simple as changing the Wrapper definition.
Gradle wrapper is a script that allow you to run the build with predefined version and settings. The generated Wrapper properties file,gradle/wrapper/gradle-wrapper.properties, stores the information about the Gradle distribution.
Wanna use newer version? Grab one athere and updatedistributionUrl accordingly.
By default, all files downloaded under docker container doesn't persist if the container is removed.Therefore, they will be re-downloaded in every build.However, Docker offers a solution calledVolume.It is typically directories or files on host filesystem and can be accessible from both container and host machine.You just need to define a location where the volume references to and let it take care the rest.Consider this following script and image:
ENV GRADLE_USER_HOME=/cacheVOLUME $GRADLE_USER_HOMEYou can always check where the volumes are located and how they work:
- On Macos:
~/Library/Containers/com.docker.docker/Data/vms/0/tty- On Linux:
~/var/lib/docker/volumes- To list all volumes are being use:
docker volume ls
- To get all properties of a volume:
docker volume inspect [volume_id]
In some circumstances, you will see this one is huge improvement, especially when a project has used ton of dependencies. Let's see the different between cached and non-cached gradle for Sunflower project.
BUILD SUCCESSFULLin 4m 25s......BUILD SUCCESSFULLin 55s
If you're familiar with Android Studio, you definitely experience this warning when booting ARM emulators. They were old and deprecated since Android SDK 25. In the contrary, x86 emulators are 10x faster, but it needs hardware acceleration to run (HAXM on Mac & Windows, QEMU on Linux). On Docker, you will also needNested Virtualization, which is not available on Virtual Box. So Linux-based OS is recommended in order to make it compatible with this image.
The script below simply checks if kvm & nested virtualization is supported.
functioncheck_kvm() { cpu_support_hardware_acceleration=$(grep -cw".*\(vmx\|svm\).*" /proc/cpuinfo) kvm_support=$(kvm-ok)if ["$cpu_support_hardware_acceleration"!= 0 ]&& ["$kvm_support"!=*"NOT"* ];thenecho 1elseecho 0fi}
You can turn off following animations by usingadb shell ( these can be found in developer options )
- Window animation scale
- Transition animation scale
- Animation duration scale
functiondisable_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"}
You can also disable keyboard or customize default locale language
adb shell settings put secure show_ime_with_hard_keyboard 0 adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE EN
Unlikeadb, you can only specify emulator options when starting it, not later on. Consider following command & options:
emulator -avd${EMULATOR_NAME} -no-window -no-boot-anim -wipe-data -no-snapshot -gpu off -accel auto -memory 2048 -skin 1440x2880| Option | Description |
|---|---|
| -no-boot-anim | Disable the boot animation |
| -acel auto | Determine automatically if acceleration is supported and use it when possible |
| -no-window -gpu off | This option is useful when running the emulator on headless servers. You'll still be able to access the emulator through adb or the console |
| -skin 1440x2880 | In case you want the screen has more room, especially with list of items. Use it at your risk, it would be better to support different screen sizes |
| -memory 2048 | Building CI Server with 4GB physical RAM, why not? |
| -wipe-data | Delete user data and fresh start emulator |
| -no-snapshot | Start app from initial state and delete snapshot data when emulator is closed |
Released under theApache License.
Read theLICENSE for more details.
About
Run E2E Android Testing with Docker Container
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.



