Search code examples
androiddebuggingwear-osandroid-debugwatchface

Forever "Waiting for application to start debug server" when debugging a WatchFace


I'm learning to develop watch faces under WearOS, with Android Studio 3.1.4. I have issues with the debugger.

It seems I can't run the application directly in debug mode (Shift-F9). If I do so, I systematically get the following message, despite having authorized debugging on the watch (emulator or real watch (Huawai Watch 2)):

08/24 09:03:00: Launching wearmodule
$ adb push     /path/wearmodule/build/outputs/apk/debug/wearmodule-debug.apk /data/local/tmp/com.example.wearmodule
$ adb shell pm install -t -r "/data/local/tmp/com.example.wearmodule"
Success


Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Waiting for application to come online: com.example.wearmodule.test | com.example.wearmodule
Connecting to com.example.wearmodule
Waiting for application to start debug server
Could not connect to remote process. Aborting debug session.

If I understand correctly, a debug server has to start on the watch itself. How can I achieve that?

The only option if I want to debug my watch is to run the app in normal mode (Shift-F10) and then attach a debugger to the process.

This is not ideal because it does not allow me to troubleshoot the initialization process of my code. In particular, methods like initializeWatchFace(), onCreate() or onSurfaceChanged() can't be debugged, which is really annoying.

Is there anything special to be done on the watch itself, in the manifest, somewhere, to fix this? Can it be linked to the fact that my app has no activity (as taught in the Google CodeLab). I've seem messages linking these issues to activities managment.


Solution

  • I'm afraid there's no real solution other than what you've already found: start the watch face normally and then attach the debugger to it. As you surmised, the issue is that the debugger is waiting for an Activity to start; since the watch face is based on a Service instead, this never occurs.

    However, there are a couple of tricks you can use to help with debugging watch face startup code. Here's one approach:

    1. Put breakpoints in your startup code as needed, then run the watch face normally.
    2. Attach the debugger to your watch face.
    3. Switch to a different face on the watch. The debugger should remain attached as long your process is alive on the watch, and you'll have a good few seconds until the system kills it.
    4. Switch back to your own face. All your startup code should run at this time.

    If this doesn't work, an alternate technique is to create a "dummy" activity in your app, debug that using Shift-F9 from Android Studio, then set your watch face. Again, all your startup code should then run after the debugger session is established.

    NOTE: one other annoyance you'll find when debugging watch faces is that the OS will kill your process as an ANR after just a few seconds stopped in the debugger. I haven't found a workaround to this other than to just be quick!