Using Facebook Sdk in Android Studio

using facebook sdk in Android studio

NOTE

For Android Studio 0.5.5 and later, and with later versions of the Facebook SDK, this process is much simpler than what is documented below (which was written for earlier versions of both). If you're running the latest, all you need to do is this:

  1. Download the Facebook SDK from https://developers.facebook.com/docs/android/
  2. Unzip the archive
  3. In Android Studio 0.5.5 or later, choose "Import Module" from the File menu.
  4. In the wizard, set the source path of the module to import as the "facebook" directory inside the unpacked archive. (Note: If you choose the entire parent folder, it will bring in not only the library itself, but also all of the sample apps, each as a separate module. This may work but probably isn't what you want).
  5. Open project structure by Ctrl + Shift + Alt + S and then select dependencies tab. Click on + button and select Module Dependency. In the new window pop up select :facebook.
  6. You should be good to go.

Instructions for older Android Studio and older Facebook SDK

This applies to Android Studio 0.5.4 and earlier, and makes the most sense for versions of the Facebook SDK before Facebook offered Gradle build files for the distribution. I don't know in which version of the SDK they made that change.

Facebook's instructions under "Import the SDK into an Android Studio Project" on their https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android-using-android-studio/3.0/ page are wrong for Gradle-based projects (i.e. your project was built using Android Studio's New Project wizard and/or has a build.gradle file for your application module). Follow these instructions instead:

  1. Create a libraries folder underneath your project's main directory.
    For example, if your project is HelloWorldProject, you would create
    a HelloWorldProject/libraries folder.

  2. Now copy the entire facebook directory from the SDK
    installation into the libraries folder you just created.

  3. Delete the libs folder in the facebook directory. If you like,
    delete the project.properties, build.xml, .classpath, and .project. files as well. You don't need them.

  4. Create a build.gradle file in the facebook directory with the
    following contents:

    buildscript {
    repositories {
    mavenCentral()
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:0.6.+'
    }
    }

    apply plugin: 'android-library'

    dependencies {
    compile 'com.android.support:support-v4:+'
    }

    android {
    compileSdkVersion 17
    buildToolsVersion "19.0.0"

    defaultConfig {
    minSdkVersion 7
    targetSdkVersion 16
    }

    sourceSets {
    main {
    manifest.srcFile 'AndroidManifest.xml'
    java.srcDirs = ['src']
    resources.srcDirs = ['src']
    res.srcDirs = ['res']
    }
    }
    }

    Note that depending on when you're following these instructions compared to when this is written, you may need to adjust the classpath 'com.android.tools.build:gradle:0.6.+' line to reference a newer version of the Gradle plugin. Soon we will require version 0.7 or later. Try it out, and if you get an error that a newer version of the Gradle plugin is required, that's the line you have to edit.

  5. Make sure the Android Support Library in your SDK manager is installed.

  6. Edit your settings.gradle file in your application’s main directory
    and add this line:

    include ':libraries:facebook'
  7. If your project is already open in Android Studio, click the "Sync Project with Gradle Files" button in the toolbar. Once it's done, the facebook module should appear.
    Sample Image

  8. Open the Project Structure dialog. Choose Modules from the left-hand
    list, click on your application’s module, click on the Dependencies
    tab, and click on the + button to add a new dependency.
    Sample Image
  9. Choose
    “Module dependency”. It will bring up a dialog with a list of
    modules to choose from; select “:libraries:facebook”.
    Sample Image
  10. Click OK on
    all the dialogs.
    Android Studio will automatically resynchronize your project (making it unnecessary to click that "Sync Project with Gradle Files" button again) and
    pick up the new dependency. You should be good to go.

How can i add facebook SDK to android project?

Following are the steps to include Facebook SDK into android project:

1) Download the Facebook SDK from the https://github.com/facebook/facebook-android-sdk/
and extract it to local drive.

2) Import "facebook" as a android project into your Eclipse, this is your Facebook SDK, and You have to reference it to your android project.

3) Now create your android project.

4) Right click on it, go to Properties-->android and Add the "facebook "Library as a reference to your project. click Apply.

Check the video link: http://www.youtube.com/watch?v=w-vNi1W4fco

For more details visit : https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/

include facebook sdk in android studio

Using the Facebook SDK:

  1. Import the sdk by adding compile dependency to build.gradle file

    dependencies {
    compile 'com.facebook.android:facebook-android-sdk:4.+'
    }

  2. Now when you are trying to use something from Facebook's SDK it should be AUTOMATICALLY imported to your files.

