'Disable Firebase initialization when running instrumentation tests for modules

I have a multi module project. I want to be able to run the instrumentation tests i'm writing for these modules separately. I am continually running into the below error when I run the tests and initialize the activity.

Default FirebaseApp is not initialized in this process. Make sure to call FirebaseApp.initializeApp(Context) first.

I have tried explicitly calling this in my test code with the test app's context, but to no avail. I know firebase is set up through the config files and I don't explicitly call it to set it up. It automatically is initialized through a content provider (read a blog) before the actual app starts.

Because it would be a static call and we don't explicitly call it, I don't see a way to mock it either. Is there a way to completely disable firebase for my UITest runs in my modules?



Solution 1:[1]

Firebase are using a content-provider to hook the library up before the application's onCreate() method. This content provider is registered in the library com.google.firebase.firebase-perf (which is imported by the line platform("com.google.firebase:firebase-bom:... from your build.gradle file)

To stop firebase hook-up process problems, you can either:

  1. create a custom Application class for android-test, call FirebaseApp.initializeApp() once in the application's onCreate() (good if you want firebase to be correctly setup and testable in your android tests)
  2. disable firebase's provider (perfect if you don't need fire, but might cause problems if you use firebase services (such as creashlytics etc) in your tested code)

How to do it?


to create a custom application class:

  1. in folder src/androidTest/ create file MyCustomTestApp.kt (or any other class name), with content:
package <...> // use the default package that was set when you created the file

import android.app.Application
import com.google.firebase.FirebaseApp

class MyCustomTestApp: Application() {
    override fun onCreate() {
        super.onCreate()
        FirebaseApp.initializeApp(this)
    }
}
  1. in file src/androidTest/AndroidManifest.xml file (create new file if it doesn't exist yet) add the following:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:name="here setup the path to your custom application class, e.g. com.yourApp.package.name.MyCustomTestApp"
        tools:replace="android:name">

        <--! if you have test activities etc, you register them here ... -->

    </application>
</manifest>

to disable firebase's content-provider altogether:

in file src/androidTest/AndroidManifest.xml file (create new file if it doesn't exist yet) add the following:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application>

        <!-- disable firebase provider to get rid of "Default FirebaseApp is not initialized in this process" exceptions -->
        <provider
            android:authorities="${applicationId}.firebaseperfprovider"
            android:name="com.google.firebase.perf.provider.FirebasePerfProvider"
            tools:node="remove" />

    </application>
</manifest>

Solution 2:[2]

If you have an environmental value that you can listen for or a state within your app that you can know exists or does not exist in those conditions, you can initiate it as needed. This does however require ALL Firebase calls to do a health check to prevent them from firing if the apps.length == 0

if (firebase.apps.length > 0) {
    // run firebase request
}

or

if (firebase.apps.length == 0) return;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Re'em
Solution 2 DIGI Byte