1.Add the Facebook SDK
1.In your project, open your_app | Gradle Scripts | build.gradle (Project) and add the following to the buildscript { repositories {}} section to download the SDK from the Maven Central Repository:
mavenCentral()
- In your project, open your_app | Gradle Scripts | build.gradle (Module: app) and add the following to the dependencies{} section to compile the latest version of the SDK:
implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
2.Add Facebook App ID
Create strings for your Facebook app ID and for those needed to enable Chrome Custom Tabs. Also, add FacebookActivity to your Android manifest.
1.Open your /app/res/values/strings.xml file.
2.Add the following:
<string name="facebook_app_id">136921836492269</string>
<string name="fb_login_protocol_scheme">fb136921836492269</string>
3.Open the /app/manifest/AndroidManifest.xml file.
4.Add a the following uses-permission
element after the application element:
<uses-permission android:name="android.permission.INTERNET"/>
5.Add the following meta-data
element, an activity for Facebook, and an activity and intent filter for Chrome Custom Tabs inside your application
element:
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
3.Tell Us about Your Android Project
1.On your's Facebook developers dashboard add your Android information as the image bellow:
4.Add Your Development and Release Key Hashes
To ensure the authenticity of the interactions between your app and Facebook, you need to supply us with the Android key hash for your development environment. If your app has already been published, you should add your release key hash too.
Generating a Development Key Hash
You'll have a unique development key hash for each Android development environment.
Mac OS
To generate a development key hash, open a terminal window and run the following command:
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
Windows
You will need the following:
- Key and Certificate Management Tool (keytool) from the Java Development Kit
openssl-for-windows
openssl library for Windows from the Google Code Archive
To generate a development key hash, run the following command in a command prompt in the Java SDK folder:
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\USERNAME\.android\debug.keystore" | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" sha1 -binary | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" base64
This command will generate a 28-character key hash unique to your development environment. Copy and paste it into the field below. You will need to provide a development key hash for the development environment of each person who works on your app.
Generating a Release Key Hash
Android apps must be digitally signed with a release key before you can upload them to the store. To generate a hash of your release key, run the following command on Mac or Windows substituting your release key alias and the path to your keystore:
keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64
This will generate a 28-character string that you should copy and paste into the field below. Also, see the Android documentation for signing your apps.
5.Verify Your App Event
To see events, open Facebook Analytics. Choose your app and navigate to Activity > Event Debugging.
You should see your app launch event as well as your custom events. If not, click the refresh button to load the latest events for your app.
Note: Events may take up to 20 minutes to appear in the dashboard.
Once you've verified that your events are being delivered, you're ready to create your first funnel to help you measure conversion for a sequence of events in your app. You may have to wait up to 20 minutes before events appear in the dashboard.
7.Log App Events
App events are the key actions that people take as they use your app, such as launching your app, viewing content, or making a purchase You can log app events for any meaningful actions people take in your app. There are many pre-defined events such as completed registration, viewed content, searched, added to cart, spent credits, and more.
1.Shut down the simulator.
2.In MainActivity.java, add the following import statements:
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
3.Add the follwing function to the MainActivity class:
/**
* This function assumes logger is an instance of AppEventsLogger and has been
* created using AppEventsLogger.newLogger() call.
*/
public void logSentFriendRequestEvent () {
logger.logEvent("sentFriendRequest");
}
4.Build the project and then run the project in the simulator.
5.See the results in the Dashboard.