In your example you are making a new Class that does uses AppEventsLogger that is part of Facebook SDK. The import statement that you presumably wrote (import com.facebook.FacebookSDK) is grey and second import statement clearly shows that it is importing form com.facebook.* SDK.

tldr: Android Studio imports from Facebook SDK as expected.

Android Studio 0.8.1 - how to use Facebook SDK?

Using Facebook SDK 3.23 in Android Studio 1.1 preview 2

Two choices :

*Maven Repository for the Facebook SDK.

dependencies {
compile 'com.facebook.android:facebook-android-sdk:4.7.0'
}

check for the latest version facebook sdk

OR

*Follow the steps below:

1.Download facebook sdk
https://developers.facebook.com/docs/android/

2.Unzip the archive facebook-android-sdk-3.23.0

3.File menu->Import Module

4.Chose “facebook” folder inside the unzipped archive.

5.Go to directory facebook/build.gradle change this part

This:

 compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
}

To proper sdk version

example:

 compileSdkVersion 21
buildToolsVersion '21.1.1'

defaultConfig {
minSdkVersion 14
targetSdkVersion 21
}

6.Click your project and press F4 ,go to 'dependencies' tab, click '+', 'module dependency' and select your imported facebook module.

7.Resync and rebuild

how to import facebook sdk in android studio?

make sure you have done the following:

first you have to add the following to the dependencies in your app gradle:

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

In your AndroidManifest.xml you have to include the facebook login activity like so :

    <activity
android:name="com.facebook.LoginActivity"
android:label="@string/app_name" />
<activity

also in the app manifest you have to include your app id as a meta-data tag :

  <meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" /> //Create a string resource with your app id

then you can follow this video which is quiet straight forward :

Facebook Official Android SDK Installation Video

Prevent network requests from Facebook Android SDK

I have noticed that the Facebook SDK (at least in version 4.33) adds a provider (com.facebook.internal.FacebookInitProvider) in the manifest of your app, which automatically calls FacebookSdk.sdkInitialize with the application context.

Even if you have added:

<meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled" android:value="false" />

in your manifest, at least 2 requests will be made to Facebook:

  • a graph request to get app settings
  • an Events request "fb_sdk_initialize" which logs all the Facebook frameworks that are included into your app

So to prevent these requests (that we don't want as long as the user didn't allow them (GDPR)), I think you did everything we need to do:

  • Do not add <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> in the manifest
  • Add <meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled" android:value="false"/> in the manifest
  • Only initialize Facebook SDK when you need it.

Like this for example:

FacebookSdk.setApplicationId(<context>.getString(R.string.facebook_app_id));
FacebookSdk.sdkInitialize(<context>);
AppEventsLogger logger = AppEventsLogger.newLogger(<context>); // I don't use FB Login for my project but app events.

But regarding your crash, I don't know why it happens since it seems that the broadcast "com.facebook.sdk.ACTION_CURRENT_ACCESS_TOKEN_CHANGED" is sent locally.

Nevertheless, I think you can prevent it by adding this to your manifest:

<provider
android:name="com.facebook.internal.FacebookInitProvider"
tools:node="remove" />
<receiver
android:name="com.facebook.CurrentAccessTokenExpirationBroadcastReceiver"
tools:node="remove" />

However, doing this may have bad consequences with the use of Facebook SDK and I think you will have to provide (and register in your manifest) your own BroadcastReceiver:

public class CustomCurrentAccessTokenExpirationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (AccessTokenManager.ACTION_CURRENT_ACCESS_TOKEN_CHANGED.equals(intent.getAction())) {
new CurrentAccessTokenExpirationBroadcastReceiver().onReceive(context, intent); // Call it only if you have initialized the Facebook SDK!
}
}
}

How to retrieve profile URL with Facebook SDK in android studio?

All those old ways of linking to the user profile using the user id or app-scoped user id won’t work any more.

You need to ask for the user_link permission, and then you can request the link field from the user object. This will return a URL with a token, that can be used to link to the user’s profile. (The token likely limits the time for how long this link can be used, so you should not store it for long-time use, but request a “fresh” one when it is needed.)

https://developers.facebook.com/docs/facebook-login/permissions#reference-user_link

https://developers.facebook.com/docs/graph-api/reference/user



Related Topics



Leave a reply



Submit