Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly set an exception breakpoint in Android Studio?

I have been trying for months to get my debugger to break on my own code, with no success. Every uncaught exception breaks in the ZygoteInit.run() method.

Here is a list of actions I've taken.

  1. Added debug { debuggable true } to my app module's build.gradle file
  2. Manually added debuggable="true" to my AndroidManifest.xml file
  3. Checked the Any exception checkbox in the Breakpoints window
  4. Added relevant class filter patterns to the Any exception breakpoint
    • this causes the debugger to completely skip all uncaught exceptions

I have been debugging by looking at the stack trace in Logcat, which does show my classes in the stack trace.

I have seen this version on current & previous builds in the stable AND canary channels.

Is there something here I'm missing?

EDIT: Just to clarify for people, the issue was that I had the "Caught exception" box unchecked. Checking this box fixed my issue.

Here is the relevant part of my Gradle file, if it helps at all.

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.0'

    defaultConfig {
        applicationId "com.--redacted--"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 30
        versionName "0.0.30"
        multiDexEnabled true
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/beans.xml'
    }
    buildTypes {
        debug {
            debuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
}

Here is a screenshot of my Breakpoints window.

Breakpoints window

like image 862
Jeremy White Avatar asked Mar 19 '15 06:03

Jeremy White


People also ask

How do I show exceptions in Android Studio?

Show activity on this post. Press Alt + 6 to open the Android DDMS window or alternatively press CTRL + Shift + A and enter Android .

How do you set breakpoints on Android?

To add a line breakpoint, proceed as follows: Locate the line of code where you want to pause execution, then either click the left gutter along that line of code or place the caret on the line and press Control+F8 (on Mac, Command+F8).

What command do you use to set a breakpoint?

Breakpoints are set with the break command (abbreviated b ). The debugger convenience variable `$bpnum' records the number of the breakpoint you've set most recently; see section Convenience variables, for a discussion of what you can do with convenience variables.

What is breakpoint exception?

An exception breakpoint is a type of breakpoint that is created when some exception occurs in the code. On occurrence of such exception our application stops at that given condition causing the exception and we can access all variables in scope at that breakpoint.


1 Answers

A couple more things to ensure:

  • Suspend set to all
  • Notifications on:
    • Caught exception if exceptions that have error handling defined are to be caught
    • Uncaught exceptions if exceptions that are not handled are to be caught
  • Set the class filters to restrict to include only your code, Android code, and Java code

See more detailed instructions here: https://stackoverflow.com/a/28862538/3063884

Breakpoints dialog

This approach catches all exceptions that are raised (when 'caught' and 'uncaught' are checked). Thus all internal exceptions are raised. The specification of a class filter restricts this by excluding some of these exceptions. For example, in the screen-dump above, the ClassNotFoundException, which is raised frequently during start-up, is ignored.

A small downside of this approach is that during app start-up, there may be a message indicating "Cannot find source class for current stack frame". This is because not all of the source code has been loaded yet. This will only occur once during start-up and can safely be ignored. An alternative approach to avoid this (if there are no exceptions expected during app start-up) is to 'Run' Run the application (as opposed to starting 'Debug'), and then manually attach the app to the debugger via Run -> Attach debugger to Android process ... or by pressing the Attach button in the toolbar.

like image 180
CJBS Avatar answered Nov 05 '22 15:11

CJBS