Search code examples
javaandroiddebuggingandroid-studiobreakpoints

Strange debugger behaviour in Android studio when debugging an Async task


I've been trying to use the Android Studio breakpoints in order to debug my code, but they are giving me extremely weird behaviour. For example I'm trying to debug the following snippet:

new AsyncTask<Void, Void, Exception>() {
    @Override
    protected Exception doInBackground(Void... params) {
        try {
            Assets assets = new Assets(MainActivity.this);
            File assetDir = assets.syncAssets();
            setupRecognizer(assetDir);
        } catch (IOException e) {
            return e;
        }
        return null;
    }

    @Override
    protected void onPostExecute(Exception result) {
        if (result != null) {
            ((TextView) findViewById(R.id.textView))
                    .setText("Failed to init recognizer " + result);
        } else {
            switchSearch(KWS_SEARCH);
        }
    }
}.execute();

I put a breakpoint on the line

Assets assets = new Assets(MainActivity.this);

And then I say "Step into". This takes me to a "decompiled class file" and the following code snippet:

public Assets(Context context) throws IOException {
    File appDir = context.getExternalFilesDir((String)null);
    if(null == appDir) {
        throw new IOException("cannot get external files dir, external storage state is " + Environment.getExternalStorageState());
    } else {
        this.externalDir = new File(appDir, "sync");
        this.assetManager = context.getAssets();
    }
}

Which is completely expected and reasonable. After this I try to step into the line

File appDir = context.getExternalFilesDir((String)null);

And then things get really weird. First I get taken to a new file where I land on a blank line in between two methods. When I try to step into the blank line I get bounced to another file where execution has halted at the closing curly brace of another method. Then I step into that curly brace and I get taken to another blank line between two methods in another file. When I step into that, I get taken to the very next line, which is simply an @override annotation. Stepping into that takes me to the closing curly brace of an if statement in yet another file.

This weird behaviour continues on and on and I can't make sense of it. At one point the debugger even seems to pause execution in the middle of a comment! How is that even possible? Does anyone know why this is happening? Does it have something to do with the fact that I am debugging an AsyncTask?

(Note that I have repeated this debugging process four times and been presented with the same strange behaviour every time)


Solution

  • Is not a problem in Debug. When Debug pause in blank space or comment is just that the original source code class and the source provided is not synchronized or is not at the same version.

    EDIT:

    Let me explain you with an example. You've a third party library xxx-1.1.jar, and you want to debug inside code of this library so you googled and find the source xxx-1.1-source.jar of this jar. But for some reason developer of xxx-1.1-source.jar packaged this source 2 hours after create xxx-1.1.jar and he add some comment at the beginning of some class that you want to debug so, at this point when you debug xxx-1.1.jar using the code provided by xxx-1.1-source.jar the source code don't match exactly.

    Hope it helps!