Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.crashlytics.android.CrashlyticsMissingDependencyException during gradle 'testDebug' on android studio project

I have an Android Studio project with the latest 1.15.2 crashlytics. It works well when i build and run on my device. However after crashlytic's setup my tests fails on gradle 'testDebug'.

Output is

....app.entity.ConfigurationTest > parseTest FAILED
    java.lang.RuntimeException
        Caused by: io.fabric.sdk.android.services.concurrency.UnmetDependencyException
            Caused by: com.crashlytics.android.CrashlyticsMissingDependencyException

It can be related to the call of an activity onCreate method during the test preparation. And in the onCreate i have a line

Fabric.with(this, new Crashlytics());

It looks like it works differently in test scope.

How can i solve this or skip the crashlytics at all during tests? F.e. here is a solution which works for one person but it either don't work for me or i am putting it in invalid gradle section.

debug {
    ext.enableCrashlytics = false
} 

CrashlyticsMissingDependencyException

build.gradle

buildscript {
    repositories {
        jcenter()
    }
    repositories { mavenCentral() }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.1'
        classpath 'org.robolectric:robolectric-gradle-plugin:0.14.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

app/app.gradle

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

    dependencies {
        classpath 'io.fabric.tools:gradle:1.15.2'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

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

apply plugin: 'robolectric'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId “…”
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName “…”
    }
    signingConfigs {
        debug {
        …
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            signingConfig signingConfigs.debug
            ext.enableCrashlytics = false
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services-location:6.5.87'
    compile 'com.facebook.android:facebook-android-sdk:3.23.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'com.squareup:otto:1.3.6'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'

    provided 'com.squareup.dagger:dagger-compiler:1.2.0'
    compile 'com.squareup.dagger:dagger:1.2.0'

    compile 'com.stripe:stripe-android:1.0.0'

    androidTestCompile 'junit:junit:4.10'
    androidTestCompile ('org.robolectric:robolectric:2.3')
    androidTestCompile "org.mockito:mockito-core:1.10.19"
    compile('com.crashlytics.sdk.android:crashlytics:2.2.1@aar') {
        transitive = true;
    }
}

More output after looking in tests results files

java.lang.RuntimeException: io.fabric.sdk.android.services.concurrency.UnmetDependencyException: com.crashlytics.android.CrashlyticsMissingDependencyException: 
This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up,
install an Android build tool and ask a team member to invite you to this app's organization.

    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: io.fabric.sdk.android.services.concurrency.UnmetDependencyException: com.crashlytics.android.CrashlyticsMissingDependencyException: 
This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up,
install an Android build tool and ask a team member to invite you to this app's organization.

    at com.crashlytics.android.Crashlytics.onPreExecute(Crashlytics.java:347)
    at com.crashlytics.android.Crashlytics.onPreExecute(Crashlytics.java:229)
    at io.fabric.sdk.android.InitializationTask.onPreExecute(InitializationTask.java:27)
    at io.fabric.sdk.android.services.concurrency.AsyncTask.executeOnExecutor(AsyncTask.java:594)
    at io.fabric.sdk.android.services.concurrency.PriorityAsyncTask.executeOnExecutor(PriorityAsyncTask.java:26)
    at io.fabric.sdk.android.Kit.initialize(Kit.java:49)
    at io.fabric.sdk.android.Fabric.initializeKits(Fabric.java:417)
    at io.fabric.sdk.android.Fabric.init(Fabric.java:364)
    at io.fabric.sdk.android.Fabric.setFabric(Fabric.java:321)
    at io.fabric.sdk.android.Fabric.with(Fabric.java:292)
    at app.AppApplication.onCreate(AppApplication.java:32)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:164)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:430)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
    ... 35 more
Caused by: com.crashlytics.android.CrashlyticsMissingDependencyException: 
This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up,
install an Android build tool and ask a team member to invite you to this app's organization.

    at com.crashlytics.android.BuildIdValidator.validate(BuildIdValidator.java:59)
    at com.crashlytics.android.Crashlytics.onPreExecute(Crashlytics.java:306)
    ... 48 more

Workaround (works for me)

Temporarily i've fixed it for me using the following approach: I've created a class

public class CrashlyticsFacade {

    public static boolean enableCrashlytics = true;

    public static void initCrashlytics(Application application) {
        if (enableCrashlytics) {
            Fabric.with(application, new Crashlytics());
        }
    }

    public static void log(int priority, String tag, String msg) {
        if (enableCrashlytics) {
            Crashlytics.log(priority, tag, msg);
        }
    }

    public static void identify(String userIdentifier, String userName, String email) {
        if (enableCrashlytics) {
            if (userIdentifier != null) {
                Crashlytics.setUserIdentifier(userIdentifier);
            }
            if (userName != null) {
                Crashlytics.setUserName(userName);
            }
            if (email != null) {
                Crashlytics.setUserEmail(email);
            }
        }
    }

    public static void setEnableCrashlytics(boolean value) {
        CrashlyticsFacade.enableCrashlytics = value;
    }

And i am using it as a facade to Crashlytics in app code and tests. Additionally in test class i have to use

@BeforeClass
public static void beforeClass() {
    CrashlyticsFacade.setEnableCrashlytics(false);
}

since robolectric uses separate classloader for tests

like image 513
Rostislav Pashuto Avatar asked Feb 27 '15 10:02

Rostislav Pashuto


1 Answers

I found another solution. If you are using robolectric in tests (If not, you can add it or use answer provided by @Rostislav) you can override your application class and skip Crashlytics initialization.

// MyApplication.java in src\main\java

public class MyAplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        setupCrashlytics();
    }

    protected void setupCrashlytics() {
        Fabric.with(this, new Crashlytics());
    }
}

=======

// TestMyAplication.java in src\test\java
/**
 * Robolectric uses class with name Test<ApplicationClassName> as test variant of the application
 * class. We use test application for API class injection so we need test version of this class.
 */
public class TestMyAplication extends MyAplication {

    @Override
    protected void setupCrashlytics() {
        // nothing
    }
}
like image 187
Oleksandr Avatar answered Oct 18 '22 06:10

Oleksandr