Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 436
Kyle Avatar asked Jul 01 '26 14:07

Kyle


1 Answers

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>

like image 85
Re'em Avatar answered Jul 04 '26 03:07

Re'em



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!