简体   繁体   English

我可以在不使用google-services.json的情况下初始化Firebase吗?

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

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. 编辑:我应该强调,我有口味,我不想使用任何这些谷歌服务,并尝试在这种情况下应用谷歌服务插件,没有适用的google-services.json,将导致构建失败。

I would like to be able to initialize the Firebase SDK, specifically to use the Remote Config, WITHOUT using the google-services.json. 我希望能够初始化Firebase SDK,特别是使用远程配置,不使用google-services.json。

I see that FirebaseApp has an initialize method, which receives a FirebaseOptions object. 我看到FirebaseApp有一个初始化方法,它接收一个FirebaseOptions对象。

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 我使用google-services.json中提供的值构建了FirebaseOptions,在使用这些选项调用FirebaseApp.initialize之后,我总是得到

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. 我知道这是使用google-services.json文件的推荐方法,但我需要能够让应用程序调用不同的Firebase项目,具体取决于调试/发布版本,同时保持包名称相同。

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. 我想这样做的方法是为初始化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);

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 以下是关于如何在同一个Android项目Firebase博客新配置文档中使用多个firebase应用程序/数据库的几点建议

However these talk about using a secondary app in addition to the default app -- which still uses the google-services.json. 然而,除了默认应用程序之外,这些还讨论了使用辅助应用程序 - 它仍然使用google-services.json。 I did a little bit of research and poking around in their Slack channel. 我做了一些研究,并在他们的Slack频道中探讨。 Firebase uses a ContentProvider called FirebaseInitProvider. Firebase使用名为FirebaseInitProvider的ContentProvider。 This uses the google-services.json to initialize the DEFAULT app at start up. 这使用google-services.json在启动时初始化DEFAULT应用。 It barfs if the json file is not present. 如果json文件不存在则barfs。 I did the following steps: 我做了以下步骤:

  • Removed the google-services.json 删除了google-services.json
  • Removed com.google.gms.google-services plugin. 已删除com.google.gms.google-services插件。 This plugin always looks for the json file. 插件始终查找json文件。
  • FirebaseInitProvider is added via manifest merging. FirebaseInitProvider通过清单合并添加。 However in the top level manifest you can specify a node=remove tag to remove this from being added in the final manifest. 但是,在顶级清单中,您可以指定node = remove标记,以将其从最终清单中添加。

     <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. FirebaseInstanceIdService看起来仍然在寻找实例应用。 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. 长话短说 - 我仍在寻找禁用默认应用程序/不使用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. 看起来这个解决方案毕竟有用 - 没有禁用FirebaseInstanceIdService,只是删除FirebaseInitProvider,如上所述。 Just need to make sure that the Firebase.initializeApp called the first thing in Application::onCreate method. 只需要确保Firebase.initializeApp在Application :: onCreate方法中调用了第一个东西。

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

I've found interesting workaround. 我找到了有趣的解决方法。 Try the following: put your google-services.json to any sample app google provides for Firebase integration. 请尝试以下操作:将google-services.json放入任何示例应用谷歌提供的Firebase集成。 Compile it. 编译它。

Take a look on path app/build/generated/res/google-services/debug/values/ there should be generated values.xml file with bunch of strings mentioned in errors you described. 看一下路径app/build/generated/res/google-services/debug/values/应该生成带有你所描述的错误中提到的一串字符串的values.xml文件。 Copy these strings to value.xml file of your project. 将这些字符串复制到项目的value.xml文件中。 That's it. 而已。

I'm also using build variants for applying different configurations for different environments. 我也在使用构建变体来为不同的环境应用不同的配置。 I had the same issue, as some of the variants didn't have an appropriate entry on the google-services.json file. 我遇到了同样的问题,因为有些变种在google-services.json文件中没有相应的条目。 that caused some of my builds to fail when generating an app package that was not configured in firebase. 这导致我的一些构建在生成未在firebase中配置的应用程序包时失败。

My workaround for this was to add dummy applications entries for the missing packages on the relevant firebase projects, on the firebase dashboard - then getting the updated google-services.json file from it. 我的解决方法是在firebase仪表板上为相关的firebase项目中的缺失包添加虚拟应用程序条目 - 然后从中获取更新的google-services.json文件。

Might not be the best practice, but did the job in my case. 可能不是最好的做法,但在我的情况下完成了工作。

The google-services.json is totally optional and meant to be convenient. google-services.json是完全可选的,意味着方便。 So if you find it inconvenient then definitely don't use it. 因此,如果您觉得不方便,那么一定不要使用它。

As you may have noticed, the json file is only used at build time. 您可能已经注意到, json文件仅在构建时使用。 The google-services plugin turns the JSON file into a resources xml file containing some specific strings. google-services插件将JSON文件转换为包含一些特定字符串的资源xml文件。 It looks like this: 它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <! -- Present in all applications -->
    <string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string>

    <! -- Present in applications with the appropriate services configured -->
    <string name="gcm_defaultSenderId" translatable="false">1035469437089</string>
    <string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string>
    <string name="ga_trackingId" translatable="false">UA-65557217-3</string>
    <string name="firebase_database_url" translatable="false">https://example-url.firebaseio.com</string>
    <string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
    <string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
    <string name="project_id" translatable="false">mydemoapp</string>

</resources>

You can just create these entries in your own strings.xml file and then FirebaseApp.initializeApp() will find them at runtime. 您可以在自己的strings.xml文件中创建这些条目,然后FirebaseApp.initializeApp()将在运行时找到它们。 Not all of the keys are required, it depends what service you're using. 并非所有密钥都是必需的,这取决于您使用的是哪种服务。 For example almost all services require the google_app_id key. 例如,几乎所有服务都需要google_app_id密钥。 Firestore only needs that and the project_id , while Firebase Auth needs the google_api_key as well. Firestore只需要它和project_id ,而Firebase Auth也需要google_api_key It shouldn't be too hard to experiment and figure out which ones you need. 试验并找出你需要的东西应该不难。

Some links: 一些链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM