Chapter 7. Testing Your Code


The Android platform provides a logging mechanism called logcat for collecting and viewing system information. Logs from the system and various apps are output to a series of buffers, which can then be filtered with the logcat command. If you have experience working with Log4J or the java.util.logging package, this will seem very familiar. You can review output from many different systems in a single location and filter it to view information relevant to your application. It is worth getting a good understanding of all the options, as this tool will make your life much easier.

Android logs pretty much everything in the system to a common log file. Information about garbage collection, various system activities, and app output are all sent to the same file. This provides a central location to gather a broad range of information in a single place. It is also important to note that this single file is shared by all apps installed on the device. Therefore, you should be careful not to output sensitive information to the logs. You can use the Proguard utility to obfuscate your code and hide certain details. It can also be used to remove log statements when packaging your app for release (details about using this tool can be found in ProGuard.

Viewing the Logcat File

To view the entire log file (without any filters), issue the command:

adb logcat

This outputs a very verbose log, which includes information about all processes on the system.

Anatomy of a Log ...

Get Android Developer Tools Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.