Android instrumentation test in Library module with Firebase

Viewed 221

I have my project structured like the following:

project
  ∟ app // apply `google-services` and `firebase-crashlytics` plugins here
    ∟ google-services.json
  ∟ lib1 // including Firebase SDK, and Firebase related operations here
  ∟ lib2

It works well when running as a normal app. But when I run androidTest in lib1, I got the following exception:

Fatal Exception: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this XXX. Make sure to call FirebaseApp.initializeApp(Context) first.

I tried to solve it by putting a strings.xml inside /androidTest/values/res/ with the google-services related config

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <! -- Present in all applications -->
    <string name="google_app_id" translatable="false">client_info/mobilesdk_app_id</string>

    <! -- Present in applications with the appropriate services configured -->
    <string name="gcm_defaultSenderId" translatable="false">${project_info/project_number}</string>
    <string name="default_web_client_id" translatable="false">${oauth_client/client_id}</string>
    <string name="ga_trackingId" translatable="false">${analytics_property/tracking_id}</string>
    <string name="firebase_database_url" translatable="false">${project_info/firebase_url}</string>
    <string name="google_api_key" translatable="false">${api_key/current_key}</string>
    <string name="google_crash_reporting_api_key" translatable="false">${api_key/current_key}</string>
    <string name="project_id" translatable="false">${project_info/project_id}</string>

</resources>

But then, I encountered another problem

java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.IllegalStateException: The Crashlytics build ID is missing. This occurs when Crashlytics tooling is absent from your app's build configuration. Please review Crashlytics onboarding instructions and ensure you have a valid Crashlytics account.

I tried applying firebase-crashlytics plugin in lib1 module, but I got the error

Caused by: org.gradle.api.GradleException: Applying the Firebase Crashlytics plugin to a library project is unsupported. It should only be applied to the application module of your project to enable automatic upload of obfuscation mapping files for your application.

So I have to keep firebase-crashlytics plugin in app module.

Then I tried to move test into app module, but got another error

java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/test/internal/runner/tracker/UsageTrackerRegistry;
        at androidx.test.espresso.GraphHolder.baseLayer(GraphHolder.java:9)
        at androidx.test.espresso.Espresso.<clinit>(Espresso.java:2)
        at androidx.test.espresso.Espresso.onView(Espresso.java:38)

Any help is welcome. Thanks.

1 Answers

Completely remove the firebase dependencies from the instrumented tests in that module by adding the following to your build.gradle file:

configurations {
    androidTestImplementation {
        exclude group: 'com.google.firebase', module: 'firebase-perf'
    }
}

This way it won't complain anymore as firebase is not on the tests and that code should not be run if you have correctly replaced the repo or data sources before with your DI or using test doubles.

Related