Debugger this! Debugging an Android Service

I recently found out that I couldn’t hit any breakpoints in an Android Service I was developing. I found that this was easily sorted by adding the following line:

android.os.Debug.waitForDebugger()

This approach was suggested on various websites including StackOverflow and HelloAndroid amongst many. Great.

I found out much later than when I ran the code “in the wild” i.e. in a production environment unattached to a debugger, my application was failing.

Long story short, this is caused by the waitForDebugger() call, which will cause any code following your invocation to not be executed if there is no debugger.

I suppose I should have realised, but I had assumed that maybe the runtime or the call itself would be clever enough to know whether the application was in debug mode at all, and ignore it if it wasn’t.

But thankfully, we can do that:

if (android.os.Debug.isDebuggerConnected)
{
    android.os.Debug.waitForDebugger()
}

So I’d recommend this is the approach you take over having to remember to take that call out.

Android Studio – Fix sudden unresolved symbols

Android Studio is a fickle beast. At times surprisingly clever and useful, and other times a flaky nightmare.

The most recent issue I came across was when a Google Glass project suddenly stopped building with unresolved symbol errors. This manifests itself as all your “com.google.android.glass.*” imports failing, and the resulting use of any object from those libs causing compilation errors.

What the hell, Android Studio??
What the hell, Android Studio??

How to fix it

Eventually I checked my project dependencies and explicitly added the GDK as a library. Bingo, that sorted it out.

  1. Copy the gdk.jar

    This will be in your Android SDK directory, under /add-ons/addon-google_gdk-google-19/libs

  2. Paste it into the libs folder in your project directory

    e.g. C:\Projects\MyProject\app\libs

  3. Go to File > Project Structure
  4. Select your module (default name is app)
  5. Click the Dependencies tab
  6. Click the “+” sign
  7. Navigate to the libs folder and select the gdk.jar file
  8. OK through everything

I have zero idea why the issue suddenly arose. As with so many other issues I’ve come across in my short time developing for Android, I’ve learned to FDM: fix, document, and move on.

Using Gradle from behind a proxy

By default Android Studio uses Gradle to build Android projects. This means Android Studio always needs a connection to the internet to check for and retrieve dependencies. If you’re developing from behind a proxy (as I am) then you’ll have to explicitly tell Gradle the proxy details to allow it to connect to its servers.

Steps to fix

  1. Navigate to the “.gradle” folder in your user directory (e.g. C:\Users\bob\.gradle)
  2. Create a “gradle.properties” file
  3. Edit the file to have the following contents (replacing your own values)
systemProp.http.proxyHost=<proxy_host>
systemProp.http.proxyPort=<port>
systemProp.http.proxyUser=<user>
systemProp.http.proxyPassword=<password>
systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost

Gradle should then succeed on the next build.

Note – thanks go to Steve Hanson for these steps.

Update 08 Dec 2014 – As of the update to Android 1.0, these steps are not enough! I’ve documented the new definitive list in this post.