4

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.

cyphics
  • 300
  • 3
  • 16
  • 1
    You can try the solution provided in this [SO thread](https://stackoverflow.com/questions/36723813/not-able-to-debug-app-on-android-device-android-studio-2-0): turn the USB Debuggin on for both devices or Invalidate Caches/Restart. – noogui Aug 24 '18 at 15:18

3 Answers3

1

Another option is to override the onCreate method on your watch face service and call Debug.waitForDebugger()

override fun onCreate() {
    Debug.waitForDebugger()
    super.onCreate()
}

Use Run in Android Studio so your watch face service is installed in your watch. Then in Android Studio go to the Run top menu and there select Attach to Process....

waitForDebugger basically puts the main thread on pause until a debugger has been attached. You can place Debug.waitForDebugger wherever it makes more sense in your code.

jush
  • 593
  • 4
  • 11
0

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!

String
  • 5,633
  • 2
  • 27
  • 39
  • A pity to hear that. It seems that watch face developement tools need strong improvements. For a newbie like me, it's a bit of a pain to manage all the disfunctions of the emulator/debugger AND make my way through all the difficulites inherent to the task in general. Anyway, thanks for the clarification. – cyphics Aug 31 '18 at 11:43
  • Indeed. I've talked to the Wear developer relations team about these issues at Google I/O the last couple of years, and while they acknowledge that the problems are real, there's been no interest shown in addressing them. – String Aug 31 '18 at 15:08
0

Restarting Android Studio worked for me.

FractalBob
  • 2,199
  • 2
  • 21
  • 31