How to create a breakpoint programmatically on Android

In C #, I can write:

if(Debugger.IsAttached)
    Debugger.Break();

This does not work when the program is not debugging. When the debugger is connected, it behaves like a breakpoint that can never be disconnected. How can I achieve a similar effect on Android?

Or maybe I should not focus on breakpoints at all. What I really want is to make no difference with regular use (a general error message will be shown to the user), but the source of the error becomes apparent the moment the developer starts looking at it. I tried to argue, but this is a sub-project that compiled to release a taste most of the time, and I cannot rely on someone who remembers to switch it to debugging.

+4
source share
2 answers

I think that Debug.isDebuggerConnected()is what you are looking for. This will trueonly return if the application is running with the debugger attached and falseotherwise, regardless of build typeor flavor. Unfortunately, I don’t think you can stop the execution programmatically, but with the above instruction you should be able to display an error message or throw an exception. Personally, I am thinking of something like this:

if (Debug.isDebuggerConnected()) {
    // throw an exception for the developer with a detailed message
} else {
    // show the general error message to the user with a dialog/toast    
}
+1
source

If I understand you clearly, try this piece of code:

if (BuildConfig.DEBUG) {
        // Your, developers behavior
    }
    else {
        // release behavior
    }
0
source

Source: https://habr.com/ru/post/1663743/


All Articles