Sunday, June 2, 2013

Make the Google+ SDK Sample app work

I was following the Google+ quick start guide for Android to the letter and found the last step quite frustrating:

Select Run > Run to test the PlusSampleActivity sample app. Choose Android application if prompted. The sample application will launch on your device.
When I launched the app from Eclipse I kept getting a crash. My LogCat looked like this:

I/dalvikvm(9444): Failed resolving Lcom/google/android/gms/samples/plus/PlusOneActivity; interface 556 'Lcom/google/android/gms/common/GooglePlayServicesClient$ConnectionCallbacks;'W/dalvikvm(9444): Link of class 'Lcom/google/android/gms/samples/plus/PlusOneActivity;' failed E/dalvikvm(9444): Could not find class '', referenced from method<clinit>
W/dalvikvm(9444): VFY: unable to resolve const-class 590 (Lcom/google/android/gms/samples/plus/PlusOneActivity;) in Lcom/google/android/gms/samples/plus/PlusSampleActivity;
D/dalvikvm(9444): VFY: replacing opcode 0x1c at 0x0018
W/dalvikvm(9444): Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lcom/google/android/gms/samples/plus/PlusSampleActivity;
W/dalvikvm(9444): Class init failed in newInstance call (Lcom/google/android/gms/samples/plus/PlusSampleActivity;)

D/AndroidRuntime(9444): Shutting down VM
W/dalvikvm(9444): threadid=1: thread exiting with uncaught exception (group=0x4203e2a0)
E/AndroidRuntime(9444): FATAL EXCEPTION: main
E/AndroidRuntime(9444): java.lang.ExceptionInInitializerErrorE/AndroidRuntime(9444): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(9444): at java.lang.Class.newInstance(
E/AndroidRuntime(9444): at
E/AndroidRuntime(9444): at
E/AndroidRuntime(9444): at
E/AndroidRuntime(9444): at$700(
E/AndroidRuntime(9444): at$H.handleMessage(
E/AndroidRuntime(9444): at android.os.Handler.dispatchMessage(
E/AndroidRuntime(9444): at android.os.Looper.loop(
E/AndroidRuntime(9444): at
E/AndroidRuntime(9444): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(9444): at java.lang.reflect.Method.invoke(
E/AndroidRuntime(9444): at$
E/AndroidRuntime(9444): at
E/AndroidRuntime(9444): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(9444): Caused by: java.lang.NoClassDefFoundError:
E/AndroidRuntime(9444): at<clinit>(
E/AndroidRuntime(9444): ... 15 more

I/Process(9444): Sending signal. PID: 9444 SIG: 9

After some googling around I stumbled upon this solution on StackOverflow:

It pretty much explains that the way external libraries has to be included in an Eclipse project have changed. The Google Play Services library was included in the project properties under Android > Library.
Reference: ..\..\libproject\google-play-services_lib
Project: google-play-services_lib
Remove this reference and instead add it in the project properties under Java Build Path > Libraries tab. Click "Add External JARs..." and browse to <android-sdk-folder>\extras\google\google_play_services\libproject\google-play-services_lib\libs. Select "google-play-services.jar". Now go to the Order and Export tab and select "google-play-services.jar".

Now the Google+ SDK Sample app runs and works!