不推荐使用 FacebookSdk.sdkInitialize (Context)

我在 Android Studio 中使用 Facebook-Android-sdk-4.19.0,我在 https://developers.facebook.com/docs/android/getting-started中遵循了 Facebook 的快速启动指南(点击快速启动按钮登录自己的 Facebook 帐户)。在指南中,它被告知在代码片段中复制和粘贴以下代码以跟踪应用程序日志

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);
}
}

但是,当我在 android 工作室中复制粘贴的代码时,似乎所有的 FacebookSdk.sdkInitialize ()方法都被弃用了。这里的 https://developers.facebook.com/docs/reference/android/current/class/FacebookSdk/文档没有说明使用什么方法来初始化 sdk 而不是 sdkInitialize ()。我应该使用什么方法?

63412 次浏览

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.

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());
}
}
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.

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);
    

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.