I initialise the Facebook SDK differently to the examples given in their documentation.
Facebook recommend that you define meta-data android:name="com.facebook.sdk.ApplicationId
in AndroidManifest.xml
and hardcode a value for facebook_app_id
in strings.xml
. If you are using source control these files are usually checked in and, as a general rule, I don't like to commit my keys to remote repositories.
I pull in a string resource, named facebook_app_id
, from an external file with my gradle build. I then define the facebook_app_id
manually when I initialise the Facebook SDK rather than relying on the library to find it.
Initialise Facebook SDK
Initialise Facebook SDK (enables tracking of install events):
private void initialiseFacebook(Application application) {
FacebookSdk.sdkInitialize(application);
AppEventsLogger.activateApp(application, application.getString(R.string.facebook_app_id));
}
Initialise Facebook AppEventsLogger
Subsequently, I wanted to log some Facebook App Events. I could see that my install events were being successfully tracked by Facebook Analytics. However, I each time I attempted to log an event the following error was returned:
Caught unexpected exception while flushing:
java.lang.NullPointerException
For me the solution was to again manually define the facebook_app_id
in my method call to create an AppEventsLogger. This solved the problem for me.
AppEventsLogger.newLogger(application, application.getString(R.string.facebook_app_id));
Also: Enable SDK Debugging:
I found it very helpful to have more detailed logs from the Facebook SDK while I was troubleshooting this problem. You can configure it with the code below:
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.REQUESTS);
This method also has the advantage of letting you use different a facebook_app_id for each build type and/or flavour.