Tutorial on Facebook Android SDK v4.1.0 Integration in Android Studio

Posted on : Wed, 6th May 15 01:20 pm GMT

Facebook SDK 4.1.0 for Android Integration Tutorial

Facebook SDK for Android keeps changing every now and then, we now have the latest version as v4.1.0. Change is inevitable and hence us programmer's have to keep learning and upgrading. If you used to develop Android Apps on Eclipse IDE, you might be knowing that Android Studio is not THE OFFICIAL IDE for Android development.

Thanks to Android Studio and Gradle build process Integrating Facebook API to your Android Project has become a cakewalk.

In this tutorial we will be using Facebook Android Login version v2.3 (should work for v2.0, v2.1 and 2.2 as well)


Before we start : Prerequisites

  1. Make sure you have created your APP ID at http://developers.facebook.com/apps
  2. You have generated your key hash for the device : if not follow this post Generate Facebook Key Hash
  3. Added the key hash to your APP Page.
  4. This will work with minSDK version 9 or above!
  5. Will not work with Eclipse ADT.

So lets see how to implement Facebook Login for Android using Facebook Android API in Android Studio IDE.

  1. Create a new Project in Android Studio (set minimum SDK version as 9 or above)
  2. Goto build.gradle (Module: app) and add the following repository and dependency,
    repositories {
    compile 'com.facebook.android:facebook-android-sdk:4.0.0'
    Your build.gradle would look something like this, (code is bold is what we have added)
    build.gradle Language : -
    apply plugin: 'com.android.application'
    android {
        compileSdkVersion 22
        buildToolsVersion "21.1.2"
        defaultConfig {
            applicationId "org.code2care.fbloginwithandroidsdk"
            minSdkVersion 10
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    repositories {
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.0.0'
        compile 'com.facebook.android:facebook-android-sdk:4.1.0'

    The Facebook SDK version 4.1.0 is now integrated with your App.

  3. Now lets add the facebook_app_id to our strings.xml file under app-> res-> values-> strings.xml

    File : strings.xml Language : Xml
        <string name="app_name">Fb Login with Android SDK</string>
        <string name="facebook_app_id">your-numeric-id-here</string>
  4. Add the following meta-data tag to AndroidManifest.xml file just below <application> before any activity tag,

    File : AndroidManifest.xml Language : XML
        android:value="@string/facebook_app_id" />

    Now just below the line we added, add the FacebookActivity tag that is needed when we are trying to implement Facebook Login feature,
    AndroidManifest.xml Language : XML
    <activity android:name="com.facebook.FacebookActivity"
        android:label="@string/app_name" />
  5. Lets now create the activity_main.xml file, we simply add a Facebook Login button from com.facebook.login.widget.LoginButton

    File : activity_main.xml Language : XML
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_alignParentTop="true" />
  6. Now lets see the MainActivity.java class,

    File : MainActivity.java Language : Java
    package org.code2care.fbloginwithandroidsdk;
    import android.content.Intent;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.Signature;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.util.Base64;
    import android.util.Log;
    import android.widget.Toast;
    import com.facebook.CallbackManager;
    import com.facebook.FacebookCallback;
    import com.facebook.FacebookException;
    import com.facebook.FacebookSdk;
    import com.facebook.login.LoginResult;
    import com.facebook.login.widget.LoginButton;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
     * Code2care.org
     * Author : Code2care
     * Date : 20150506
     * Note : All you need to do is configure your
     * Facebook APP ID in String XML before running this
     * APP
    public class MainActivity extends ActionBarActivity {
        private CallbackManager callbackManager;
        private LoginButton fbLoginButton;
        protected void onCreate(Bundle savedInstanceState) {
            callbackManager = CallbackManager.Factory.create();
            //You need this method to be used only once to configure
            //your key hash in your App Console at
            // developers.facebook.com/apps
            fbLoginButton = (LoginButton)findViewById(R.id.fb_login_button);
            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                public void onSuccess(LoginResult loginResult) {
                    System.out.println("Facebook Login Successful!");
                    System.out.println("Logged in user Details : ");
                    System.out.println("User ID  : " + loginResult.getAccessToken().getUserId());
                    System.out.println("Authentication Token : " + loginResult.getAccessToken().getToken());
                    Toast.makeText(MainActivity.this, "Login Successful!", Toast.LENGTH_LONG).show();
                public void onCancel() {
                    Toast.makeText(MainActivity.this, "Login cancelled by user!", Toast.LENGTH_LONG).show();
                    System.out.println("Facebook Login failed!!");
                public void onError(FacebookException e) {
                    Toast.makeText(MainActivity.this, "Login unsuccessful!", Toast.LENGTH_LONG).show();
                    System.out.println("Facebook Login failed!!");
        public void getFbKeyHash(String packageName) {
            try {
                PackageInfo info = getPackageManager().getPackageInfo(
                for (Signature signature : info.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA");
                    Log.d("YourKeyHash :", Base64.encodeToString(md.digest(), Base64.DEFAULT));
                    System.out.println("YourKeyHash: "+ Base64.encodeToString(md.digest(), Base64.DEFAULT));
            } catch (PackageManager.NameNotFoundException e) {
            } catch (NoSuchAlgorithmException e) {
        protected void onActivityResult(int reqCode, int resCode, Intent i) {
            callbackManager.onActivityResult(reqCode, resCode, i);

Note : getFbKeyHash() method is only needed to generate the key hash.

If you found this Tutorial helpful then please share!!

We are operating with limited resources due to COVID-19 - Stay Safe!

We stands in support of racial equality!

Follow us : Facebook - Twitter