React-native-fbsdk: Error on generated file PackageList.java using FBSDKPackage()

Created on 19 Jul 2019  路  13Comments  路  Source: facebook/react-native-fbsdk

馃悰 Bug Report

The file PackageList.java being created on the folder android/app/build/generated/rncli/src/main/java/com/facebook/react is being generated with an error on the line it add new FBSDKPackage() to the array of packages because there is no argument being passed and according to the document it must pass the Callback Manger, as is being done on MainApplication.java:

...
packages.add(new FBSDKPackage(mCallbackManager))

To Reproduce

I just made all configuration according to docs and ran:
% react-native run-android

Expected Behavior

It should open on the device just the way it was before adding react-native-fbsdk

Code Example

File being generated with the error:

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.MYAPP.BuildConfig;
import com.MYAPP.R;

// react-native-fbsdk
import com.facebook.reactnative.androidsdk.FBSDKPackage;
// react-native-firebase
import io.invertase.firebase.RNFirebasePackage;

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 FBSDKPackage(), // ERROR HERE
      new RNFirebasePackage()
    ));
  }
}

The MainApplication.java:

package com.MYAPP;

import android.app.Application;
import android.util.Log;

import com.facebook.react.PackageList;
import com.facebook.hermes.reactexecutor.HermesExecutorFactory;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.ReactApplication;

import io.invertase.firebase.RNFirebasePackage;
import io.invertase.firebase.auth.RNFirebaseAuthPackage; // added manually

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

// Facebook SDK
import com.facebook.reactnative.androidsdk.FBSDKPackage;
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;

import java.util.List;

public class MainApplication extends Application implements ReactApplication {

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

    protected static CallbackManager getCallbackManager() {
        return mCallbackManager;
    }

    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.add(new RNFirebaseAuthPackage()); // Add the Firebase Auth Package
            packages.add(new FBSDKPackage(mCallbackManager)); // Register Facebook SDK package with the Callback Manager
            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);
        AppEventsLogger.activateApp(this);
    }

}

Environment

System:
    OS: macOS 10.14.5
    CPU: (4) x64 Intel(R) Core(TM) i5-4250U CPU @ 1.30GHz
    Memory: 25.61 MB / 4.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 8.11.3 - /usr/local/bin/node
    Yarn: 1.17.3 - /usr/local/bin/yarn
    npm: 6.10.1 - ~/.npm-packages/bin/npm
  SDKs:
    iOS SDK:
      Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
    Android SDK:
      API Levels: 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28
      Build Tools: 26.0.1, 26.0.2, 27.0.1, 27.0.2, 27.0.3, 28.0.3
      System Images: android-16 | Google APIs Intel x86 Atom, android-19 | ARM EABI v7a, android-19 | Intel x86 Atom, android-20 | Android Wear ARM EABI v7a, android-20 | Android Wear Intel x86 Atom, android-21 | Android TV ARM EABI v7a, android-21 | Android TV Intel x86 Atom, android-21 | Android Wear ARM EABI v7a, android-21 | Android Wear Intel x86 Atom, android-21 | ARM EABI v7a, android-21 | Intel x86 Atom, android-21 | Intel x86 Atom_64, android-21 | Google APIs ARM EABI v7a, android-21 | Google APIs Intel x86 Atom, android-22 | Android TV ARM EABI v7a, android-22 | Android TV Intel x86 Atom, android-22 | Android Wear ARM EABI v7a, android-22 | Android Wear Intel x86 Atom, android-22 | ARM EABI v7a, android-22 | Intel x86 Atom, android-22 | Intel x86 Atom_64, android-22 | Google APIs ARM EABI v7a, android-22 | Google APIs Intel x86 Atom, android-22 | Google APIs Intel x86 Atom_64, android-23 | Google APIs Intel x86 Atom, android-24 | Intel x86 Atom, android-24 | Intel x86 Atom_64, android-24 | Google APIs Intel x86 Atom, android-25 | Android TV Intel x86 Atom, android-25 | Google APIs Intel x86 Atom_64
  IDEs:
    Android Studio: 3.4 AI-183.6156.11.34.5522156
    Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.8.6 => 16.8.6
    react-native: 0.60.3 => 0.60.3
  npmGlobalPackages:
    create-react-native-app: 2.0.2
    react-native-cli: 2.0.1
    react-native-rename: 2.4.1

package.json:
```
"dependencies": {
"react": "16.8.6",
"react-native": "0.60.3",
"react-native-fbsdk": "^0.10.1",
"react-native-firebase": "^5.5.5"
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/runtime": "^7.5.5",
"@react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.8.0",
"eslint": "^6.0.1",
"jest": "^24.8.0",
"metro-react-native-babel-preset": "^0.55.0",
"react-test-renderer": "16.8.6"
},

I also deleted that file and put everything in `MainApplication.java` (as you can see below) but when I run `react-native run-android` it comes back with the error:
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
                new RNFirebasePackage(),
                new RNFirebaseAuthPackage(),
                new MainReactPackage(),
                new FBSDKPackage(mCallbackManager)
        );
    }

```

Most helpful comment

It's because react-native 0.60 tries to link the package for you.

new FBSDKPackage() -----> (I dont know why the parameter mCallbackManager doesn't get passed
to it although iam passing it the correct way in MainApplication.java )

It's because this line does not come from YOUR mainApplication.java but the one generated by react-native cli.

You can upgrade to the latest version of fbsdk AND unlink this library since it will be linked automatically by react native OR exclude fbsdk from the autolink process

You can read more in my previous comment :
https://github.com/facebook/react-native-fbsdk/issues/597#issuecomment-513299757

All 13 comments

I have facing same issue any one please help me to out from this problem

It seems that you are using the version 0.10.1 of react-native-fbsdk, this version needs to be instanciated using a callbackManager like this :
new FBSDKPackage(mCallbackManager)
The new react-native autolink process just can't do this.

