React Native fb-sdk 找不到符号 CallbackManager
React Native fb-sdk cannot find symbol CallbackManager
React Native 和 fbsdk 版本:
"react": "16.6.3",
"react-native": "0.58.5",
"react-native-fbsdk": "^0.8.0"
我收到以下错误:
> Task :app:compileDebugJavaWithJavac FAILED
C:\Users\xxxx\Desktop\project\android\app\src\main\java\com\project\MainApplication.java:22: error: cannot find symbol
private static CallBackManager mCallbackManager = CallbackManager.Factory.create();
^
symbol: class CallBackManager
location: class MainApplication
C:\Users\xxxx\Desktop\project\android\app\src\main\java\com\project\MainApplication.java:24: error: cannot find symbol
protected static CallBackManager getCallbackManager() {
^
symbol: class CallBackManager
location: class MainApplication
2 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
问题似乎与这部分代码有关,但我不太清楚哪里出了问题。我完全按照他们 official repository.
中提供的说明进行操作
public class MainApplication extends Application implements ReactApplication {
private static CallBackManager mCallbackManager = CallbackManager.Factory.create();
protected static CallBackManager getCallbackManager() {
return mCallbackManager;
}
// ...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new FBSDKPackage(mCallbackManager)
);
}
// ...
}
我还手动将以下导入添加到 MainActivity.java
:
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
将这些行添加到 app/build.gradle
:
implementation project(':react-native-fbsdk')
implementation 'com.facebook.android:facebook-android-sdk:4.34.0'
这些 settings.gradle
:
include ':react-native-fbsdk'
project(':react-native-fbsdk').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fbsdk/android')
如果有人碰巧遇到了同样的问题,这与 npm 有关,而不是 link 正确地将 fb-sdk 添加到项目中。我尝试使用 npm install
和 npm link
,但没有成功,尽管 npm 没有抛出任何错误。但是,使用 react-native
安装 sdk 解决了问题:
react-native install react-native-fbsdk
react-native link react-native-fbsdk
如果您在尝试 link 时 运行 遇到 MODULE_NOT_FOUND 错误,请在项目根文件夹和 运行:
上打开命令行
cd android
gradlew clean
此外,虽然他们的官方 GitHub repository does contain instructions regarding the installation process, I recommend following the facebook developers page 相反,因为它在可读性和细节方面要好得多。
React Native 和 fbsdk 版本:
"react": "16.6.3",
"react-native": "0.58.5",
"react-native-fbsdk": "^0.8.0"
我收到以下错误:
> Task :app:compileDebugJavaWithJavac FAILED
C:\Users\xxxx\Desktop\project\android\app\src\main\java\com\project\MainApplication.java:22: error: cannot find symbol
private static CallBackManager mCallbackManager = CallbackManager.Factory.create();
^
symbol: class CallBackManager
location: class MainApplication
C:\Users\xxxx\Desktop\project\android\app\src\main\java\com\project\MainApplication.java:24: error: cannot find symbol
protected static CallBackManager getCallbackManager() {
^
symbol: class CallBackManager
location: class MainApplication
2 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
问题似乎与这部分代码有关,但我不太清楚哪里出了问题。我完全按照他们 official repository.
中提供的说明进行操作public class MainApplication extends Application implements ReactApplication {
private static CallBackManager mCallbackManager = CallbackManager.Factory.create();
protected static CallBackManager getCallbackManager() {
return mCallbackManager;
}
// ...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new FBSDKPackage(mCallbackManager)
);
}
// ...
}
我还手动将以下导入添加到 MainActivity.java
:
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
将这些行添加到 app/build.gradle
:
implementation project(':react-native-fbsdk')
implementation 'com.facebook.android:facebook-android-sdk:4.34.0'
这些 settings.gradle
:
include ':react-native-fbsdk'
project(':react-native-fbsdk').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fbsdk/android')
如果有人碰巧遇到了同样的问题,这与 npm 有关,而不是 link 正确地将 fb-sdk 添加到项目中。我尝试使用 npm install
和 npm link
,但没有成功,尽管 npm 没有抛出任何错误。但是,使用 react-native
安装 sdk 解决了问题:
react-native install react-native-fbsdk
react-native link react-native-fbsdk
如果您在尝试 link 时 运行 遇到 MODULE_NOT_FOUND 错误,请在项目根文件夹和 运行:
上打开命令行cd android
gradlew clean
此外,虽然他们的官方 GitHub repository does contain instructions regarding the installation process, I recommend following the facebook developers page 相反,因为它在可读性和细节方面要好得多。