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.
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);