Sep 29, 2012

f Comment

Fixing Issues When You Run an Android Facebook App!

Amazon If you are developing a social Android app with the latest Facebook SDK API on Eclipse IDE you probably would run into the following issues. Read on to see how to fix them!

I am using Eclipse Indigo on Windows 7. My Android SDK is version 4.1, or API 16. My Facebook SDK API is version 1.6 per Facebook.class.
Error #1
Let's say you've created an Android Application Project in Eclipse and integrated Facebook SDK in the project. You build and run the project and you see the following error message in Eclipse's Console:

[2012-09-26 22:29:56 - Android Facebook App] ------------------------------
[2012-09-26 22:29:56 - Android Facebook App] Android Launch!
[2012-09-26 22:29:56 - Android Facebook App] adb is running normally.
[2012-09-26 22:29:56 - Android Facebook App] Performing com.example.facebook.app.MainActivity activity launch
[2012-09-26 22:29:56 - Android Facebook App] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'avdName'
[2012-09-26 22:29:56 - Android Facebook App] Uploading Android Facebook App.apk onto device 'emulator-5554'
[2012-09-26 22:29:57 - Android Facebook App] Installing Android Facebook App.apk...
[2012-09-26 22:30:15 - Android Facebook App] Success!
[2012-09-26 22:30:15 - facebook] Could not find facebook.apk!
[2012-09-26 22:30:15 - Android Facebook App] Starting activity com.example.facebook.app.MainActivity on device emulator-5554
[2012-09-26 22:30:17 - Android Facebook App] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.facebook.app/.MainActivity }

Or you see the following error message:

09-26 14:23:20.200: E/AndroidRuntime(1377): FATAL EXCEPTION: main
09-26 14:23:20.200: E/AndroidRuntime(1377): java.lang.NoClassDefFoundError: com.facebook.android.Facebook
09-26 14:23:20.200: E/AndroidRuntime(1377): at com.example.facebook.app.MainActivity.(MainActivity.java:23)
09-26 14:23:20.200: E/AndroidRuntime(1377): at java.lang.Class.newInstanceImpl(Native Method)
...
09-26 14:29:31.991: E/dalvikvm(1555): Could not find class 'com.facebook.android.Facebook', referenced from method com.example.facebook.app.MainActivity.

Read on to see how to fix this nasty error!

Solution for Error #1
The issue is the Android runtime cannot find the Facebook library! Why not? It turns out that Java runtime environment and Android runtime environment are separate and entirely independent of each other! Making sure your app can be compiled and built does not mean it can run successfully on an Android device or emulator.

To make Android aware of the libraries it needs you need to locate your project's libs/ directory and drop facebook.jar in there!

If you use git to download Facebook Android SDK then facebook.jar should be located at <git directory>\facebook-android-sdk\facebook\bin.
My Eclipse workspace is D:\software\eclipse-php-3.0.2.v20120611144-Win32\workspace-android and Android facebook project folder is D:\software\eclipse-php-3.0.2.v20120611144-Win32\workspace-android\Facebook App. Therefore my libs directory is located at D:\software\eclipse-php-3.0.2.v20120611144-Win32\workspace-android\Facebook App\.
Once you drop the jar in libs/ refresh your Android project and take a look at Android Dependencies/ and you should see facebook.jar in there like the following screenshot.

Eclipse Android Dependencies facebook.jar in Package Explorer

Basically your goal is to get facebook.jar to show up under Android Dependencies in Package Explorer. This is one way I figured out that works. If there's other way please let me know!

Your app's package does not need to be com.facebook.android! It can be the default name which starts with com.example.

If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael