Android Quick Tips #4 — Debugging

In our last Android Quick Tips blog post, we've looked at some Gradle tips. This week, we'll give you a few suggestions to make your debugging life easier.

Android Quick Tips #1 Series Overview

Rise and Shine™

Are you sick of hitting the unlock button on your device and then swiping away the lock screen dozens of time during your work day?

This little gist from Jake Wharton will wake up your device automatically:

/**
 * Show the activity over the lockscreen and wake up the device. If you launched the app manually
 * both of these conditions are already true. If you deployed from the IDE, however, this will
 * save you from hundreds of power button presses and pattern swiping per day!
 */
public static void riseAndShine(Activity activity) {  
    activity.getWindow().addFlags(FLAG_SHOW_WHEN_LOCKED);

    PowerManager power = (PowerManager) activity.getSystemService(POWER_SERVICE);
    PowerManager.WakeLock lock =
      power.newWakeLock(FULL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE, "wakeup!");

    lock.acquire();
    lock.release();
}

If you want to see a fully integrated example, look at the fork of the gist from androidfu.

It takes you less than five minutes to integrate in your app, but hours in the future!

Check If It's a Debug Build

The gist mentioned above also demonstrates a very nice piece of code:

if (BuildConfig.DEBUG) {  
    // do something you only want to do in debug builds (logging, ...)
} 

The BuildConfig.Debug variable returns if the current build is a debug build. When you often add some lines while debugging and then remove them again before pushing them to your code repository, so it's not in production builds, just add this little if statement and avoid small mistakes.

Advanced Debug Logging

Larger Android apps often have a code component, which keeps giving the developer headaches. Small bugs and errors keep popping up. Most developers now will have to add some logging to all the functions and their parameters, as well as timers to see the performance of certain functions.

All of this can be simply automated with hugo. Hugo will log the parameters and performance of a function by simply adding an annotation.

@DebugLog
public String getName(String first, String last) {  
  SystemClock.sleep(15); // Don't ever really do this!
  return first + " " + last;
}

On logcat, you'll see the following result:

V/Example: ⇢ getName(first="Jake", last="Wharton")  
V/Example: ⇠ getName [16ms] = "Jake Wharton"  

Once again, a five minute effort to set it up will save you hours of adding plain logging calls.

New Android Tools

Our last tip for the day is a shoutout to the Android tools team. Lately, they've been working hard to give us developers better tools to develop better apps.

If you haven't already, take a look at the new network and GPU profiler in the current Android Studio 1.4. builds.

What are your tips for making a developer's debugging life easier? Let us know in the comments!

Explore the Library

Find interesting tutorials and solutions for your problems.