Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Run E2E Android Testing with Docker Container

License

NotificationsYou must be signed in to change notification settings

phatnhse/android-container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker HubDocker StarsDocker PullsBuild Status

Goals

  • 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.

Release notes

Change logs can be foundhere

Remarks

  • 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.

Quick start

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 /data

Android SDK Packages Management

These 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...

Gradle

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.

Get the idea of Gradle Wrapper

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.

gradle wrapper properties

Wanna use newer version? Grab one athere and updatedistributionUrl accordingly.

Speed up build with Gradle Caching

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_HOME


You 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]

Non-cached vs cached gradle dependencies

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

build time comparison

Android Emulator

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.

Check the availability of running android emulator in docker container

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}

Reduce flaky tests

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

Emulator startup options

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
OptionDescription
-no-boot-animDisable the boot animation
-acel autoDetermine automatically if acceleration is supported and use it when possible
-no-window -gpu offThis 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 1440x2880In 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 2048Building CI Server with 4GB physical RAM, why not?
-wipe-dataDelete user data and fresh start emulator
-no-snapshotStart app from initial state and delete snapshot data when emulator is closed

License

Released under theApache License.

Read theLICENSE for more details.

About

Run E2E Android Testing with Docker Container

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp