Reader small image

You're reading from  Mastering Android Studio 3

Product typeBook
Published inAug 2017
Reading LevelIntermediate
Publisher
ISBN-139781786467447
Edition1st Edition
Languages
Right arrow
Author (1)
Kyle Mew
Kyle Mew
author image
Kyle Mew

Kyle Mew has been programming since the early '80s and has written for several technology websites. Also, he has written three radio plays and four other books on Android development.
Read more about Kyle Mew

Right arrow

Testing and Profiling

If there was only one reason to choose Android Studio over other IDEs, it could easily be claimed that it was due to its powerful debugging and testing tools. These tools range from a simple Logcat reporting to complex testing mechanisms based on the JUnit framework. In addition to tools to help us identify bugs in our code, Android Studio also has a host of very smart, performance monitoring tools that allow developers to fine-tune projects and maximize their efficiency.

This chapter will explore each of these processes in turn, beginning with simple inline debug calls and then moving on to the different types of JUnit tests and concluding by taking a look at how to monitor the performance of our apps under a variety of conditions.

In this chapter, you will learn how to:

  • Configure a Logcat debug filter
  • Create local unit tests
  • Build instrumented tests
  • Record...

Logcat filters

One of the simplest, but nevertheless the most useful debugging, techniques is the simple inclusion of a Logcat filter. This can be used to report variable values or simply keep track of which methods are being called. This is of particular use when keeping track of processes that are not visibly apparent, such as services, broadcast receivers and callbacks that have no obvious impact on the UI.

Perhaps the very simplest debug tool available, and useful when we are in a hurry and just want to check for a single value or event, is to include a line like:

System.out.println("Something happened here"); 

This is only an on the fly solution as the output will be buried in among the rest of the Logcat text. Far easier to manage is to configure a Logcat filter. The following short exercise demonstrates how this is done:

  1. Start a new project, or open a new one...

JUnit testing

No development project is complete until it has been thoroughly and rigorously tested, and Android Studio incorporates JUnit testing directly into the workspace. As the name suggests, the framework allows the testing of individual units of code. These are often individual modules but can just as likely be a single class or method.

The Android Studio JUnit test framework provides for two distinct types of test. They are as follows:

  • Local unit tests are used to test business logic in an isolated environment that is not dependent on Android components or other code, although it is possible to mock some dependencies. These tests run on the local Java virtual machine and are consequently considerably faster than testing on a hardware device or emulator.
  • Instrumented tests are used when we want to test elements of the Android framework itself, such as how our UIs behave...

Testing a UI

Although considered separately here, instrumented tests can also be unit tests. There are many non-UI Android classes that we need to test against, and although these can be mocked up, this can be a time-consuming process, particularly when we know these classes are sitting there, already fully implemented on our devices and emulators. If we are prepared to sacrifice the fast build times of mock testing, then we might as well plug in our devices and boot up our emulators.

One aspect of development that is difficult to mock is UI simulation and interaction, and generally speaking, when we want to test our layouts against physical gestures. Fortunately, there are some very handy tools and features at our disposal that help test and optimize our designs.

Testing views

...

Performance monitoring

We may have ironed out all the glitches in our code, but there is still plenty of fine-tuning to do, and one of Android Studio's most innovative features, the Android Profiler, allows us to do just that.

The Android Profiler is not available for modules developed using C++.

The Android Profiler was introduced in Android Studio 3.0 and replaced the previous Android Monitor. At the most basic level, it monitors live CPU, memory, and network usage. This allows us to test our app under different conditions and configurations and improve its performance. It can be accessed from the View | Tool Windows menu or the tool window bar.

Performance monitoring

This basic monitoring is no different from the Android Monitor of previous incarnations. This is because features such as method tracing and memory allocation inspection have a negative impact on build times...

Summary

In this chapter, we took a look at the process of testing and profiling our apps. Not only did we see how to take advantage of JUnit integration to test the integrity of our own business logic, but also how to incorporate tools such as Mockito and Espresso to test the platform itself, and resources such as Firebase to test on a wider range of devices.

Besides testing our code and UIs, we need a way of testing our apps, hardware performance and whether there are issues with CPU, memory, or network usage. This is where Android Studio's built-in profiler, which allows us to inspect and record our app performance in great detail, comes in handy.

With our apps now running smoothly and fine-tuned for performance, we can take a look at the final stages of development, building, packaging, and deployment. Android Studio allows us to use the Gradle build system to simply create...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Android Studio 3
Published in: Aug 2017Publisher: ISBN-13: 9781786467447
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Kyle Mew

Kyle Mew has been programming since the early '80s and has written for several technology websites. Also, he has written three radio plays and four other books on Android development.
Read more about Kyle Mew