technology

Debugging issues with Debugging in Android Studio

debug-post

I recently started a new project in Android Studio and immediately ran into unexpected behavior when trying to debug my app. None of my breakpoints were working. This was very frustrating and, even worse, very familiar. After some searching I was able to get it to work, so I figured I would post it here in case it’s helpful to anyone else in the same situation. At the very least I’ll know where to look for help the next time I forget again.

My first thought was that the application was some somehow being configured as not debuggable. The gradle build system should automatically build your application as debuggable so long as you have selected the ‘debug’ build type and is it configured correctly. So let’s look at my debug settings:

debug {
    minifyEnabled true
    shrinkResources true
    ProguardFiles getDefaultProguardFile('proguard-android.txt'), 
                  'proguard-rules.pro'
}

With these settings, breakpoints were ignored in the project code itself, but I was able to set breakpoints in included libs and android code.

This is what the stack trace looks like:

Caused by: java.lang.NullPointerException
    at com.blogpost.app.activity.HomeActivity.onCreate(Unknown Source)
    at android.app.Activity.performCreate(Activity.java:5326)
    ...

At this point I started to remember something about running Proguard. With Proguard on, debugging will not work without more configuration. So I turned it off with this change:

minifyEnabled false

Now my breakpoints worked in the project code and the stack trace looked better:

Caused by: java.lang.NullPointerException
    at com.blogpost.app.activity.HomeActivity.onCreate(HomeActivity.java:70)
    at android.app.Activity.performCreate(Activity.java:5326)
    ...

However, I want to use Proguard when creating a debug build. I want the debug and release builds to be as close as possible. The answer was to make some changes to Proguard settings. First, here’s what the new debug configuration looks like:

debug {
    minifyEnabled true
    shrinkResources true
    ProguardFiles getDefaultProguardFile('proguard-android.txt'), 
                  'proguard-rules-debug.pro'
 }

I created a new ‘proguard-rules-debug.pro’ file for debug specific settings. At first the file was simple, it only contained a single line to include the release rules file:

-include proguard-rules.pro

After some Google searching, I found two ways to get the breakpoints to work and make the stack trace look like I want. Using the approach described in this StackOverflow, I added these two lines to the rule:

-dontobfuscate
-dontoptimize

The second approach I found on this post on Coderwall.  I removed the previous lines and added this one:

-keepattributes SourceFile,LineNumberTable

I left out the recommended ‘-renamesourcefileattribute SourceFile’ setting because I did not care about obfuscating the file names.

Combining both approaches solved my problem. The breakpoints were respected and this is final stack trace:

Caused by: java.lang.NullPointerException
    at com.blogpost.app.activity.HomeActivity.onCreate(HomeActivity.java:70)
    at android.app.Activity.performCreate(Activity.java:5326)
    ...

If you want to know more about Proguard configuration, check out this reference card. Cutting and pasting the settings solved my problem, but reading up on Proguard helped me understand why.

Want to help build and debug Android applications? We’re hiring!

 

Tags:

Discussion

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s