-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Crash Reporting with Crashlytics
Crashlytics is a part of Fabric.io and gives mobile app developers insight into their apps’ performance, so you can pinpoint and fix issues quickly and easily. It is free to use for everybody.
- Register on Fabric.io
- Download Android Studio plugin Fabric for Android
- After restart Android Studio, open your project and click Fabric button
to login into your account
- Next click on +New app and select your organization which was given during registration
- Select Crashlytics from available kits
- On next step select install button to run integration wizard
- Wizard provide all required code for you. You can either copy it yourself or let wizard to do it for you
- After finish you have to synchronize gradle and run your project to verify configuration
- That's it, you successfully configured Crashlytics. Each of uncaught exceptions will be send to Fabric
- To test crash reporting let's put this code into your onCreate method
throw new RuntimeException("This is a crash");
- After few second you can see crash in Fabric dashboard
build.gradle
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
// We recommend changing it to the latest version from our changelog:
// https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
// Put Fabric plugin after Android plugin
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
...
compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
transitive = true;
}
}
AndroidManifest.xml
<application
...
<meta-data
android:name="io.fabric.ApiKey"
android:value="YOUR_API_KEY" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
MainActivity or CustomApplication class
import com.crashlytics.android.Crashlytics;
import io.fabric.sdk.android.Fabric;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
Crashlytics provides 4 logging mechanisms right out of the box: logging, custom keys, user information, and caught exceptions
For any individual app session, only the most recent 8 logged exceptions are stored.
All logged exceptions will appear as “non-fatal” issues in the Fabric dashboard. Crashlytics processes exceptions on a dedicated background thread, so the performance impact to your app is minimal. To reduce your users’ network traffic, Crashlytics batches logged exceptions together and sends them the next time the app launches.
try {
myMethodThatThrows();
} catch (Exception e) {
Crashlytics.logException(e);
// handle your exception here!
}
Logged messages are associated with your crash data and are visible in the Crashlytics dashboard if you look at the specific crash itself.
Crashlytics.log("Higgs-Boson detected! Bailing out...");
Crashlytics supports a maximum of 64 key/value pairs. Once you reach this threshold, additional values are not saved. Each key/value pair can be up to 1 KB in size.
Crashlytics allows you to associate arbitrary key/value pairs with your crash reports, which are viewable right from the Crashlytics dashboard. Setting keys are as easy as calling: Crashlytics.setString(key, value) or one of the related methods. Options are:
void setBool(String key, boolean value);
void setDouble(String key, double value);
void setFloat(String key, float value);
void setInt(String key, int value);
You can use Crashlytics.setUserIdentifier to provide an ID number, token, or hashed value that uniquely identifies the end-user of your application without disclosing or transmitting any of their personal information. This value is displayed right in the Fabric dashboard.
void Crashlytics.setUserIdentifier(String identifier);
void Crashlytics.setUserName(String name);
void Crashlytics.setUserEmail(String email);
If you don’t need Crashlytics crash reporting or beta distribution for debug builds, you can safely speed up your debug-builds by disabling the plugin entirely with these two steps:
First, add this to your app’s build.gradle:
android {
buildTypes {
debug {
// Disable fabric build ID generation for debug builds
ext.enableCrashlytics = false
...
Next, disable the Crashlytics kit at runtime. Otherwise, the Crashlytics kit will throw the following error:
com.crashlytics.android.core.CrashlyticsMissingDependencyException: This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up`
You can disable the kit at runtime for debug builds only with the following code:
// 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);
This guide was originally authored by Mateusz Utkała for the CodePath guides.
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.