我可以在不使用google-services.json的情况下初始化Firebase吗? [英] Can I initialize Firebase without using google-services.json?

查看:2578
本文介绍了我可以在不使用google-services.json的情况下初始化Firebase吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我应该强调,我有口味,我不想使用任何这些谷歌服务,并试图在这种情况下应用谷歌服务插件,没有适用的谷歌服务。 json,会导致构建失败。

我想能够初始化Firebase SDK,特别是使用远程配置,而不使用google-services。 json。



我看到FirebaseApp有一个初始化方法,它接收一个FirebaseOptions对象。

我已经使用google-services.json中提供的值构建了FirebaseOptions,在使用这些选项调用FirebaseApp.initialize之后,我总是得到

  FirebaseInitProvider:FirebaseApp初始化失败

我知道这是使用google-services.json文件的推荐方式,但是我需要能够使应用程序调用不同的Firebase项目,具体取决于调试/版本构建,同时保持包名称相同。

我想这样做的方法是为初始化Firebase所需的所有值设置一个调试/释放对,然后动态执行初始化。

  FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId(appId)
.setApiKey(apiKey )
.setGcmSenderId(appContext.getString(
isDebug?R.string.firebase_testing_gcm_sender_id:
R.string.firebase_production_gcm_sender_id))
.setDatabaseUrl(appContext.getString(
isDebug?R.string.firebase_testing_database_url:
R.string.firebase_production_database_url))
.setStorageBucket(
appContext.getString(
isDebug?R.string.firebase_testing_storage_bucket:
R.string.firebase_production_storage_bucket))
.build();
FirebaseApp.initializeApp(appContext,options);

到目前为止,它似乎没有用。



$ p

解决方案

这里有几个关于如何使用多个firebase应用程序/数据库的指针相同的Android项目 Firebase博客新建配置文件



然而,这些讨论除了使用默认应用程序之外还使用辅助应用程序 - 仍然使用google-services.json。我做了一些研究,并在他们的Slack频道中探索。 Firebase使用名为FirebaseInitProvider的ContentProvider。这使用google-services.json在启动时初始化DEFAULT应用程序。它barf如果json文件不存在。我做了以下步骤:


  • 删除了google-services.json

  • 删除了com.google .gms.google服务插件。此插件将始终查找json文件。

  • FirebaseInitProvider是通过清单合并添加的。但是,在顶级清单中,您可以指定一个node = remove标记,以将其从最终清单中删除。

     < provider android:authorities =$ {applicationId} .firebaseinitprovider
    android:name =com。 google.firebase.provider.FirebaseInitProvider
    android:exported =false
    工具:node =remove/>




然而,即使如此,一个例外:

  java.lang.IllegalStateException:默认的FirebaseApp在这个过程中没有被初始化myfirebasedemo.net。确保先调用FirebaseApp.initializeApp(Context)。 
,位于com.google.firebase.FirebaseApp.getInstance(未知来源)
,位于com.google.firebase.iid.FirebaseInstanceId.getInstance(未知来源)
,位于com.google.firebase.iid .FirebaseInstanceIdService.zza(未知源)
,位于com.google.firebase.iid.FirebaseInstanceIdService.zzm(未知源)
,位于com.google.firebase.iid.zzb $ 2.run(未知源)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)$ b $在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588)
在java.lang.Thread.run(Thread.java:818)

看起来像FirebaseInstanceIdService仍然在寻找实例应用程序。如果您禁用该服务,则使用相同的清除节点清除技术,则此异常消失。但是我认为这个服务做重要的同步/消息传递的东西。所以不知道是否明智地删除这个。长话短说 - 我仍然在寻找禁用默认应用程序的完美解决方案/不使用google-services.json。任何进一步的指针将深受赞赏。

更新:



看起来像这个解决方案毕竟 - 没有禁用FirebaseInstanceIdService并仅删除FirebaseInitProvider,如上所述。只需要确保Firebase.initializeApp在Application :: onCreate方法中调用了第一个东西。

  FirebaseApp.initializeApp(context ,新的FirebaseOptions.Builder()。setApiKey(< VAL>)。
setApplicationId(< VAL>)。
setDatabaseUrl(< VAL>)。
setGcmSenderId(< VAL>)。
setStorageBucket(< VAL>)。build());


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, without having an applicable google-services.json, would result in a failed build.

I would like to be able to initialize the Firebase SDK, specifically to use the Remote Config, WITHOUT using the google-services.json.

I see that FirebaseApp has an initialize method, which receives a FirebaseOptions object.

I've built the FirebaseOptions with values provided in the google-services.json, and after a call to FirebaseApp.initialize with these options, I always get

FirebaseInitProvider: FirebaseApp initialization unsuccessful

I know it's the recommended way to use the google-services.json file, but I need to be able to make the app call different Firebase projects, depending on debug/release builds, while keeping the package name the same.

The way I would like to do this is to have a debug/release pairs for all values necessary to initialize Firebase, and then dynamically do the initialization.

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setApplicationId(appId)
                .setApiKey(apiKey)
                .setGcmSenderId(appContext.getString(
                        isDebug ? R.string.firebase_testing_gcm_sender_id :
                                R.string.firebase_production_gcm_sender_id))
                .setDatabaseUrl(appContext.getString(
                        isDebug ? R.string.firebase_testing_database_url :
                                R.string.firebase_production_database_url))
                .setStorageBucket(
                        appContext.getString(
                                isDebug ? R.string.firebase_testing_storage_bucket :
                                        R.string.firebase_production_storage_bucket))
                .build();
        FirebaseApp.initializeApp(appContext, options);

So far it doesn't seem to work.

All help is greatly appreciated!

解决方案

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.

    <provider android:authorities="${applicationId}.firebaseinitprovider"
        android:name="com.google.firebase.provider.FirebaseInitProvider" 
        android:exported="false"
        tools:node="remove"/>
    

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("<VAL>").
                    setApplicationId("<VAL>").
                    setDatabaseUrl("<VAL>").
                    setGcmSenderId("<VAL>").
                    setStorageBucket("<VAL>").build());  

这篇关于我可以在不使用google-services.json的情况下初始化Firebase吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