Search code examples
react-nativereact-native-fbsdk

constructor FBSDKPackage in class FBSDKPackage cannot be applied to given types


I am using react native 0.60.0 and trying to implement fbsdk in android.

Based on fbsdk documentation https://github.com/facebook/react-native-fbsdk at 3.1 Android you need to add new FBSDKPackage() manually in your MainApplication.java

But with the new environment of RN 0.60.0, all packages are linked automatically and you don't have to add them at MainApplication.java anymore.

This is how it looks like in 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);
  }
}

There is this PackageList.java file under your-project-folder\android\app\build\generated\rncli\src\main\java\com\facebook\react where the packages are imported.

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

In this same file, I import

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

Add CallbackManager

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

protected static CallbackManager getCallbackManager() {
    return mCallbackManager;
}

and pass the mCallbackManager

new FBSDKPackage(mCallbackManager)

When I save it and run, I received error saying that

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

 new FBSDKPackage(),
  ^

required: CallbackManager found: no arguments

So I checked back the file and saw the code, for some unknown reason, been reverted back to how it was before I added CallbackManager.

How do I implement this fbsdk in my project??


Solution

  • For React Native 0.60+ you should use the one of the release candidate versions, the latest one being v1.0.0-rc.4 and needs to be set directly in package.json. There are some breaking changes that you might have to look into if your app already has fbsdk code in it, which you can read about here. If not, just follow the documentation on the master branch. With the correct version installed, 0.60's new autolinking feature should take over and you won't have to get your hands dirty in the native code at all!