Getting Started with the Facebook SDK for Android
The Facebook SDK for Android helps us to integrate our Android app with Facebook’s platform.
The SDK provides support for Login with Facebook authentication, reading and writing to Facebook APIs and support for UI elements such as pickers and dialogs.
In this post, I am going to discuss about to get started with the Facebook SDK for Android.
Basically I will talk about the initial setup you have to do to get started with Android – Facebook social Apps development.
What Facebook SDK for Android has inside?
It lets people easily sign in to your app with their Facebook identity/account. If they have already signed in on the Facebook for Android app, they don’t have to reenter their username and password.
Allow people using your app to share or send a message from your app to Facebook. People can also like content in your app with the native like button. When people on Facebook engage with these posts they are directed to your app.
Allow users to send requests to their friends from your app.
App Events allow you to understand the actions taken by users within your app and to measure the effectiveness of your Mobile App Ads.
Install Eclipse, Android SDK and ADT plugin as well. Take help from the post – Android – Basic setup and installation to do basic setup and installation of Android SDK in Eclipse. Please ignore if you already have Eclipse configured with Android SDK and go ahead with below steps.
Make sure you have Android 2.2 (API 8) or above configured.
Steps involved in setting up Facebook SDK
Step 1: Install the Facebook APK
The Facebook SDK uses Facebook’s native Android app to provide support for authentication when needed. On a real device, you can test this by simply installing this app for free from Google Play (I believe most of us have Facebook app installed in Android device).
However, it’s not possible to access Google Play on an emulator (in case if you want to test your Application in emulator). If you want to test the flow there, you can download and extract the APK of the Facebook app. It should be named Facebook-11.apk or similar and install it in emulator.
If you don’t want to test your Apps in emulator, you can very well skip this step.
To install it onto an Android emulator, you’ll first need to start the emulator.
Look for Eclipse menu Window >> Android Virtual Device Manager >> Choose AVD Name >> Click start
Secondly,unzip the downloaded apk and locate the adb tool in the platform-tools directory of your main Android SDK. Open adb tool and run below command:
adb install <<PATH WHERE UNZIPPED APK PRESENT>>Facebook-11.apk
For me, it took 30 minutes to complete installation, so stay cool.
Step 2: Import Facebook SDK and projects to Eclipse
Download and extract the SDK ZIP file. The resulting folder, facebook-android-sdk-X.XX, contains the SDK itself. Note the location of this folder.
Open the downloaded content, you can see facebook directory which is SDK. There is also couple of sample projects you can find within the ‘sample’ folder.
Open new workspace in Eclipse (recommended) and import facebook SDK and sample projects into it.
Look for Eclipse menu File >> Import:
Import Facebook SDK and sample projects:
Don’t select the ‘Copy projects into workspace’ option so that each of the sample projects retains a correct reference to the neighboring SDK. However, this means that Eclipse creates a link to the project in the SDK installation, rather than making a copy of it.
Step 3: Run the samples
The samples should be visible in your workspace following the import in the previous step, and each should correctly reference the neighboring ‘FacebookSDK’ library project.
Select a sample project and press ‘Run’ in Eclipse. When you first try to do so, you may be prompted to select the device.
I would recommend you to run the samples in real device (make sure Facebook App is already installed in it).
You must be able to run sample Facebook applications in your Android device.
We are left with one more step (Facebook App ID and Key Hash creation) that is must to create Facebook social Android applications.
Create Facebook App and update Key Hashes
If your app already exists on another platform (e.g. iOS or Web) you may already have a Facebook App ID. You should use the same Facebook App ID for your app across all platforms.
If you do not already have a Facebook App ID for this app, you should create a new one:
- Goto https://developers.facebook.com/?advanced_app_create=true
- Fill in the required details and Click ‘Create App ID’ button.
- Note down the App ID as it will be used in Android applications. Go to ‘Settings’ in the left nav, then click on the ‘Add Platform’ button.
Select Android from the available platforms:
Fill in required details:
- Your Android application package name
- MainActivity which gets called from Facebook App or vice versa
- Create Key Hash and update it:
Just run below command in cmd, enter password as ‘android’ and you will get 28 characters Hash key:
keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%.androiddebug.keystore | openssl sha1 -binary | openssl base64
We are almost done with the setup, very few steps are left:
Click on Quick Start as shown below:
- Click on App Info as shown below:
- Review the Package name and Activity Class name, Click Next:
- Enter the same Key Hash for Release Key Hash as well without fail. Otherwise the applications you will be creating will throw “Key Hash XXXXXX does not match any stored key hashes” error.
With great effort, we completed Facebook SDK setup, going forward use the Application ID got created in Step 1 in Android applications that interact with Facebook.
If you feel this article is helpful and interesting please spread a word about it to your friends and colleagues by sharing the article in Facebook or Twitter.Tweet Share
You are always welcome to provide your comments and feedback from comment box.