How do I configure the Android app to work with the Facebook SDK?

How do I connect the Facebook SDK to my app? How to register an app on https://developers.facebook.com ? How do I get started with the Facebook SDK?

Author: Alex Kisel, 2017-12-28

1 answers

Minimum project setup to get started with the Facebook SDK according to the documentation:

1) In your project, open your_app -> Gradle Scripts -> build.gradle (Project) and add the following repository to the buildscript { repositories {}} section to download the SDK from Maven Central Repository:

mavenCentral() 

2) Now in build.gradle (Module: app) we add the compilation instructions to the dependencies{} section of the library required for working with Facebook:

compile 'com.facebook.android:facebook-android-sdk:4.28.0'

Or for Gradle from v3.0:

implementation 'com.facebook.android:facebook-android-sdk:4.28.0'

implementation - to avoid dragging out all the dependencies for compilation, for more information about the difference between compile and implementation, see here

3) In the manifest file (AndroidManifest.xml), before the tag <application>, we declare the permission to access the Internet:

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

4) Now inside <application> we add ApplicationId:

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

5) We write the schema (type) of the data that Activity will accept inside the <activity> <intent-filter> … tags:

<data android:scheme="@string/facebook_login_protocol_scheme" />

6) In res/values/strings.xml add two lines, the values (i.e. ApplicationId and the data schema) to which we will write later:

<string name="facebook_app_id">add late</string>
<string name="facebook_login_protocol_scheme">add late</string>

7) Now we get Key Hash in the format Base64 according to this documentation. In Activity we temporarily write the code:

//...
public final String TAG = this.getClass().getSimpleName();

protected void onCreate(Bundle savedInstanceState) {
    //...

    printKeyHash();
}

private void printKeyHash() {
    try {
        PackageInfo info = getPackageManager()
                .getPackageInfo("io.github.ziginsider.facebooksdkdemo",
                        PackageManager.GET_SIGNATURES);
        for (Signature signature:info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d(TAG, "KeyHash: " + Base64.encodeToString(md.digest(),Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}
//...

8) Launch the application and find the Key Hash in the logs:

Key Hash

9) If you are not yet registered as a developer on Facebook, then go to the site developers.facebook.com and in the upper right corner we find the start button, click, follow the instructions and get to the application control panel.

Next Настройки(Settings) -> Основное(Basic). At the bottom we see the button + Add platform (+Add platform). Click. Adding Android. In the Название пакета Google Play(Google Play package name) field, enter your package (example io.github.ziginsider.facebooksdkdemo). In the Название класса(Class name) field, the full name of the Activity class (example io.github.ziginsider.facebooksdkdemo.MainActivity). In the Key хэш-адреса(Key Hashes) field, enter the previously received key Key Hash.

enter Key Hash

Saving the changes.

10) Next, we take Идентификатор приложения(App ID) and write it to the values of res/values/strings.xml. For example:

<string name="facebook_app_id">383374132098267</string>
<string name="facebook_login_protocol_scheme">fb383374132098267</string>

In the second line, just add "fb" before the ID.

Your app can now work with Facebook.

Russian Facebook SDK tutorial: a small tutorial in Russian on working with the Facebook SDK.

 9
Author: Alex Kisel, 2017-12-28 14:11:57