class FBSDKPackage 中的构造函数 FBSDKPackage 无法应用于给定类型

constructor FBSDKPackage in class FBSDKPackage cannot be applied to given types

我正在使用 React Native 0.60.0 并尝试在 android 中实现 fbsdk

根据 fbsdk 文档 https://github.com/facebook/react-native-fbsdk 3.1 Android 您需要在 [=19] 中手动添加 new FBSDKPackage() =]

但是在 RN 0.60.0 的新环境下,所有的包都会自动链接,你不必再在 MainApplication.java 添加它们。

这是 MainApplication.java 中的样子:

package com.testing;

import android.app.Application;

import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;

import java.util.List;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      @SuppressWarnings("UnnecessaryLocalVariable")
      List<ReactPackage> packages = new PackageList(this).getPackages();
      // Packages that cannot be autolinked yet can be added manually here, for example:
      // packages.add(new MyReactNativePackage());
      return packages;
    }

    @Override
    protected String getJSMainModuleName() {
      return "index";
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}

your-project-folder\android\app\build\generated\rncli\src\main\java\com\facebook\react 下有这个 PackageList.java 文件是导入包的地方。

package com.facebook.react;

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;

import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import java.util.Arrays;
import java.util.ArrayList;

import com.testing.BuildConfig;
import com.testing.R;

// react-native-appstore-version-checker
import com.masteratul.RNAppstoreVersionCheckerPackage;
// react-native-document-picker
import io.github.elyx0.reactnativedocumentpicker.DocumentPickerPackage;
// react-native-fast-image
import com.dylanvann.fastimage.FastImageViewPackage;
// react-native-fbsdk
import com.facebook.reactnative.androidsdk.FBSDKPackage;
// react-native-gesture-handler
import com.swmansion.gesturehandler.react.RNGestureHandlerPackage;
// react-native-vector-icons
import com.oblador.vectoricons.VectorIconsPackage;

public class PackageList {
  private Application application;
  private ReactNativeHost reactNativeHost;
  public PackageList(ReactNativeHost reactNativeHost) {
    this.reactNativeHost = reactNativeHost;
  }

  public PackageList(Application application) {
    this.reactNativeHost = null;
    this.application = application;
  }

  private ReactNativeHost getReactNativeHost() {
    return this.reactNativeHost;
  }

  private Resources getResources() {
    return this.getApplication().getResources();
  }

  private Application getApplication() {
    if (this.reactNativeHost == null) return this.application;
    return this.reactNativeHost.getApplication();
  }

  private Context getApplicationContext() {
    return this.getApplication().getApplicationContext();
  }

  public ArrayList<ReactPackage> getPackages() {
    return new ArrayList<>(Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      new RNAppstoreVersionCheckerPackage(),
      new DocumentPickerPackage(),
      new FastImageViewPackage(),
      new FBSDKPackage(),
      new RNGestureHandlerPackage(),
      new VectorIconsPackage()
    ));
  }
}

在同一个文件中,我导入

import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;

添加回调管理器

private static CallbackManager mCallbackManager = CallbackManager.Factory.create();

protected static CallbackManager getCallbackManager() {
    return mCallbackManager;
}

并传递 mCallbackManager

new FBSDKPackage(mCallbackManager)

当我保存它并 运行 时,我收到错误提示说

error: constructor FBSDKPackage in class FBSDKPackage cannot be applied to given types;

 new FBSDKPackage(),
  ^

required: CallbackManager found: no arguments

所以我检查了文件并看到代码,出于某种未知原因,已经恢复到添加 CallbackManager 之前的状态。

如何在我的项目中实现这个fbsdk?

对于 React Native 0.60+,您应该使用候选发布版本之一,最新版本是 v1.0.0-rc.4,需要直接在 package.json 中设置。如果您的应用程序中已有 fbsdk 代码,您可能需要研究一些重大更改,您可以阅读 here。如果没有,请按照 master 分支上的文档进行操作。安装正确的版本后,0.60 的新自动链接功能应该会接管,您根本不必亲自动手编写本机代码!