Disable Firebase in development mode in Android
Asked Answered
C

6

30

I am using Firebase in my android project. Wanted to know how to disable it in development mode. All crashes and usage/events are being logged and messing up with actual analytics.

Any better way to disable this in development mode?

Cristycriswell answered 18/9, 2016 at 14:12 Comment(3)
As far as i know only option to create 2 different projects for dev and prod environmentsConfab
:( No better way?Cristycriswell
That is the better way. But otherwise see #37518712Horaciohorae
D
57

Checkout https://firebase.google.com/docs/analytics/configure-data-collection?platform=android

<meta-data android:name="firebase_analytics_collection_deactivated" android:value="true" />

To do this automatically add the following line to manifest:

 <meta-data
        android:name="firebase_analytics_collection_deactivated"
        android:value="@bool/FIREBASE_ANALYTICS_DEACTIVATED"/>

Set different value of boolean for debug and release in your app/build.gradle

buildTypes {
    debug {
        resValue("bool", "FIREBASE_ANALYTICS_DEACTIVATED", "true")
    }
    release {
        resValue("bool", "FIREBASE_ANALYTICS_DEACTIVATED", "false")
    }
}
Dolphin answered 4/3, 2019 at 20:43 Comment(1)
Thanks! I also had to add tools:replace="android:value" to the AndroidManifest.xml part: (if using react-native-firebase) eg: <meta-data tools:replace="android:value" android:name="firebase_analytics_collection_deactivated" android:value="@bool/FIREBASE_ANALYTICS_DEACTIVATED"/>Stammer
M
19

Add this line to your manifest file while development.

<meta-data android:name="firebase_analytics_collection_deactivated" android:value="true" />

For more details check https://firebase.google.com/support/guides/disable-analytics

Melodious answered 28/10, 2016 at 12:43 Comment(2)
Even better, add a property to Gradle to handle it automatically: resValue("bool", "FIREBASE_ANALYTICS_DEACTIVATED", "true") such as was suggested for Crashlytics here: https://mcmap.net/q/264891/-how-to-disable-firebase-crash-reporting-when-the-app-is-running-on-debugCulminate
how to enable it back?Motherwort
C
5

It would be better to separate your dev and prod environments instead of disabling things completely. You have options on how to implement this, so you can choose what suits your team the best. This blog post details your options: https://firebase.googleblog.com/2016/08/organizing-your-firebase-enabled-android-app-builds.html

Chitwood answered 19/9, 2016 at 17:41 Comment(0)
T
1
    public class MyApp extends Application {
        public static boolean isDebuggable;

        public void onCreate() {
            super.onCreate();
            isDebuggable = (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));
            FirebaseCrash.setCrashCollectionEnabled(!isDebuggable);
        }
    }
Tadashi answered 5/11, 2017 at 17:50 Comment(0)
O
0

Since Google Play Services / Firebase 11+, we can programmatically disable the Firebase Crashlytics at runtime.

FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false);

To disable Firebase Crashlytics in Debug builds:

FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG);

or for better readability:

if(BuildConfig.DEBUG) {
    FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false);
}
Orangeism answered 23/9, 2022 at 8:19 Comment(0)
B
0

To disable crashlytics and analytics

in manifest
<meta-data
   android:name="firebase_crashlytics_collection_enabled"
   android:value="${crashlyticsCollectionEnabled}" />

<meta-data 
   android:name="firebase_analytics_collection_enabled" 
   android:value="${analyticsCollectionEnabled}" />
in gradle
buildTypes {
    debug {
       manifestPlaceholders["crashlyticsCollectionEnabled"] = false
       manifestPlaceholders["analyticsCollectionEnabled"] = false
    }
    
    release {
       manifestPlaceholders["crashlyticsCollectionEnabled"] = true
       manifestPlaceholders["analyticsCollectionEnabled"] = true
    }
}

you can check from Firebase Analytics - Firebase Crashlytics - Inject build variables into the manifest

Bullpen answered 13/7, 2023 at 12:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.