I initialize the Facebook SDK in different ways with the examples given in their documentation.
Facebook recommends that you define meta-data android:name="com.facebook.sdk.ApplicationId in AndroidManifest.xml and hardcode value for facebook_app_id in strings.xml . If you use a control source, these files are usually checked and usually I do not like to pass my keys to remote repositories.
I am outputting a string resource called facebook_app_id from an external file with my gradle construct. Then I define facebook_app_id manually when I initialize the SDK for Facebook, rather than relying on the library to find it.
Initialize SDK
Initialize the Facebook SDK (allows you to track installation events):
private void initialiseFacebook(Application application) { FacebookSdk.sdkInitialize(application); AppEventsLogger.activateApp(application, application.getString(R.string.facebook_app_id)); }
Initialize Facebook AppEventsLogger
Subsequently, I wanted to register Facebook App Events . I could see that my installation events were successfully tracked by Facebook Analytics. However, every time I tried to register an event, the following error was returned:
Caught an unexpected exception while flushing: java.lang.NullPointerException
For me, the solution was to manually define facebook_app_id again in my method call to create the AppEventsLogger. This solved the problem for me.
AppEventsLogger.newLogger(application, application.getString(R.string.facebook_app_id));
Also: Enable SDK debugging:
It was very useful for me to have more detailed logs from the Facebook SDK when I was looking for this problem. You can configure it using the code below:
FacebookSdk.setIsDebugEnabled(true); FacebookSdk.addLoggingBehavior(LoggingBehavior.REQUESTS);
This method also has the advantage that you can use different facebook_app_id for each type of build and / or taste.