Select Page

Crashlytics is used to keep tracking the crashes of your mobile app, you can get the basic setup and implementation from my previous post.

So now, we dive into the deep, as a developer we all face our fateful day at least once. Because after doing multiple types of testing, we push the application into production, but sometimes chick leaving the nest.

In that case, we are getting notification from crashlytics at the same time we will get the notification from some of our regular enjoyers.

If your application has accounts and logins, then you can set the identifier into crashlytics and easily find out the users, those who are all affected by the crash.

 

Identify the users:

Crashlytics provides the option for the user identification, we can easily set the identity to the users, for this you can use the below snippet.

Crashlytics.setUserIdentifier("12345");
Crashlytics.setUserEmail("jaisonfdo@gmail.com");
Crashlytics.setUserName("Jaison");

 

Log the exceptions :

Nowadays most of the developers doing one round of testing, so we can easily identify one set of edge cases and handle them by using some exotic/complex logic or simply using the exception.

Crashlytics provides the option for logging this kind of exception with default and customized messages. All logged exceptions will appear as “non-fatal” issues in our Fabric dashboard. To achieve this you can use the the below code

Crashlytics.logException(e);

We can create a custom log message associated with our crash.

Crashlytics.log("Test from sample activity");
Crashlytics.logException(e);

Crashlytics dashboard

Configure build variants:

The interesting part is now everybody has at least two build types of their application, one is for alpha/beta testing and another one is for production meantime, we can have build types like staging, debug, release, offline.etc

crashlytics_flavour

In crashlytics, every app is connected with an organization and we can have the same application in multiple organization. So we just create the same application into multiple organizations and use an individual key for each build type, with this you can get the crashes based on the build types.

To achieve this we have to use manifestPlaceholders in our build.gradle(app) like this:

android {
defaultConfig {
manifestPlaceholders = [crashlyticsApiKey: "514bec26d15bb8f67dc8129f0eda3cabf79fXXXX"]
}
buildTypes {
debug {
}
staging {
manifestPlaceholders = [crashlyticsApiKey: "848bec26d15bb8f67dc8129f0eda3cabf79fXXXX"]
}
release {
manifestPlaceholders = [crashlyticsApiKey: "427bec26d15bb8f67dc8129f0eda3cabf79fXXXX"]
}
}
}

At next, we have to insert that placeholder into the manifest file as an attribute value like this:

<meta-data
android:name="io.fabric.ApiKey"
android:value="${crashlyticsApiKey}" />

 

Disable Crashlytics for debug builds:

We have to disable this crashlytics at the time of development because we can get those report or stack trace from the logcat. So there is no point of use to get that notification from crashlytics.

The crashlytics team, provide an option to disable it at the time of debugging, we can do it by using the following code snippet.

android {
buildTypes {
debug {
// Disable fabric build ID generation for debug builds
ext.enableCrashlytics = false
...
// Set up Crashlytics, disabled for debug builds
Crashlytics crashlyticsKit = new Crashlytics.Builder()
.core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
.build();
// Initialize Fabric with the debug-disabled crashlytics.
Fabric.with(this, crashlyticsKit);

And we can do it by our own, as I said earlier in a single application contains multiple build types so we can disable crashlytics tracking for multiple build types based on our need using the below-mentioned code

android {
defaultConfig {
buildConfigField 'Boolean', 'enableCrashlytics', 'false'
}
buildTypes {
debug {
buildConfigField 'Boolean', 'enableCrashlytics', 'false'
}
staging {
buildConfigField 'Boolean', 'enableCrashlytics', 'true'
}
release {
buildConfigField 'Boolean', 'enableCrashlytics', 'true'
}
}
}

 

The source code is available on GitHub, use the below links to get the application.

View on GithubDownload .zip

Feel free to comment and share, keep watching this space get more updates on Android Stuff!

Elsewhere

Jaison Fernando

Android Developer at NFN Labs
Elsewhere