简体   繁体   English

构造函数 FirebaseOptions.Builder() 已弃用

[英]The constructor FirebaseOptions.Builder() is deprecated

Firebase gives you the ability to add the Firebase Admin SDK to your server : Firebase 使您能够将 Firebase 管理员 SDK 添加到您的服务器

FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
.build();

FirebaseApp.initializeApp(options);

Previously, I used the following code, however, I am now getting a message in Eclipse that "The constructor FirebaseOptions.Builder() is deprecated".以前,我使用以下代码,但是,我现在在 Eclipse 中收到一条消息,即“构造函数 FirebaseOptions.Builder() 已弃用”。

InputStream serviceAccount = context.getResourceAsStream("/WEB-INF/[my-web-token].json");
    try {
        options = new FirebaseOptions.Builder() // <--DEPRECATED
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            //.setDatabaseUrl(FIREBASE_DATABASE_URL)
            .build();
        } catch(Exception e) {
            e.printStackTrace();
        }

        firebaseApp = FirebaseApp.initializeApp(options);

Sure enough, Firebase advises :果然,Firebase 建议

Builder() This constructor is deprecated. Builder() 此构造函数已弃用。 Use builder() instead.改用 builder() 。

The constructor now looks like this :构造函数现在看起来像这样

public static FirebaseOptions.Builder builder ()公共 static FirebaseOptions.Builder 构建器 ()

How is this accomplished?这是如何实现的? if I just replace如果我只是更换

FirebaseOptions options = FirebaseOptions.Builder()
...

with the new builder...与新的建设者...

FirebaseOptions options = FirebaseOptions.builder()
...

I get an error:我收到一个错误:

FirebaseOptions.builder cannot be resolved to a type FirebaseOptions.builder 无法解析为类型

and the file will not compile.并且该文件将无法编译。

Can someone show me how to use the new constructor or point me to the updated Firebase documentation?有人可以告诉我如何使用新的构造函数或指向更新的 Firebase 文档吗? I can't find it.我找不到它。

This was a breaking change in the Firebase Admin SDK for Java version 7.0.0 .这是 Firebase Admin SDK for Java版本 7.0.0 中的重大更改。 The release notes say:发行说明说:

This release contains several breaking API changes.此版本包含多项重大 API 更改。 See the Java Admin SDK v7 migration guide for more details.有关更多详细信息,请参阅 Java Admin SDK v7迁移指南

If you navigate to that guide, unfortunately it does not address this specific situation (though there is a similar breaking change documented for the FCM notification builder ).如果您导航到该指南,不幸的是它没有解决这种特定情况(尽管FCM 通知生成器记录类似的重大更改)。 The builder constructor was changed to a method rather than an object constructor.构建器构造函数被更改为方法而不是对象构造函数。 (Please feel free to use the "send feedback" link on that page to express your opinion about this missing information.) (请随时使用该页面上的“发送反馈”链接来表达您对这些缺失信息的看法。)

You can see, however, that the API documentation for FirebaseOptions.builder() is the new way to start build of FirebaseOptions.但是,您可以看到FirebaseOptions.builder()的 API 文档是开始构建 FirebaseOptions 的新方法。 And you can see that the old Builder constructor is deprecated .您可以看到旧的 Builder 构造函数已被弃用

So, you should make sure that you are using version 7.xx of the Admin SDK in your dependencies, which should let you create a new FirebaseOptions.Builder object using the new method call:因此,您应该确保在您的依赖项中使用 Admin SDK 7.xx 版,这应该允许您使用新方法调用创建一个新的FirebaseOptions.Builder对象:

FirebaseOptions.Builder builder = FirebaseOptions.builder()

Or, used inline as you originally tried:或者,像您最初尝试的那样使用内联:

FirebaseOptions options = FirebaseOptions.builder()
    .setCredentials(GoogleCredentials.getApplicationDefault())
    .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
    .build();

FirebaseApp.initializeApp(options);

尝试:

FirebaseOptions.Builder options = FirebaseOptions.builder()

Try removing new ie .,尝试删除新的 ie .,

Instead of代替

options = new FirebaseOptions.Builder()

use利用

options = FirebaseOptions.Builder()

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

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