android-dev-ext icon indicating copy to clipboard operation
android-dev-ext copied to clipboard

Breakpoints disabled: "The runtime hasn't loaded this code location"

Open Miyou opened this issue 5 years ago • 5 comments

Hi. I'm trying to use this extension to debug the java files of an android java plugin in a Unity project. The app is launched fine, but when trying to set breakpoints in the java files they become grey circles and hovering over them shows the text "The runtime hasn't loaded this code location". The code should have been run as I have tried setting breakpoints in the main java file UnityPlayerActivity.java.

Any tips on figuring out how to get this working?

Miyou avatar Jun 02 '20 16:06 Miyou

Hi @Miyou - thanks for the report. It's true that the debugger cannot load breakpoints until the Java (Dalvik) runtime loads the associated class.

Once the class is loaded by the runtime, the debugger tries to match the code location with the source file - this fails if the compiled file has been optimised to remove source information or has used obfuscation during the build process.

The most common causes of this issue are;

  • trying to set a breakpoint on a non-code line
  • the class not being loaded by the runtime

but I agree that the main Activity file should always be recognised. Can you successfully set breakpoints in other locations/files?

I don't have Unity, but I'm happy to look into this some more if you can share the APK and source file.

adelphes avatar Jun 03 '20 10:06 adelphes

I have the same problem. I can successfully install an apk and launch it, but no breakpoints works. The configuration is pretty much the same as in the example:

{
            "type": "android",
            "request": "launch",
            "name": "Android testnet",
            "appSrcRoot": "${workspaceRoot}/android/app/src/main",
            "apkFile": "${workspaceRoot}/android/app/build/outputs/apk/TestNet/debug/electroncash-4.2.4-3.apk",
            "adbSocket": "localhost:5037",
            "autoStartADB": true,
            "staleBuild": "warn",
            "targetDevice": "",
            "manifestFile": "${workspaceRoot}/android/app/src/main/AndroidManifest.xml",
            "pmInstallArgs": ["-t -r"],
            "amStartArgs": [
                "-D",
                "--activity-brought-to-front",
                "-a android.intent.action.MAIN",
                "-c android.intent.category.LAUNCHER",
                "-n org.electroncash.wallet/org.electroncash.electroncash3.MainActivity"
            ],
            "postLaunchPause": 1000,
            "trace": false
        }

One thing to note is that the path contains one more level then "usual". I.e.: ${workspaceRoot}/android/app/src/main/ instead of ${workspaceRoot}/app/src/main/

The code for this project can be found at https://github.com/Electron-Cash/Electron-Cash

Update: I am able to set a breakpoint in App.kt and in some of the Java files, like PythonConsoleActivity.java, where the debugger acually breaks and works as intended. I haven't found somewhere else in the Kotlin files where it works and I've tested most of the (like, in Main.kt:MainActivity.onCreate())

jonas-lundqvist avatar May 21 '21 07:05 jonas-lundqvist

Hi Jonas. I can't get the Electron-Cash project to build so it's difficult for me to test why it's failing.

Something I noticed though - in Java, the source filename needs to match the name of the public class defined in the file, but in the Kotlin files, many of the classes do not match the filename (for example, MainActivity is defined in Main.kt). This might be a bug with the Android debugger extension - I haven't tested this, but it would explain why breakpoints work in App.kt. I'd be interested to know if any of the other Kotlin source files load breakpoints where the class name matches the filename.

It's also important to note that breakpoints won't load until the relevant code is loaded in the JVM - that might not happen when the app starts. It's quite normal in Android apps for classes to be delay-loaded - this error appears until the debugger is told that the classes are loaded and then it attempts to match them up with the source files.

adelphes avatar May 24 '21 22:05 adelphes

Something I noticed though - in Java, the source filename needs to match the name of the public class defined in the file, but in the Kotlin files, many of the classes do not match the filename (for example, MainActivity is defined in Main.kt). This might be a bug with the Android debugger extension - I haven't tested this, but it would explain why breakpoints work in App.kt. I'd be interested to know if any of the other Kotlin source files load breakpoints where the class name matches the filename.

I can confirm that renaming a class, like TransactionDialog -> Transactions in Transactions.kt, makes it possible to set and stop at breakpoints in that class.

jonas-lundqvist avatar May 25 '21 05:05 jonas-lundqvist

Thanks a lot. I'll see if there's an easy way to fix this for Kotlin files.

adelphes avatar May 25 '21 19:05 adelphes