Reputation: 1524
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?
Upvotes: 8
Views: 2187
Reputation: 11
I faced the same problem and luckily I found this thread. I can confirm that the Re'em's answer above is correct but there is a small issue that the AndroidManifest.xml of androidTest is being ignored. You can find the solution to this problem here. AndroidManifest in androidTest directory being ignored
Upvotes: 1
Reputation: 1937
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:
onCreate()
(good if you want firebase to be correctly setup and testable in your android tests)to create a custom application class:
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)
}
}
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>
Upvotes: 2
Reputation: 4164
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;
Upvotes: -1