Android

Android Fabric Crashlytics Integration

Twitter has recently released a new software development kit. Its name is Fabric and it provides a set of crash reporting and mobile analytics tools for mobile applications. One of the tools that Fabric offers, is Crashlytics. Crashlytics is a crash reporting service, that can track your Android application crashes and lots of statistical information and analytics. Crashlytics is a very known tool, it is completely free of charge and can help you have a very detailed view of your application crashes in the real production world.

Twitter has also created a plugin both for Android Studio as well as for Eclipse, in order to fasten this procedure with very specific details and step by step instructions.
In this example, we will see how we can use Fabric via Android Studio in order to add Crashlytics in our Android application and implement the crash tracking service step by step.

For our example will use the following tools in a Windows 64-bit or an OS X platform:

  • JDK 1.7
  • Android Studio 1.3.2
  • Android SDK 5.1

Let’s take a closer look:

1. Import Fabric plugin into Android Studio

Tip
You may skip the plugin installation on Android Studio and jump directly to the beginning of the example below.

For this example, we have to install the Fabric Plugin for Android Studio. Let’s open Android Studio select Configure > Plugins.

“Welcome to Android Studio” screen. Choose “Configure”.

In the "Configuration" screen choose "Plugins".
In the “Configuration” screen choose “Plugins”.

Then we should find the “Fabric” Plugin, download and install it into Android Studio.

Type "Fabric" and click "Browse Directories".
Type “Fabric” and click “Browse Directories”.

Download the "Fabric" Plugin into Android Studio.
Download the “Fabric” Plugin into Android Studio.

Restart Android Studio, after "Fabric" plugin download.
Restart Android Studio, after “Fabric” plugin download.

2. Create a New Android Application Project (or open a project you want to integrate Crashlytics)

Open Android Studio and choose “Start a new Android Studio Project” in the welcome screen.

“Welcome to Android Studio” screen. Choose “Start a new Android Studio Project”.
“Welcome to Android Studio” screen. Choose “Start a new Android Studio Project”.

Specify the name of the application, the project and the package.

“Configure your new project” screen. Add your application name and the projects package name.
“Configure your new project” screen. Add your application name and the projects package name.

In the next window, select the form factors your app will run on.

“Target Android Devices” screen.
“Target Android Devices” screen.

In the next window you should choose to “Add an activity to Mobile”. In our example, we will choose to create a project with no activity, because we will migrate our Activities for the eclipse formed project. So, choose: “Add no activity”.

“Add an activity to Mobile”. Choose: “Add no activity”.
“Add an activity to Mobile”. Choose: “Add no activity”.

Now, our project has just been created. This is how it looks like in the “Android” project view:

This is how our Android Studio workspace looks like with the new project and the "Fabric" plugin opened.
This is how our Android Studio workspace looks like with the new project and the “Fabric” plugin opened.

Tip
You can add the Fabric library and integrate Crashlytics even if you do not install the Fabric Android Studio Plugin. Just follow the instructions below, and add Fabric on build.gradle and AndroidManifest.xml. Do not forget to register an account on the Fabric.io web application, and add your own Fabric ApiKey for your specific application.

3. Composing build.gradle file to include Crashlytics

Open build.gradle file, and add the following code:

build.gradle

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'


repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.javacodegeeks.androidcrashlyticsexample"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }
}

4. Adding the source code in the starting Activity of our application

We are going now to add the Crashlytics code in our starting Activity.

Open src/main/java/com.javacodegeeks.androidcrashlyticsexample/StartActivity.java file and paste the code below.

StartActivity.java

package com.javacodegeeks.androidcrashlyticsexample;

import android.os.Bundle;
import android.app.Activity;
import io.fabric.sdk.android.Fabric;
import com.crashlytics.android.Crashlytics;

public class StartActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());
        setContentView(R.layout.activity_sample);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
Tip
It is essential to register your Fabric account from the Android Studio plugin, or from the Fabric.io web application. Follow the instructions and you will see a full integration guide. Please follow this guide as long you read this example. Throughout this guide you will find out how to make a new account, to add a new Android application and the ApiKey for your specific application.

5. Android Manifest

The AndroidManifest.xml of our project is simple and contains the permissions:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.javacodegeeks.androidcrashlyticsexample"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".StartActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="io.fabric.ApiKey"
            android:value="2f160620708ff1ba8a44940d7808bec5d334c9b6" />
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

6. Build and run the application

When we build, compile and run our project, the Fabric web application control panel will start to track crashes and general statistics.

7. Download the Android Studio Project

This was an example of Android Fabric Crashlytics implementation.

Download
You can download the full source code of this example here: AndroidFabricCrashlyticsExample

Chryssa Aliferi

Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button