Exclude testing device from Firebase Analytics logging

A solution using the ANDROID_ID

1) Get ANDROID_ID of all your testing devices (This is a 64-bit unique ID generated when the user first sets up the device. It remains constant thereafter.)

private static String getDeviceID(Context c) {
    return Settings.Secure.getString(c.getContentResolver(), Settings.Secure.ANDROID_ID);
}

2) Add these IDs to an array:

private static String testingDeviceIDs[] = {"8ab5946d3d65e893", "ada1247bfb6cfa5d", ...};

3) Check if the current device is one of the testing devices.

private static boolean isDeviceForTesting(Context c) {
    for (String testingID : testingDeviceIDs)
        if (getDeviceID(c).equals(testingID))
            return true;
    return false;
}

4) Finally, log Firebase events only if the device is not a testing device.

static void logFirebaseEvent(Context c, String name) {
    if (!isDeviceForTesting(c))
        FirebaseAnalytics.getInstance(c).logEvent(name, null);
}

UPSIDE: Unlike Firebase's provision of controlling analytics collection, this method will also work on Release builds/APKs.


You can control analytics collection using manifest metadata with the setting defined by a manifestPlaceholder:

<application
    android:name="MyApplication"
    //... >
    <meta-data
        android:name="firebase_analytics_collection_deactivated"
        android:value="${analytics_deactivated}" />
    //...
 </application>

Then define the placeholder value in the build variant blocks of your build.gradle file:

buildTypes {
    debug {
        manifestPlaceholders = [analytics_deactivated: "true"]
        //...
    }

    release {
        manifestPlaceholders = [analytics_deactivated: "false"]
        //...
    }