Monday, 22 July 2013

Integrating the Facebook SDK into the Android native apps - Getting started

Facebook has done a really good job creating a SDK which integrates with the activities of Android. They also has a good documentation about how to use it.

This post try to transcribe the content of the video of this link in order to make it more easy to read.

1. Download and integrate the Facebook SDK
1.1 The last version of facebook SDK could be downlaod from here:

1.2 Import the SDK from eclipse by right click on File -> import -> Android -> Existing Android Code into Workspace.
1.3 From the project which you want to use the FacebookSDK, right click on it and select Properties
1.4 On the menu on the left, select Android
1.5 At the bottom click on Add and select the FacebookSDK.

Be aware that the Facebook SDK and your project might require the android-support-v4.jar. This could be done downloading the jar to the libs directory of each project and right click on it, select Build Path -> Add to Build Path. There could be different versions of the same jar, so be aware that both project must has the same version.

2. Generates the personal keyhash to logged in with Facebook
2.1 Open the console and write the follow command:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

Enter the keystore password if it asks (try to type "android" if you don't remember it)
The console will return a string which is the key required by Facebook.
2.2 Copy the key and enter the developer web page of Facebook

Log in if necessary.
2.3 Click on the top-right button and select settings
2.4 On the buttom, paste the key on the Sample App Settings field.

3. Register the application on Facebook
Each time you creates a new application which uses the Facebook SDK, it will requires you to register it on the Facebook developer web page in order to get a special key to make it works.
3.1 Go to the Facebook developer web page

3.2 On the top bar, click on Apps
3.3 Click on Create a new application
3.4 Enter the app name, the namespace(optional), the category and if it use web hosting.
3.5 Enter the text shown in the captcha
3.6 A new app will be registered on the Facebook account.
3.7 On the bottom, select Native Android App and enter the package name, the class name (complete path) and the Key hashes generated on the step 2.1
3.8 Select the field "Initialize the session of Facebook" as activated.
3.9 Click on "Save Changes"

4. Link the application to Facebook account
4.1 On the web page of the App, under the name of the app, copy the field called "App ID".
4.2 Open the String.xml on the Android application and creates a new String called app_id with the new app id. It could be something like this:

<string name="app_id">559000364098826</string>

4.3 Go to the Android manifest file of the app and add a metadata called com.facebook.sdk.ApplicationId which refers to the app_id

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>

5. Add permission to the application
5.1 Go to the Android manifest file and add the follow line to get the Internet permission.

<uses-permission android:name="android.permission.INTERNET"/>

5.2 The application will launch the Facebook login activity as well, so it must be registered in the manifest file also.

<activity android:name="com.facebook.LoginActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@string/app_name" />

FAcebook Developers - Getting started with the Facebook SDK for Android

No comments:

Post a Comment