Can I initialize Firebase without using google-services.json?

前端 未结 4 2038
轻奢々
轻奢々 2020-12-08 22:49

EDIT: I should emphasize, I have flavors, for which I don\'t want to use any of these Google services, and attempting to apply the google-services plugin in such a case, wit

4条回答
  •  执笔经年
    2020-12-08 23:22

    Here are couple pointers on how to use multiple firebase apps / databases in the same android project Firebase Blog and New Config Docs

    However these talk about using a secondary app in addition to the default app -- which still uses the google-services.json. I did a little bit of research and poking around in their Slack channel. Firebase uses a ContentProvider called FirebaseInitProvider. This uses the google-services.json to initialize the DEFAULT app at start up. It barfs if the json file is not present. I did the following steps:

    • Removed the google-services.json
    • Removed com.google.gms.google-services plugin. This plugin always looks for the json file.
    • FirebaseInitProvider is added via manifest merging. However in the top level manifest you can specify a node=remove tag to remove this from being added in the final manifest.

      
      

    However, even after this, I still see an exception:

    java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process myfirebasedemo.net. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                                     at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                     at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
                                                                     at com.google.firebase.iid.FirebaseInstanceIdService.zza(Unknown Source)
                                                                     at com.google.firebase.iid.FirebaseInstanceIdService.zzm(Unknown Source)
                                                                     at com.google.firebase.iid.zzb$2.run(Unknown Source)
                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                     at java.lang.Thread.run(Thread.java:818)
    

    Looks like FirebaseInstanceIdService still looks for the instance app. If you disable the service, using the same manifest node removal technique then this exception goes away. But I think this Service does important sync-ing / messaging stuff. So not sure if its wise to remove this. Long story short -- I am still looking for the perfect solution for disabling the default app / not using the google-services.json. Any further pointers will be deeply appreciated.

    UPDATE:

    Looks like this solution works after all -- without disabling the FirebaseInstanceIdService and just removing the FirebaseInitProvider as mentioned above. Just need to make sure that the Firebase.initializeApp called the first thing in Application::onCreate method.

    FirebaseApp.initializeApp(context, new FirebaseOptions.Builder().setApiKey("").
                        setApplicationId("").
                        setDatabaseUrl("").
                        setGcmSenderId("").
                        setStorageBucket("").build());  
    

提交回复
热议问题