Firebase realtime database crash when android application used offline - android

I am using firebase offline database for my android app, in PreferenceScreen and load dynamic config data.
compile 'com.google.firebase:firebase-database:9.8.0'
Using it as:
FirebaseDatabase.getInstance().getReference(fireBaseReference);
I am using it as per tutorial: https://firebase.google.com/docs/database/
App works fine when it is connected, on offline it is crashing with below stack trace
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.nishant.starterkit.mock.
Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)

Seems like your app has multiple process. Make sure you initialize firebase database only through Context of your application.
if (!FirebaseApp.getApps(this).isEmpty()) {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
You should initialize firebase DB as mentioned above.

Related

Realm db in lib + Realm db in app using the lib is conflicting with each other

I have created an SDK which uses Realm db to store data. see code below how i have initialized my sdk db
Realm.init(application);
config = new RealmConfiguration.Builder().name("sdk.db")
.schemaVersion(1)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
Similarly for App in the application class.
First app db is created and initialized and the sdk method is called,which internally creates and initializes sdk.db.
But when i run the app i get following error
io.realm.exceptions.RealmException: Async transaction failed
Caused by: io.realm.exceptions.RealmException: 'class com.models.db.FavouriteIdsModel' is not part of the schema for this Realm.
at io.realm.internal.RealmProxyMediator.getMissingProxyClassException(RealmProxyMediator.java:234)
at io.realm.DefaultRealmModuleMediator.getSimpleClassNameImpl(DefaultRealmModuleMediator.java:82)
at io.realm.internal.RealmProxyMediator.getSimpleClassName(RealmProxyMediator.java:72)
at io.realm.RealmSchema.getTable(RealmSchema.java:177)
at io.realm.Realm.delete(Realm.java:1689)
at com.jiostb.jiogames.databaserealm.FavouriteMethod$4.execute(FavouriteMethod.java:79)
at io.realm.Realm$1.run(Realm.java:1601)
at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
and when i see db files, i am getting strange things
sdk.db data is store in app.db and sdk.db also has the same data
There's a chance that the SDK is overriding the default configuration, instead of using its own internal RealmConfiguration.
Also, if a library module wants to expose its schema for another module, you must use #RealmModule annotation with library = true, and specify that on the configuration.

FireStore Inside a Android Service Not Working. Database Locked ERROR

I got the following Error when I tried to access data from firestore. Tried setting persistence to true yet getting the same error.
java.lang.RuntimeException: Internal error in Firestore (0.6.6-dev).
at com.google.android.gms.internal.zzeyi.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.RuntimeException: Failed to gain exclusive lock to the Firestore client's offline persistence. This generally means you are using
Firestore from multiple processes in your app. Keep in mind that multi-
process Android apps execute the code in your Application class in all
processes, so you may need to avoid initializing Firestore in your
Application class. If you are intentionally using Firestore from multiple
processes, you can only enable offline persistence (i.e. call s
setPersistenceEnabled(true)) in one of them.
at com.google.android.gms.internal.zzetz.zza(Unknown Source)
at com.google.android.gms.internal.zzeqp.zza(Unknown Source)
at com.google.android.gms.internal.zzeqr.run(Unknown Source)
My App has a Service in it and running in a seperate process
<service
android:name=".ReceivingOrderService"
android:enabled="true"
android:exported="true"
android:process=":receivingorder"></service>
Sorry Guys I just played with code and set the
setPersistenceEnabled(true)
to
setPersistenceEnabled(false)

Test - FirebaseApp is not initialized in this process

I'm trying to run a test with Robolectric and it has an integration with Firebase. I have a project MyProject - Test that I'll be using to run the tests in a real instance of the database.
The problem is that, when running cleanup before test, I have the error:
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
at com.cgbros.silkhub.activity.listener.LoginActivityUnitTest.cleanUp(LoginActivityUnitTest.kt:26) <28 internal calls>
The test file:
https://gist.github.com/augustoerico/e88d3e5b59ae5d023d83c114b8ffa708
(I tried to copy-paste the source here. Failed miserably...)
Any insights on how I can make this work?
Thank you!
Ok, I've figured it out. It's actually a conceptual problem. The problem is that I'm trying to use the Firebase Android SDK to run a test outside of the Android context. I mean, my test is not an Android application, and I'm trying to use the Android SDK. All FirebaseApp.initializeApp() need a Context as a parameter.
So, what I have to do is to find a way to interact with my FirebaseDatabase. I think I can go with firebase-admin, or maybe use this one: https://github.com/bane73/firebase4j
Thank you for taking the time to help me (:
** Update **
I've opted to go with the Firebase REST API https://firebase.google.com/docs/reference/rest/database/
For future reference, I'm giving public full access, by tweaking the rules, so it is easier to write the tests setup and cleanup.
I've had the same problem and fixed it by initializing Firebase in the before step of my unit test:
FirebaseApp.initializeApp(InstrumentationRegistry.getInstrumentation().targetContext)
I also had the same issue a few day ago and i solve it in the same way the error says. You get that error because you're trying to get an instance of Firebase without initialize it. Please add this line of code before you try to get an instance of Firebase like this:
FirebaseApp.initializeApp(this);
Also, as Franks says, please be sure you have the following line of code added in your build.gradle file, as the last line of your entire file.
apply plugin: 'com.google.gms.google-services'
I used the most simple and straightforward approach.
Since you init your Firebase App in your Application, just create a variable there. For example,
//Your Application.class
open var isTestMode = false
onCreate(){
if(!isTestMode){
//Init your Firebase app
}
}
And then in Robolectric Application just extend your application and override the variable. In this case don't forget to provide configuration
#Config(application = TestApplication::class)
I also faced the above issue. Do all plugin download and checks by nuget. But in the end, I found the crashing was due to , Application Package Name not matching the one Assigned in Google Dev.
Application package name must matche the one that assigned in the Google Dev. Console
after add this line and then you get error
FirebaseApp.initializeApp(this);
then you have to do that also
add this line to Gradle (App)
apply plugin: 'com.google.gms.google-services'
add this line to Gradle (Project)
classpath 'com.google.gms:google-services:3.2.1'
also check if no dependncy is missing and make sure you add google-services.json

FirebaseAuthInvalidUserException after Firebase authentication migration

TL; DR
Is it possible the same Android app authenticate on both legacy and latest Firebase?
Description
We are refactoring an Android app that stills rely on legacy Firebase framework to the latest version of Google Firebase.
Unfortunately we have to authenticate on both framework versions calling simultaneously:
FirebaseAuth.signInWithEmailAndPassword
Firebase.authWithPassword
Even though the email/password works on the legacy API, the following error is thrown by the latest one:
com.google.firebase.auth.FirebaseAuthInvalidUserException:
There is no user record corresponding to this identifier.
The user may have been deleted.
at com.google.android.gms.internal.zzblv.zzce(Unknown Source)
at com.google.android.gms.internal.zzbls$zzj.zza(Unknown Source)
at com.google.android.gms.internal.zzbmd.zzcf(Unknown Source)
at com.google.android.gms.internal.zzbmd$zza.onFailure(Unknown Source)
at com.google.android.gms.internal.zzbly$zza.onTransact(Unknown Source)
at android.os.Binder.execTransact(Binder.java:453)
So does it means it is not possible the same Android app authenticate on both legacy and latest Firebase?

Cannot find GOOGLE appId for project : <projectid>

I'm implementing google authentication on firebase using the firebaseui lib.
Firebase is working properly, i can access the data in my database, and email/password auth is also working, although when i try to login using google i always get the same error :
W/AuthMethodPicker: Firebase sign in with credential unsuccessful
com.google.firebase.FirebaseException: An internal error has occured. [ Cannot find GOOGLE appId for project: 12345678. ]
at com.google.android.gms.internal.zzafg.zzes(Unknown Source)
at com.google.android.gms.internal.zzafd$zzg.zza(Unknown Source)
at com.google.android.gms.internal.zzafo.zzet(Unknown Source)
at com.google.android.gms.internal.zzafo$zza.onFailure(Unknown Source)
at com.google.android.gms.internal.zzafj$zza.onTransact(Unknown Source)
at android.os.Binder.execTransact(Binder.java:453)
i know that this value is extracted from the json file and added as a resource property during build by com.google.gms.google-services plugin, so i checked and the property was in fact added but for some reason it seems that firebase is not able to read it at runtime..
Any idea about what i can be doing wrong?
thanks!
Before you generated your app's google-services.json, did you enable Google Sign In for your project?
https://developers.google.com/identity/sign-in/android/start-integrating
Make sure you have a Firebase project
In that project make sure you have enabled GOOGLE AUTHENTICATION
Now Download a fresh copy of the google-services.json and replace it with the old one inside your app folder
yes i enabled google auth before generating the json.
Another detail is descovered in the meantime if that google auth does work without firebase-ui.
What i did was to try their example of google auth (adding to my app source) and it worked without issues.
Adding firebase-ui seems to remove the need for some extra logic so i wanted to use it, but it seems i'm either missing something or there is a bug on firebase-ui .
Leaving this here for anyone that might encounter this issue now.
Had a similar problem to above only difference is i had it on iOS. Would try to launch and sign in through google and would get the same internal error.
What worked for me was enabling the identity kit on google API and then checking "Google" and the other providers I used. You can find this on the google cloud console -> API Manager -> Dashboard.
App restart, clean build and then everything worked.

Categories

Resources