f-miyu / Plugin.CloudFirestore

MIT License
121 stars 44 forks source link

Cannot initialize CloudFirestore on Android #1

Open stavroaudi opened 5 years ago

stavroaudi commented 5 years ago

I have installed Plugin.CloudFirestore and Xamarin.Firebase.Firestore, but I cannot initialize CloudFirestore on Android's mainactivity.cs

Plugin.CloudFirestore.CloudFirestore.Init();

Gives an error saying that CloudFirestore does not exist in Plugin.CloudFirestore

stefanosandes commented 5 years ago

Same here!

Qormix commented 5 years ago

Have you Initialized FirebaseApp by calling

FirebaseApp.InitializeApp(Application.Context);

stefanosandes commented 5 years ago

Have you Initialized FirebaseApp by calling

FirebaseApp.InitializeApp(Application.Context);

It is expected to occur when the Plugin.CloudFirestore.CloudFirestore.Init(); is called.

        public static void Init(Context context)
        {
            try
            {
                Firebase.FirebaseApp.GetInstance(DefaultAppName);
            }
            catch (Exception)
            {
                var baseOptions = Firebase.FirebaseOptions.FromResource(context);
                var options = new Firebase.FirebaseOptions.Builder(baseOptions).SetProjectId(baseOptions.StorageBucket.Split('.')[0]).Build();

                Firebase.FirebaseApp.InitializeApp(context, options, DefaultAppName);
            }
        }
DigitalArtifex commented 5 years ago

Having a similar issue. When initializing I keep receiving a null reference: Java.Lang.NullPointerException: Attempt to read from field 'java.lang.String com.google.firebase.FirebaseOptions.zzehy' on a null object reference

If I initialize FirebaseApp manually, and try to make any calls, I get an illegal app name exception: {Java.Lang.IllegalStateException: FirebaseApp with name [FirebasePlugin] doesn't exist. Available app names: Our Pantry at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue args) [0x00069] in <42dc777b518744fdae9988e94489a4a0>:0 at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue parms) [0x00000] in /Users/builder/data/lanes/6102/8255f42f/source/monodroid/external/xamarin-android/src/Mono.Android/Android.Runtime/JNIEnv.g.cs:562 at Firebase.FirebaseApp.GetInstance (System.String name) [0x0004e] in <9eb73a399f94426da0a6f6162f59beed>:0 at Plugin.CloudFirestore.CloudFirestoreImplementation.get_Instance () [0x00000] in /Users/f-matsushima/Plugin.CloudFirestore/Plugin.CloudFirestore.Android/CloudFirestoreImplementation.cs:13 at OurPantry.Droid.RecipeStorageAndroid.get_Firestore () [0x00000] in C:\Users\james\source\repos\OurPantry\OurPantry\OurPantry.Android\RecipeStorage.cs:24 at OurPantry.Droid.RecipeStorageAndroid+d__13.MoveNext () [0x0002e] in C:\Users\james\source\repos\OurPantry\OurPantry\OurPantry.Android\RecipeStorage.cs:149 --- End of managed Java.Lang.IllegalStateException stack trace --- java.lang.IllegalStateException: FirebaseApp with name [FirebasePlugin] doesn't exist. Available app names: Our Pantry at com.google.firebase.FirebaseApp.getInstance(Unknown Source) at md58432a647068b097f9637064b8985a5e0.ButtonRenderer.n_onTouch(Native Method) at md58432a647068b097f9637064b8985a5e0.ButtonRenderer.onTouch(ButtonRenderer.java:84) at android.view.View.dispatchTouchEvent(View.java:10019) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at md51558244f76c53b6aeda52c8a337f2c37.VisualElementRenderer_1.n_dispatchTouchEvent(Native Method) at md51558244f76c53b6aeda52c8a337f2c37.VisualElementRenderer_1.dispatchTouchEvent(VisualElementRenderer_1.java:65) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at md51558244f76c53b6aeda52c8a337f2c37.Platform_DefaultRenderer.n_dispatchTouchEvent(Native Method) at md51558244f76c53b6aeda52c8a337f2c37.Platform_DefaultRenderer.dispatchTouchEvent(Platform_DefaultRenderer.java:55) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at md51558244f76c53b6aeda52c8a337f2c37.Platform_DefaultRenderer.n_dispatchTouchEvent(Native Method) at md51558244f76c53b6aeda52c8a337f2c37.Platform_DefaultRenderer.dispatchTouchEvent(Platform_DefaultRenderer.java:55) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at md51558244f76c53b6aeda52c8a337f2c37.Platform_DefaultRenderer.n_dispatchTouchEvent(Native Method) at md51558244f76c53b6aeda52c8a337f2c37.Platform_DefaultRenderer.dispatchTouchEvent(Platform_DefaultRenderer.java:55) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at md51558244f76c53b6aeda52c8a337f2c37.VisualElementRenderer_1.n_dispatchTouchEvent(Native Method) at md51558244f76c53b6aeda52c8a337f2c37.VisualElementRenderer_1.dispatchTouchEvent(VisualElementRenderer_1.java:65) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at md51558244f76c53b6aeda52c8a337f2c37.VisualElementRenderer_1.n_dispatchTouchEvent(Native Method) at md51558244f76c53b6aeda52c8a337f2c37.VisualElementRenderer_1.dispatchTouchEvent(VisualElementRenderer_1.java:65) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at md51558244f76c53b6aeda52c8a337f2c37.VisualElementRenderer_1.n_dispatchTouchEvent(Native Method) at md51558244f76c53b6aeda52c8a337f2c37.VisualElementRenderer_1.dispatchTouchEvent(VisualElementRenderer_1.java:65) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at md51558244f76c53b6aeda52c8a337f2c37.PlatformRenderer.n_dispatchTouchEvent(Native Method) at md51558244f76c53b6aeda52c8a337f2c37.PlatformRenderer.dispatchTouchEvent(PlatformRenderer.java:55) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264) at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:414) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808) at android.app.Activity.dispatchTouchEvent(Activity.java:3064) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69) at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:376) at android.view.View.dispatchPointerEvent(View.java:10243) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4438) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4306) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6247) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6221) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6182) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6350) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:323) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:6121) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:890) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:780) }

