Best Practices for Using Android Emulators In A CI/CD Pipeline
Published: 2023-02-02 10:41:46

Setting up the CI/CD pipeline for mobile app testing is critical for scaling the development process with quality and security assurance. Many Android developers are responsible for frequent and predictable releases of the application. It is realized that building APKs/ AABs off an individual machine does not scale. Hence, CI/CD pipeline is explored, and by using an Android emulator, consistency, quality, and reliability of the development of the application are maintained.

The best practices for using an Android emulator highlight its importance in the CI/CD pipeline. The main focus of the present blog will be to address the best practice for using Android Emulators in a CI/CD pipeline.

Before starting, let’s get an overview of the CI/CD pipeline and Android emulator!

## CI/CD pipeline
Continuous Integration and Continuous Delivery/Continuous Deployment (CI/CD) pipelines are prerequisites for modern software development. It is defined as the series of steps which is executed to deliver the latest version of the software. In other words, CI/CD pipelines are practices that aim to improvise the software delivery throughout the development, testing, production, and monitoring stage of software development lifecycles by automation.

It is the backbone of the DevOps approach which integrates the collaboration of developers and IT operation teams for the deployment of software. CI/CD pipeline is a process that directs the development of software via the phase of building, testing, and deploying code. It is basically done via automation to lower human error. The tools which are included in the CI/CD pipeline involve unit tests, code analysis, binary creations, security, and compiling code.

## Concepts attributed to CI/CD 
Continuous integration is understood as the continuous merging of changes to the shared primary branch. It is done as each pull request contains isolated changes which help in diagnosing the issue and allowing us to investigate it quickly.

Continuous delivery automatically merges the pull request into a shared primary branch. It helps in delivering it to production at any time, noting successfully configured quality checks.

Continuous deployment is done by pushing each pull request to production on completion of the checks instead of batching the code changes and sending them to production for the release.

Significance of CI/CD pipeline in mobile application testing
The use of CI/CD pipeline in mobile app testing is diverse, which highlights the use of Android emulators. Some of the significance are:

It enables continuous delivery with assuring software quality and security of the application.
CI/CD pipeline helps in enabling a much shorter time to market for the new features in the mobile application.
It helps in rolling back changes quickly. For example, in the development of a mobile application, if any new code changes break the production applicant, it helps in the immediate return of the application to the previous state.
It accelerates the automation testing process, and the code release process also helps with deployment.
Android Emulator
Android emulators can be understood as tools responsible for creating virtual Android devices on the system. It works by mimicking the guest device’s architecture which eases the automation testing process. It stimulates the Android device so that you can test the mobile application by following CI/CD pipeline on a variety of devices and Android API levels without the requirement of the physical device.

## Best practice of using Android Emulator

### Running of the Android test.
In the CI/CD pipeline, the “Android-test” job is the one that uses the new Android emulator ability. It uses a similar Android machine executor as the build job. It is required to run the emulator on a machine image; for this reason, the specification of “Android/Android-machine” as an executor is needed. For improvising the build times, we require the “resource-class” as xlarge. It is possible to run it on the “large” executor. However, it is noted that there is a lack of resources which makes the testing slow. Therefore, it is recommended to perform some experiments to find the right size executor for the mobile project.

Further, the testing process can be even shorter. You just need to run the command “Android/start-emulator-and-run-tests”. This command starts the specified emulator and runs the tests. The developers pass in the “test-command” again as the parameter. It is also possible to use an Android emulator by coding all the emulator setup steps with the commands highlighted in Android orb: “create-and, start-emulator, wait-for-emulator, and run-tests”

### Test many times
Mobile testing involving CI/CD pipeline makes use of Android emulators to save build minutes per job run. If you are an experienced Android developer, it is possible to use the “connectedAndroidTest” to run the complete build process from initials.

