Home » Android » Android Fabric Crashlytics Integration

About Chryssa Aliferi

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.

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:

Want to create a kick-ass Android App?

Subscribe to our newsletter and download the Android Programming Cookbook right now!

With this book, you will delve into the fundamentals of Android programming. You will understand user input, views and layouts. Furthermore, you will learn how to communicate over Bluetooth and also leverage Google Maps into your application!

 

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
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of