zanesc commented 5 years ago

Have you Initialized FirebaseApp by calling FirebaseApp.InitializeApp(Application.Context);

It is expected to occur when the Plugin.CloudFirestore.CloudFirestore.Init(); is called.

        public static void Init(Context context)
        {
            try
            {
                Firebase.FirebaseApp.GetInstance(DefaultAppName);
            }
            catch (Exception)
            {
                var baseOptions = Firebase.FirebaseOptions.FromResource(context);
                var options = new Firebase.FirebaseOptions.Builder(baseOptions).SetProjectId(baseOptions.StorageBucket.Split('.')[0]).Build();

                Firebase.FirebaseApp.InitializeApp(context, options, DefaultAppName);
            }
        }

Adding Plugin.CloudFirestore.CloudFirestore.Init(); isn't included in the setup instructions for Android for this project but it is necessary. Thank you.

arturo1212 commented 4 years ago

I have installed Plugin.CloudFirestore and Xamarin.Firebase.Firestore, but I cannot initialize CloudFirestore on Android's mainactivity.cs

Plugin.CloudFirestore.CloudFirestore.Init();

Gives an error saying that CloudFirestore does not exist in Plugin.CloudFirestore

I'm facing the same issue, is there a workaround or something? I already installed everything and followed the tutorial.

simondotm commented 4 years ago

As per CatalinPrata's update the initialisation step on Android that works is: Plugin.CloudFirestore.CloudFirestore.Init(Application.Context);

DanielMuhlig commented 4 years ago

@simondotm Sorry, but Plugin.CloudFirestore.CloudFirestore does not exist in Plugin.CloudFirestore v2.0.0 If I add the line in my MainActivity in my android project, the project WON'T COMPILE!

The Plugin.CloudFirestore is listed under References. Is something missing?

image

simondotm commented 4 years ago

@simondotm Sorry, but Plugin.CloudFirestore.CloudFirestore does not exist in Plugin.CloudFirestore v2.0.0 If I add the line in my MainActivity in my android project, the project WON'T COMPILE!

The Plugin.CloudFirestore is listed under References. Is something missing?

image

Ah my bad, I'm using v1.2.3 package, not the latest. 2.x

arturo1212 commented 4 years ago

I found a workaround for this adding: FirebaseFirestore firestore = FirebaseFirestore.GetInstance(Firebase.FirebaseApp.Instance); FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder().SetTimestampsInSnapshotsEnabled(true).Build(); firestore.FirestoreSettings = settings;

inside OnCreate method at MainActivity.cs.

I'm using version 2.0.0-pre1 and VS for MAC. Make sure you also add the plugin to Android project. This is how it looks:

Screen Shot 2019-09-18 at 4 18 26 PM
DanielMuhlig commented 4 years ago

@arturo1212 Not working in 2.0.0. I get the following error: Java.Lang.IllegalStateException: 'Default FirebaseApp is not initialized in this process com.companyname.MyApp. Make sure to call FirebaseApp.initializeApp(Context) first.'

image

DannyTimms commented 4 years ago

is there any fix for this yet? I'm still unable to open my project on android...

Loki606 commented 4 years ago

@DannyTimms I'm able to use it without issue. Do you have an error message and implementation code?

mhomde commented 4 years ago

is there any fix for this yet? I'm still unable to open my project on android...

@DannyTimms I thought I better reply because I had the same problem and it was driving me insane. You just have to make sure you have a google-services.json file set in your android project, it's build action need to be GoogleServicesJson,

but most importantly, you must have set your package name to on that's in your google-services.json file (and in your firestore console), otherwise you get that error

Loki606 commented 4 years ago

@DannyTimms you can also manually initialize it, by manually openning your google-services.json file and copy pasting the info here:

FirebaseFirestore database;

var options = new FirebaseOptions.Builder() .SetProjectId("--") .SetApplicationId("--") .SetApiKey("--") .SetDatabaseUrl("--") .SetStorageBucket("--") .Build();

var app = FirebaseApp.InitializeApp(this, options);

database = FirebaseFirestore.GetInstance(app);

bornemanb commented 4 years ago

make sure you have using Firebase.Firestore; at the top of mainactivity.cs

bornemanb commented 4 years ago

I have the plugin.cloud Firestore and the Xamarin.Firebase.Firestore installed alongside my google-services.json build set to GoogleServicesJson and have FirebaseApp.InitializeApp(Application Context) and still getting java.exe exited with a code 2 error. Any suggestions

vinicius-ov commented 4 years ago

Sorry for necroing this issue but I was having this same problem using Xamarin Forms. My steps to solve:

marcfabregatb commented 4 years ago

I have had a bad time treating to initialize Android project. Finally, I found out the package name from google-services.json and android project were different. Following the instructions and making sure the package names are equals worked for me. Using: