Book Image

Android Studio Cookbook

By : Mike van Drongelen
Book Image

Android Studio Cookbook

By: Mike van Drongelen

Overview of this book

This book starts with an introduction of Android Studio and why you should use this IDE rather than Eclipse. Moving ahead, it teaches you to build a simple app that requires no backend setup but uses Google Cloud or Parse instead. After that, you will learn how to create an Android app that can send and receive text and images using Google Cloud or Parse as a backend. It explains the concepts of Material design and how to apply them to an Android app. Also, it shows you how to build an app that runs on an Android wear device. Later, it explains how to build an app that takes advantage of the latest Android SDK while still supporting older Android versions. It also demonstrates how the performance of an app can be improved and how memory management tools that come with the Android Studio IDE can help you achieve this. By the end of the book, you will be able to develop high quality apps with a minimum amount of effort using the Android Studio IDE.
Table of Contents (17 chapters)
Android Studio Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Testing your app with an emulator called Genymotion


The best way to test your app is by using a real device. The Android emulator is pretty slow and does not provide you with all the features that come with a real device, such as a camera and all kinds of sensors.

I can image that you do have just one or perhaps a few devices. With thousands of Android devices being available and many brands and models that run on a customized (for example, the Samsung devices) or a plain (like the Nexus devices) flavor of the Android OS and on any Android version that you can think of, testing on real devices only would become pretty expensive.

If, for example, you are creating an app that should run well on Android 2.3, Android 4.x, and Android 5.x, using emulated devices can be handy. Unfortunately, the default emulator is terribly slow. It takes ages to start Android on the emulator, and debugging can be very slow on it as well. To make the emulator a little bit faster, you could try to install Hardware Accelerated Execution Manager (HAXM). There are some topics on the Internet that tell you how to do this; however, there is a much better solution, Genymotion.

Genymotion is a real, fast, and easy-to-use emulator and comes with many real-world device configurations. You can read more about Genymotion on its website at www.genymotion.com. It is available as a free or paid solution. The free one will be all right to start with.

Getting ready

Make sure you have Internet access and sufficient space on your hard drive. We will need to download both VirtualBox and Genymotion. After this, you are ready to create your first virtual device. Let the magic begin.

How to do it...

Let's install Genymotion to prepare Android Studio to work with smoothly running emulated devices:

  1. Both Oracle's VirtualBox and the Genymotion app need to be installed. This is because Genymotion virtualizes various Android operating systems using the virtualization techniques of Oracle Virtual Machine (VM) VirtualBox in the background. If you do not already have Oracle VM VirtualBox installed on your computer (or if you have a version of VirtualBox that is below 4.1.1 which is not compatible with Genymotion), you need to install it first.

    Download VirtualBox for OS X hosts (or for Windows) from the VirtualBox download page at https://www.virtualbox.org/wiki/Downloads.

    Install VirtualBox, and after that, reboot your computer.

    Download Genymotion from its web page at https://www.genymotion.com/#!/download.

  2. Now, open and install the downloaded file.

  3. Run Genymotion. A dialog box then asks you whether you want to create a new device. Click on the Yes button to do so. Later, you can create additional devices by clicking on the + (plus) button on the main screen.

  4. Select the Android OS version from the drop-down list on the left-hand side of dialog box.

  5. Select a virtual device (brand and model) from the drop-down list on the center and click on the Next button.

  6. Name your device. It's recommended that you include both the device and the OS version in your device name so what you are testing on can be easily recognized when you want to use it later.

  7. Click on the Next button to confirm the name. Your virtual device will be created, and it will appear in the list on the main screen of Genymotion. Create as many virtual devices as you need.

  8. To run a virtual device, select it and click on the Play button. It will launch the Genymotion emulator so that you can use it together with Android Studio. When it is launched, you can unlock the device so that it is ready to use.

  9. If you hit the Run button in Android Studio again, you will notice that the running virtual device is shown in the list of available devices in the Choose Device dialog box. Just click on the OK button to let the magic begin. Your Android app will be launched on the emulator.

And it is running fast and smooth! Pretty cool, isn't it?

The following is an example of the main screen of Genymotion listing a couple of virtual devices that have been created:

There's more...

Genymotion comes with emulated front and/or backend cameras, depending on the chosen configuration. To enable them, click on the camera icon. A new dialog box appears in which you can change the slider to On and choose a real camera for the front and backend camera of your virtual device.

After selecting a camera, you can close the dialog box. A green checkbox will now appear next to the Camera button. Now, whenever an app needs to use a camera, it will use the selected camera, which in my case is the webcam on the laptop. To check whether this is working, choose the Camera app on the virtual device.

The paid version of Genymotion has additional features available, including emulated sensors such as GPS and accelerometers. If you like, you can check out the differences at https://www.genymotion.com/#!/store.

Remember that although using virtual devices for testing purposes works really great with Genymotion, it is important to always test it on multiple real devices. Some issues, in particular the ones that are related to memory management that we will see later in this book, are easy to reproduce on real devices, but may be somewhat harder to reproduce on virtual devices.

In addition to this, real devices are much more pixel perfect and some issues may appear only on a particular device so when it comes to see how the artwork looks, you are going to need a couple of them.

By the time your app is nearly complete, you might be interested in the (paid) services from Testdroid, a cloud-based service that allows to run (automated) tests on many real devices. Visit www.testdroid.com to learn more about this great service!

The following screenshot provides an example of the Hello Android Studio app running on a virtual Nexus 5 device using Genymotion: