As I reported, Google Analytics for Android would sometimes throw an SQLiteException. Of course the big G was getting on-line flack over that and has now put out a new version of their JAR file. Version 1.1 claims to fix this problem. You can check it out at code.google.com/mobile/analytics/docs/android at the bottom of the page.

You would think if anyone would know how to write Android code it would be coders at Google. Yet if you use the Google Analytics library available for Android, you might run into trouble. Google Analytics is great to track usage of your Android application, but should Google Analytics ever cause your app to crash?

Google Analytics did cause one of my apps to crash and the stack trace looked like:

android.database.sqlite.SQLiteException: unable to open database file
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: unable to open database file
at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1899)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:881)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:168)
at com.google.android.apps.analytics.PersistentEventStore.getNumStoredEvents(Unknown Source)
at com.google.android.apps.analytics.PersistentEventStore.startNewVisit(Unknown Source)
at com.google.android.apps.analytics.GoogleAnalyticsTracker.start(Unknown Source)
at com.google.android.apps.analytics.GoogleAnalyticsTracker.start(Unknown Source)

So Google Analytics tried to use SQLite to open the database, but FAILED to catch an SQLiteException. The fix was to wrap opening up Google Analytics into a try-catch block and catch that exception. Here are the relevant code fragments to fix the crash by catching the exception Google Analytics failed to: