Getting Started Android SDK
The Facebook SDK for Android is the easiest way to integrate your Android app with Facebook. It enables:
- Facebook Login - Authenticate people with their Facebook credentials.
- Account Kit - Log people in with just their phone number or email address.
- Share and Send dialogs - Enable sharing content from your app to Facebook.
- App Events - Log events in your application.
- Graph API - Read and write to Graph API.
You have two ways to set up your app to use the Facebook SDK:
- By using Quick Start.
- By setting up your project with the Facebook SDK.
Quick Start
To get a Facebook App ID, configure your app's settings, and import the Facebook SDK, click on the button below and follow the on-line instructions.
Quick Start for Android
Related guides:
Android Studio Setup
To use Facebook SDK in a project, add it as a build dependency and import it.
1. Go to Android Studio | New Project | Minimum SDK
2. Select API 15: Android 4.0.3
or higher and create your new project.
3. After you create a new project, open
your_app | build.gradle
4. Add this to Module-level /app/build.gradle
before dependencies
:
repositories { mavenCentral() }
5. Add the compile dependency with the latest version of the Facebook SDK in the build.gradle
file:
dependencies { compile 'com.facebook.android:facebook-android-sdk:4.+' }
When you use the Facebook SDK, events in your app are automatically logged and collected for Facebook Analytics unless you disable automatic event logging. For details about what information is collected and how to disable automatic event logging, see Automatic App Event Logging.
6. Build your project. Now you can import com.facebook.FacebookSdk
into your app.
Add Facebook App ID
Then add your Facebook App ID to your project's strings file and update your Android manifest:
1. Open your strings.xml
file. Example path: /app/src/main/res/values/strings.xml
.
2. Add a new string with the name facebook_app_id
and value as your Facebook App ID
3. Open AndroidManifest.xml
4. Add a uses-permission
element to the manifest:
<uses-permission android:name="android.permission.INTERNET"/>
5. Add a meta-data
element to the application
element:
<application android:label="@string/app_name" ...> ... <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> ... </application>
Sending Images or Videos
If you're sharing links, images or video via the Facebook for Android app, you also need to declare the FacebookContentProvider
in the manifest.
Append your app id to the end of the authorities
value. For example if your Facebook app id is 1234
, the declaration looks like:
<provider android:authorities="com.facebook.app.FacebookContentProvider1234" android:name="com.facebook.FacebookContentProvider" android:exported="true" />
Using the SDK with ProGuard
You don't have to perform any additional steps to use ProGuard for the Facebook Android SDK. For instructions on Proguard, see Android Developer Site, Shrink Your Code and Resources.
Running Sample Apps
The following samples come with the SDK:
- HelloFacebookSample - Shows profile access, status updates and photo upload
- RPSSample - Use Native Share Dialog, Open Graph publishing, pickers, invites, and deep linking.
- Scrumptious - Login, requests, pickers, picture uploads, and Open Graph publishing.
You can experiment with samples by importing the SDK into an Android Studio project.
The samples have a project dependency rather than a central repository dependency via maven central or jcenter. This is so that when a local copy of the SDK gets updates, the samples reflect the changes.
To run samples apps quickly, you can generate key hashes for your development environments. Add these to your Facebook developer profile for the sample apps. Keytool, for generating the key hashes, is included with the Java SE Development Kit (JDK) that you installed as part of setting up your development environment. OpenSSL is available for download from OpenSSL.
On OS X, run:
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
On Windows, run:
keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64
This generates a 28 character string.
Go to the Facebook Developer site. Log into Facebook and, using the dropdown menu in the top-right, go to Developer Settings:
In your developer settings, select Sample App from the menu, and add and save your key hash into your profile:
You can add multiple key hashes if you develop with multiple machines.
You can now compile and run all of the samples - including those that use Facebook Login.
Create a Development Key Hash
Facebook uses the key hash to authenticate interactions between your app and the Facebook app. If you run apps that use Facebook Login, you need to add your Android development key hash to your Facebook developer profile.
For the version of your app that you release to you also need to generate and set a Release Key Hash.
On either OS X or Windows you can get a key has by generating it or by using the value returned by Settings.getApplicationSignature(Context)
. For instructions, see Running Sample Apps .
Next Steps
After you install Facebook SDK for Android and configure a Facebook App ID, you can see:
Setting a Release Key Hash
To authenticate the exchange of information between your app and the Facebook, you need to generate a release key hash and add this to the Android settings within your Facebook App ID. Without this, your Facebook integration may not work properly when you release your app to the store.
In a previous step, you should have updated your Facebook Developer Settings with the key hashes for your development environments.
When publishing your app, it is typically signed with a different signature to your development environment. Therefore, you want to make sure you create a Release Key Hash and add this to the Android settings for Facebook App ID.
To generate a hash of your release key, run the following command on Mac or Windows substituting your release key alias and the path to your keystore. On OS X, run:
keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64
On Windows, use:
keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64
Make sure to use the password that you set when you first created the release key.
This command should generate a 28 characher string. Copy and paste this Release Key Hash into your Facebook App ID's Android settings.
You should also check that your Facebook App ID's Android setting also contain the correct package name and main activity class for your Android package.
Using the Facebook SDK with Maven
You can declare the Maven dependency with the latest available version of the Android SDK:
<dependency> <groupId>com.facebook.android</groupId> <artifactId>facebook-android-sdk</artifactId> <version>PUT_LATEST_VERSION_HERE</version> </dependency>
Troubleshooting Sample Apps
If you have a problem running a sample app, it may be related to the key hash. You may see one of the following scenarios:
- A native Login Dialog appears but after accepting the permissions you are still in a logged out state. The logcat also contains an exception:
12-20 10:23:24.507: W/fb4a:fb:OrcaServiceQueue(504): com.facebook.orca.protocol.base.ApiException: remote_app_id does not match stored id
- A non-native Login Dialog appears with an error message: ''..App is Misconfigured for facebook login...''.
Check your key hash and you can make sure you use the correct key hash. I
You can also manually modify the sample code to use the right key hash. For example in HelloFacebookSampleActivity
class make a temporary change to the onCreate()
:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Add code to print out the key hash try { PackageInfo info = getPackageManager().getPackageInfo( "com.facebook.samples.hellofacebook", PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { } ...
Save your changes and re-run the sample. Check your logcat output for a message similar to this:
12-20 10:47:37.747: D/KeyHash:(936): 478uEnKQV+fMQT8Dy4AKvHkYibo=
Save the key hash in your developer profile. Re-run the samples and verify that you can log in successfully.