Thursday, January 17, 2013

Android web service issue caused by targetSDK change

Yesterday I wrapped up some code changes to an older Android app that had its original minSdkVersion set to 4 and no targetSdkVersion set.  The app makes numerous web service calls.

The updated application had been tested successfully on a Galaxy Tab running 4.2, a Motorola DroidX running 2.2 and numerous emulators running 4.2 or 2.2.  Therefore I updated the minSdkVersion to 8 and the targetSdkVersion to 17, then tested again on the same devices and emulators.

An end-user with a Nexus 7 running 4.2 immediately encountered an Internet connectivity problem.  Further testing determined the following:
  • Setting the targetSdkVersion back to 8 fixed the problem on the Nexus 7
  • The root source of the problem is the app makes web service calls on the main thread, which was allowable under Android 1.6 (4) when the app was originally developed.  This policy changed somewhere between SDK 8 and 17.  The key error message in the stack trace was: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
 There are numerous examples online describing the correct way to do web service calls, here is one nice example.

No comments:

Post a Comment