Leos Literak
Leos Literak

Reputation: 9474

How to detect that I run Android app from debugger?

I want to turn off the Crashlytics and Answers for my devices where I develop an app. E.g. attached mobile phone or virtual device. If it was plain java I would set java property. But Android Studio misses such feature. I can see there some install flags but they are not documented.

I found that I can detect running inside Firebase with the following condition

Settings.System.getString(getContentResolver(), "firebase.test.lab") != null

but I have no idea how to set it in Android Studio.

Update:

current code based on Max's answer is

insideFirebase = Settings.System.getString(getContentResolver(), "firebase.test.lab") != null;
if (!insideFirebase) {
    CrashlyticsCore crashlyticsCore = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
    Crashlytics crashlyticsKit = new Crashlytics.Builder().core(crashlyticsCore).build();
    if (BuildConfig.DEBUG) {
        Fabric.with(this, crashlyticsKit);
    } else {
        Fabric.with(this, new Answers(), crashlyticsKit);
    }
}

Upvotes: 0

Views: 162

Answers (1)

Max Plakhuta
Max Plakhuta

Reputation: 310

You can disable crashlytics in gradle only for debug:

android {
    buildTypes {
        debug {
          // Disable fabric build ID generation for debug builds
          ext.enableCrashlytics = false
          ...

And then init crashlytycs in runtime like this:

Crashlytics crashlyticsKit = new Crashlytics.Builder()
    .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
    .build()

Fabric.with(this, crashlyticsKit);

Here more information in documentation

P.S. You can also provide any flag / variable for build type in gradle product flavors:

productFlavors {
        Dev {
            buildConfigField 'Boolean', 'enableCrashlytics', 'false'
        }

and then check it while running app:

if (BuildConfig.enableCrashlytics == true) {}

UPD: We should use

CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
Fabric.with(this, new Crashlytics.Builder().core(core).build());

instead Fabric.with(this, crashlyticsKit);

Upvotes: 2

Related Questions