FacebookSdk.sdkInitialize (Context) is deprecated
Asked Answered
L

6

115

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?

Lilllie answered 26/1, 2017 at 22:47 Comment(2)
If answer helped you please marked as correct answer or if you still have the same problem we can continue searching.Jones
Facebook added deprecation warning but in comment they say This method can bee called manually if needed. ( github.com/facebook/facebook-android-sdk/commit/… ). So deprecation warning should be never added. Due to performance reason we do not wish to initialize Facebook SDK at app start. We just do it manually only when user need to use Facebook Login feature.Lannielanning
J
142

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.

Jones answered 26/1, 2017 at 22:52 Comment(10)
What about callback of auto initialization?Baroda
How does the FB SDK manage to initialize itself "on Application start" without the need to add anything in Application.onCreate() ??Cuspidate
Answering my own comment: since v4.19.0, the Facebook SDK initialization is done by a ContentProvider, declared in the app manifest: github.com/facebook/facebook-android-sdk/commit/…Cuspidate
Thanks Sébastien, I spend a lot of time, to figure out form where called sdkInitialize()Trojan
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.0Thornberry
@Thornberry 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 inJones
@Jones 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...Agustinaah
Hmmm...maybe the section under Manual Event Logging? developers.facebook.com/docs/app-events/android They should mention this in the class method though :-PAgustinaah
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.Indescribable
Facebook added deprecation warning but in comment they say This method can bee called manually if needed. ( github.com/facebook/facebook-android-sdk/commit/… ). So deprecation waring should be never added. Due to performance reason we do not wish to initialize Facebook SDK at app start. We just do it manually only when user need to use Facebook Login function.Lannielanning
L
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.

Luteous answered 1/2, 2018 at 5:46 Comment(0)
P
9

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);
    
Portcullis answered 7/11, 2019 at 6:41 Comment(0)
C
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());
    }
}
Cherellecheremis answered 5/10, 2017 at 4:50 Comment(2)
According to this public static void activateApp(Context context) is deprecated. It suggests to use this. For that I replaced MainActivity.this with getApplication().Sabelle
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 ApplicationInstructions
G
3

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/

Gamal answered 27/12, 2022 at 10:29 Comment(0)
I
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.

Intersidereal answered 11/12, 2019 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.