Fortunately, since the version v1.0.0-rc.4 the callback manager is not needed anymore. ( https://github.com/facebook/react-native-fbsdk/commit/321923e800f03c41b9c0c2802ee6e529af4024e2 )
So, the easiest way to solve your issue is to upgrade this library to its latest version.

However, If you can't use the release candidate and prefer to use the 0.10.1instead, you can still exclude the fbsdk package from the autolink process as mentioned here :
https://github.com/react-native-community/cli/blob/master/docs/autolinking.md

Simply edit your react-native.config.js file like this :

module.exports = {
  dependencies: {
    'react-native-fbsdk': {
      platforms: {
        android: null, // disable Android platform, other platforms will still autolink if provided
      },
    },
  },
};

I updated to but now other errors came out on MainApplication.java

What happened to these two packages?

import io.invertase.firebase.RNFirebasePackage;
import io.invertase.firebase.auth.RNFirebaseAuthPackage; // added manually

I ended up getting errors here, since I will use Authentication with Firebase I need those packages, at least it's what they say on the documentation:

            return Arrays.<ReactPackage>asList(
                    new RNFirebasePackage(), // NOT FOUND
                    new RNFirebaseAuthPackage(), // NOT FOUND
                    new MainReactPackage(),
                    new FBSDKPackage(mCallbackManager)
            );

Also, there is no com.facebook.FacebookSdk anymore, how do I run .sdkInitialize(...)?

I have got it running on device but I get this message, fair enough:
The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.

FacebookSdk.sdkInitialize() is deprecated now, simply remove it from your code.
According to this comment https://github.com/facebook/react-native-fbsdk/issues/485#issuecomment-508017055
Make sure you have added these lines

AndroidManifest.xml

<meta-data android:name="com.facebook.sdk.ApplicationId"
          android:value="@string/facebook_app_id"/>

strings.xml
<string name="facebook_app_id">YOUR_APP_ID_HERE</string>

more here :
(https://stackoverflow.com/questions/30213369/facebook-sdk-has-not-been-initialized-facebooksdk-sdkinitialize/51651988#51651988 )

iam also stuck in this process

my configurations are

{
"name": "dotCli",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"react": "16.8.6",
"react-native": "0.60.4",
"react-native-fbsdk": "^0.10.1"
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/runtime": "^7.5.5",
"@react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.8.0",
"eslint": "^6.1.0",
"jest": "^24.8.0",
"metro-react-native-babel-preset": "^0.55.0",
"react-test-renderer": "16.8.6"
},
"jest": {
"preset": "react-native"
}
}

also i have linked properly every thing don't know what's missing ....
each time i build i get the PackageList.java file with

File : PackageList.java

public ArrayList getPackages() {
return new ArrayList<>(Arrays.asList(
new MainReactPackage(),
new FBSDKPackage() -----> (I dont know why the parameter mCallbackManager doesn't get passed
to it although iam passing it the correct way in MainApplication.java )
));
}

File : MainApplication.java

package com.dotcli;

import android.app.Application;
import android.util.Log;

import com.facebook.react.PackageList;
import com.facebook.hermes.reactexecutor.HermesExecutorFactory;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.ReactApplication;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;

// For fbsdk
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
import com.facebook.appevents.AppEventsLogger;

import java.util.List;

public class MainApplication extends Application implements ReactApplication {

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

protected static CallbackManager getCallbackManager() {
return mCallbackManager;
}

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

same issue

It's because react-native 0.60 tries to link the package for you.

new FBSDKPackage() -----> (I dont know why the parameter mCallbackManager doesn't get passed
to it although iam passing it the correct way in MainApplication.java )

It's because this line does not come from YOUR mainApplication.java but the one generated by react-native cli.

You can upgrade to the latest version of fbsdk AND unlink this library since it will be linked automatically by react native OR exclude fbsdk from the autolink process

You can read more in my previous comment :
https://github.com/facebook/react-native-fbsdk/issues/597#issuecomment-513299757

@ACHP Thanks it worked for me !

You'r the best man ! Thanks 馃檹

Not sure this is relevant now, but I've come across this thread several times while trying to fix this issue for another package, so maybe it'll help others. Assuming that the arg you need is even available in PackageList.java (mCallbackManager in this case) you can specify how the package constructor is called by using a react-native.config.js file. If the package maintainer wants to fix it for the package itself then add react-native.config.js at the root of the package with

module.exports = {
  dependency: {
    platforms: {
      android: {
        packageInstance: "new FBSDKPackage(mCallbackManager)",
      },
    },
  },
};

If the package maintainer is unable to update the package for whatever reason then you can fix it just for your app by adding react-native.config.js at the root of your app before trying to install the package with

module.exports = {
  dependencies: {
    'react-native-fbsdk': {
      platforms: {
        android: {
          packageInstance: "new FBSDKPackage(mCallbackManager)",
        },
      },
    },
  },
};

After adding this file then you can do your yarn add/npm install for the package.

@spasecadet Are you sure this can work for the fbsdk package ?
How does it handle the generation of the mCallbackManager related code.

For this to work, the following code should be generated in the MainApplication.java and I really don't think that the react-native-cli is able to do that. ( Or maybe I missed something .. )

import com.facebook.CallbackManager;
...
private static CallbackManager mCallbackManager = CallbackManager.Factory.create();
protected static CallbackManager getCallbackManager() {
   return mCallbackManager;
}

I really think this issue should be closed since upgrading to the last version of this package will just solve the problem.
(And an alternative solution exist for people who can't upgrade : https://github.com/facebook/react-native-fbsdk/issues/597#issuecomment-513299757 )

poke @franzisk :)

Was this page helpful?
0 / 5 - 0 ratings