将facebook SDK添加到react-native项目

时间:2016-06-09 07:18:22

标签: react-native facebook-sdk-4.0 react-native-fbsdk

我正在尝试使用此链接在我的react-native项目中添加facebook sdk - https://developers.facebook.com/docs/react-native/getting-started-android

但是,rnpm没有按照链接中的描述自动添加SDK。 所以,我使用此链接手动添加了react-native-fbsdk - https://github.com/facebook/react-native-fbsdk/issues/205

然后,我构建我的项目并生成发布和调试apk。 但是,现在Android应用程序在发布时崩溃了。 并且,没有办法找出为什么会发生这种情况。

这是代码 -

MainActivity.java

package com.netizen_hybrid;


import android.content.Intent;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;

import com.facebook.FacebookSdk;
import com.facebook.CallbackManager;
import com.facebook.reactnative.androidsdk.FBSDKPackage;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends ReactActivity {
    CallbackManager mCallbackManager;
    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    
    @Override
    protected String getMainComponentName() {
        return "Netizen_Hybrid";
    }

    /**
     * Returns whether dev mode should be enabled.
     * This enables e.g. the dev menu.
     */
    @Override
    protected boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
    }

    /**
     * A list of packages used by the app. If the app uses additional views
     * or modules besides the default ones, add more packages here.
     */
    @Override
    protected List<ReactPackage> getPackages() {
        mCallbackManager = new CallbackManager.Factory().create();
        ReactPackage packages[] = new ReactPackage[]{
           new MainReactPackage(),
           new FBSDKPackage(mCallbackManager),
        };
        return Arrays.<ReactPackage>asList(packages);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }
    
}

build.gradle -

dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+"  // From node_modules
compile 'com.facebook.android:facebook-android-sdk:4.+'
compile project(":react-native-fbsdk")

}

settings.gradle -

rootProject.name = 'Netizen_Hybrid'

include ':app'

include ':react-native-fbsdk'
project(':react-native-fbsdk').projectDir = new File(settingsDir, '../node_modules/react-native-fbsdk/Android')

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.netizen_hybrid"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />

    <application
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/AppTheme"
      android:name="com.netizen_hybrid.MyApplication"
      >
      
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.FacebookActivity"
          android:configChanges=
                 "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
          android:theme="@android:style/Theme.Translucent.NoTitleBar"
          android:label="@string/app_name" />
      <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
      <provider android:authorities="com.facebook.app.FacebookContentProvider142933766117579"
          android:name="com.facebook.FacebookContentProvider"
          android:exported="true" />
    </application>

</manifest>

MyApplication.java -

import android.app.Application;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize the SDK before executing any other operations,
        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
    }
}

任何人都可以告诉我哪里做错了吗? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

这些配置对我来说是正确的。但我可以俯视某事。您可以尝试为HelloFacebook示例应用https://github.com/facebook/react-native-fbsdk/tree/master/Sample生成apk吗?这可能有助于查看遗漏的内容。此外,您还可以将我指向GH上的项目(仅包含必要的代码),以便我可以帮助您进行调试。

答案 1 :(得分:0)

可能是因为您未在MyApplication.java中指定了自己的套餐吗?尝试按照与MainActivity.java中相同的方式进行设置。

MyApplication.java:

package com.netizen_hybrid;

... 
我犯了同样的错误。我在尝试启动应用之前运行adb logcat找到了问题。日志输出将我指向我的Application类。