113

I'm using facebook-android-sdk-4.19.0 in Android Studio and I followed the Facebook quick start guide at https://developers.facebook.com/docs/android/getting-started (Click on the Quick Start button to sign in with your own facebook account). In the guide, it's told to copy&paste the following code in the snippet to track app logs

import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
    }
}

However, when I copy pasted the code in android studio, it appears that all of the FacebookSdk.sdkInitialize() methods are deprecated. The documentation here https://developers.facebook.com/docs/reference/android/current/class/FacebookSdk/ tells nothing about what method to use to initialize the sdk instead of sdkInitialize(). What method should I use?

Ozan
  • 1,381
  • 2
  • 10
  • 13
  • 1
    If answer helped you please marked as correct answer or if you still have the same problem we can continue searching. – Yupi Jan 26 '17 at 23:41

6 Answers6

139

From the documentation about upgrading SDK:

The Facebook SDK is now auto initialized on Application start. If you are using the Facebook SDK in the main process and don't need a callback on SDK initialization completion you can now remove calls to FacebookSDK.sdkInitialize. If you do need a callback, you should manually invoke the callback in your code.

Refer to: https://developers.facebook.com/docs/android/upgrading-4x

UPDATE

In SDK 4.22 the title, description, caption and image field of FBSDKShareLinkContent are deprecated. Consider removing them from usage.

Yupi
  • 4,402
  • 3
  • 18
  • 37
  • 1
    What about callback of auto initialization? – Konstantin Konopko May 03 '17 at 14:38
  • 10
    How does the FB SDK manage to initialize itself `"on Application start"` without the need to add anything in `Application.onCreate()` ?? – Sébastien Oct 09 '17 at 13:20
  • 6
    Answering my own comment: since v4.19.0, the Facebook SDK initialization is done by a ContentProvider, declared in the app manifest: https://github.com/facebook/facebook-android-sdk/commit/e610fe89f44dad8c4402fb34d0eb68abe4522af2#diff-1b1024d6bc6def0e7c192dab15687476 – Sébastien Oct 09 '17 at 14:18
  • Thanks Sébastien, I spend a lot of time, to figure out form where called sdkInitialize() – Vahe Gharibyan Nov 12 '17 at 22:57
  • 3
    If I remove the sdkInitialize line, some user still meet the exception: "The Facebook sdk must be initialized before calling activateApp", sdk version: 4.27.0, Android version 5.0 – diousk Nov 21 '17 at 08:21
  • @diousk take a look on this from documentation: Basic App Events are now logged after initializing the Facebook SDK. Calls to activateApp can now be removed from your application. This feature can be disabled by following the instructions in – Yupi Nov 21 '17 at 08:45
  • @Yupi do you have a link to that documetation that the activateApp() call is no longer needed? The AppEventsLogger class' method description still says it is... – kenyee Apr 30 '18 at 15:19
  • Hmmm...maybe the section under Manual Event Logging? https://developers.facebook.com/docs/app-events/android They should mention this in the class method though :-P – kenyee Apr 30 '18 at 15:22
  • Be sure to use the `@string/facebook_app_id` method in your `AndroidManifest.xml` file (as suggested by Facebook)!! I've copied my app ID directly into the Manifest and always got a crash suggesting to `sdkInitialize` even though I had the latest SDK v5.x. – alexkaessner Aug 19 '19 at 16:04
16
FacebookSdk.sdkInitialize(getApplicationContext()); 

This method is deprecated so simply delete this line of code in your class. because according to the latest Facebook we now don't need to initialize the SDK manually, it gets initialize by itself.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
VaibhavBhosale
  • 275
  • 4
  • 6
8

My requirement was to disable autoInit at app launch and initialise it from Activity's onCreate method. AutoInit before app launch was causing my flutter app to take time to start on slow network connections.

  1. Disable AutoInit from manifest

    <meta-data android:name="com.facebook.sdk.AutoInitEnabled"
        android:value="false"/>
    
  2. Initialise Fb sdk in activity's onCreate method

    FacebookSdk.fullyInitialize();
    AppEventsLogger.activateApp(application);
    
Sahil Patel
  • 684
  • 7
  • 19
4

So Instead of calling the deprecated methods you can call AppEventsLogger.activateApp(Application) inside your application class's onCreate()

public class MyApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        AppEventsLogger.activateApp(getApplication());
    }
}
Rakesh Yadav
  • 1,966
  • 2
  • 21
  • 35
  • 1
    According to [this](https://developers.facebook.com/docs/reference/androidsdk/current/facebook/com/facebook/appevents/appeventslogger.html/) `public static void activateApp(Context context)` is deprecated. It suggests to use [this](https://developers.facebook.com/docs/reference/androidsdk/current/facebook/com/facebook/appevents/appeventslogger.html/#activateapp-application-). For that I replaced `MainActivity.this` with `getApplication()`. – Nikhil Wagh Nov 20 '17 at 15:59
  • 2
    `MyApplication.this` is also the same `Application` object. May I ask what's the need for replacing it with `getApplication()`? I simply use `this`(which is also the same as `MyApplication.this`) and it's received as `Application` – vida Nov 24 '17 at 08:49
2

FacebookSdk.sdkInitialize(getApplicationContext());

No need of this method as Facebook doc says: This function initializes the Facebook SDK is called automatically on app start up if the proper entries are listed in the AndroidManifest, such as the facebook app id. Automatic event logging from 'activateApp' can be controlled via the 'com.facebook.sdk.AutoLogAppEventsEnabled' manifest setting.

Rahul Khatri
  • 1,502
  • 2
  • 13
  • 25
1

Android :

Add this line in AndroidManifest.xml

<meta-data android:name="com.facebook.sdk.ClientToken" android:value="YOUR-CLIENT-TOKEN-HERE" />

For client ID Please check the below link-

https://developers.facebook.com/apps/Your_App_ID/settings/advanced/

Aditya Nandardhane
  • 915
  • 1
  • 8
  • 22