Using tools for CI/CD pipeline, you can build the entire application and implement tests once by passing the artifacts down the following job, followed by running the test on the Android emulators. This use of the Android emulator helps save build minutes per job run.

In order to execute this, you can add three command line steps in the Android emulator run: first, you have to install the application, then install the instrumentation app and run the tests with the use of Android emulators.

### Pre-built Android Emulator Containers  
   
Setting up and running the Android emulator on CI is now very easy with the pre-built Android Emulator Containers. Such a container helps you find and execute the running of the correct emulator version without any dependency management issues. It further makes it easy to augment the automated tests on the part of the CI/CD pipeline without involving any extra cost of physical device farm.

Android emulators and docker image generator scripts in mobile application testing using CI/CD pipeline aid the developers to deploy and debug remote emulators. It also assists in finding the right system image, running of an Android emulator, and managing system dependencies.  

For each release of the major Emulator, pre-built Android Emulator Containers help remove the requirement of manually running the generator, saving time and lowering the complexity.

In addition, pre-built Android Emulator Containers provide the features offered by those built with the docker script, which includes web and ADB access.

The below image shows the way of integrating the pre-built Android Emulator Containers into the system provided by GitHub.

### Decide initial test to be automated.
In CI/CD pipeline, automating the testing process is a major advantage that eases the process. However, it is always noted that organizations switching to automated testing from manual find challenges in deciding which process should be automated first. Say, for instance, it is advisable to automate the process of compiling the code first. Being a developer of mobile apps, automation testing requires a commitment to code on a daily basis.

Android emulator allows testing of the code on virtual devices supporting all devices, browsers, and operating systems. It makes sense to perform automated smoke tests. With an Android emulator, a Unit Test can be done and automated first, which lowers the workload of the developers.

Utilizing an on-demand testing environment
It is suggested to run the test of mobile applications in Android emulators as this approach helps in lowering the environment variables and modifying evidence in the production and development environment. The main use of such a testing environment using an Android emulator is that it gives agility to the CI/CD cycles.

The quality assurance team needs not pull a build from CI servers and use different testing environments. Android emulator allows running of the test against the container image. It is easy to spin up the containers and later destroy them when not required.

### Use “instant Run”
Instant Run has been recently introduced in Android studio. Prior to this, in mobile app testing and integrating CI/CD pipeline, when you needed to make a few changes in the code of the Android application and deploy the application with Android emulators to note the changes due to the code, it involved lots of time.

In each phase, it changes the complete code to machine-level language and then leads to the deployment and launch of the application. To deal with the issue, in CI/CD pipeline involving changes in code and integration, an instant run in an Android emulator converts the newly added code to machine language. It executes the application in the emulator quickly.

### Using HAXM and switching to the x86
In the Android emulator, the system images are supported by two CPUs, which include ARM and Intel X86. With the use of X86 bit system image, the Android emulator begins to run at speed faster than previously executed. For the implementation of this, you are required to update the IDE and SDK for execution according to CI/CD pipeline. Following this, whenever you choose an Android virtual device, you can advise selecting an X86 bit system image.

To download a new system image, you only need to click on the download link. However, if you are experiencing any errors, this shows that you do not have HAXM installed. For installing, it is important to launch your SDK Manager<select SDK Tools tab, and you can then see the option for downloading Intel HAXM. Double-click on IntelHAXM.mpkg to start the installation for this. 

## Conclusion
In the present blog, we have highlighted the best practice of using the Android emulator in a CI/CD pipeline. This has given new information on the use of Android emulators in a CI/CD pipeline. You can use LambdaTest – a cloud-based testing cloud for exploratory and automation testing that lets you perform web and mobile testing across 3000+ real browsers, devices and OS combinations. It offers integration with a different type of CI/CD tools that helps in tracking the robust delivery pipeline for the quick launch of the application. Some of the known CI/CD tools offered by LambdaTest are Bitbucket, AWS CodePipeline, Jenkins, CircleCI, and others.