英文:
The constructor FirebaseOptions.Builder() is deprecated
问题
Firebase使您能够将Firebase Admin SDK添加到您的服务器:
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
以前,我使用了以下代码,但是现在在Eclipse中收到一条消息,指出“构造函数FirebaseOptions.Builder()已弃用”。
InputStream serviceAccount = context.getResourceAsStream("/WEB-INF/[my-web-token].json");
try {
options = new FirebaseOptions.Builder() // <--已弃用
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
//.setDatabaseUrl(FIREBASE_DATABASE_URL)
.build();
} catch(Exception e) {
e.printStackTrace();
}
firebaseApp = FirebaseApp.initializeApp(options);
确实,Firebase建议:
Builder() 此构造函数已弃用。请改用builder()。
现在构造函数如下所示这样:
public static FirebaseOptions.Builder builder ()
如何完成此操作?如果我只是替换
FirebaseOptions options = FirebaseOptions.Builder()
用新的builder...
FirebaseOptions options = FirebaseOptions.builder()
我会收到一个错误:
FirebaseOptions.builder 无法解析为类型
文件将无法编译。
有人可以向我展示如何使用新的构造函数,或者指点我去更新的Firebase文档吗?我找不到它。
英文:
Firebase gives you the ability to add the Firebase Admin SDK to your server:
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".
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:
> Builder() This constructor is deprecated. Use builder() instead.
The constructor now looks like this:
> public static FirebaseOptions.Builder 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
and the file will not compile.
Can someone show me how to use the new constructor or point me to the updated Firebase documentation? I can't find it.
答案1
得分: 15
这是 Firebase Admin SDK for Java 版本 7.0.0 中的重大更改。发布说明中指出:
> 此版本包含了一些重大的 API 更改。有关更多详情,请参阅 Java Admin SDK v7 的迁移指南。
如果您导航到该指南,不幸的是它并未解决这个特定的情况(尽管有一个类似的有关 FCM 通知构建器的重大更改记录)。该构建器的构造函数已更改为方法,而不是对象构造函数。(您可以随意使用该页面上的“发送反馈”链接,表达您对这个缺失信息的意见。)
然而,您可以看到 FirebaseOptions.builder() 的 API 文档是开始构建 FirebaseOptions 的新方法。您还可以看到 旧的 Builder 构造函数已被弃用。
因此,您应该确保您的依赖中使用了版本为 7.x.x 的 Admin SDK,这应该允许您使用新的方法调用来创建一个新的 FirebaseOptions.Builder
对象:
FirebaseOptions.Builder builder = FirebaseOptions.builder()
或者,像您最初尝试的那样内联使用:
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
英文:
This was a breaking change in the Firebase Admin SDK for Java version 7.0.0. The release notes say:
> This release contains several breaking API changes. See the Java Admin SDK v7 migration guide for more details.
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). 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. And you can see that the old Builder constructor is deprecated.
So, you should make sure that you are using version 7.x.x of the Admin SDK in your dependencies, which should let you create a new FirebaseOptions.Builder
object using the new method call:
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);
答案2
得分: 5
尝试:
FirebaseOptions.Builder options = FirebaseOptions.builder()
英文:
Try:
FirebaseOptions.Builder options = FirebaseOptions.builder()
答案3
得分: 0
尝试移除 "new",即 .,
替代:
options = FirebaseOptions.builder()
英文:
Try removing new i.e. .,
Instead of
options = new FirebaseOptions.Builder()
use
options = FirebaseOptions.builder()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论