20

I have a library project (a custom view library project) which doesn't have any Activities/services. I want to integrate Crashlytics SDK to my library. When I try to add it via Crashlytics plugin (the icon in toolbar) for Android Studio, it just stays on "Checking" for "Select a kit to install". The question is how can I add Crashlytics to my Android library project?

Best Regards

user2498079
  • 2,872
  • 8
  • 32
  • 60

3 Answers3

5

It can be done, with the help of the parent app that consumes the module.

  • Set up Firebase on the parent using the normal steps.
  • Add this to the build.gradle of the library module.
  implementation 'com.google.firebase:firebase-core:16.0.1'
  implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
  • Create a class in the library module with this static variable. public static Crashlytics crash;
  • In the parents Application class, assign Crashlytics.getInstance(); to that variable.
  • The library module can call crash.core.logException(e); anywhere in the library and sent off to the Firebase console - but include a null check in case eg. library test methods are being called without a parent app.
  • Other unhandled exceptions eg throw new RuntimeException("test"); in the library should also show up in the console.
g2server
  • 5,037
  • 3
  • 29
  • 40
  • 2
    Why not use Crashlytics.logException() instead? It's static, so only use the 1st and 2nd point from your list above. – Kocus Aug 31 '19 at 19:02
4

You need an app module at the moment, even if your intention is to integrate Crashlytics only in your library. If what you only have is the library, there is no reasonable way of doing that, not officially yet at least.

See below for generating the necessary stuff first in the app side and then moving (some of them) towards your library such as the initialization.

When you onboard a kit, Fabric sets up the initialization code inside the base project, not the library. Here's how you'd do it...

Kerem
  • 2,867
  • 24
  • 35
  • Note that the 'repositories { maven { url 'https://maven.fabric.io/public' }}' has to be set in all library-projects from the root-library-project to the app-project, for the chain which contains the compile-dependency with crashlytics – arberg Sep 08 '16 at 14:21
2

Create an android library project with the following gradle:

apply plugin: 'com.android.library'
<-- other project specific includes -->

buildscript {
  repositories {
    maven { url 'https://maven.fabric.io/public' }
  }

  dependencies {
    classpath 'io.fabric.tools:gradle:1.+'
  }
}

apply plugin: 'io.fabric'

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

dependencies {
  compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
    transitive = true;
  }
}

To your base project add the maven line to allProjects/repositories:

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

From your application project add a dependency to the newly created library project. for example:

compile project(':crashlytics_lib')
Guy
  • 12,250
  • 6
  • 53
  • 70