'Firebase Admin SDK with Java

I am trying to setup firebase admin sdk in a Java project. Following the steps at https://firebase.google.com/docs/admin/setup#add_the_sdk I successfully added the sdk by adding the dependency to my build.gradle using,

dependencies {
    compile 'com.google.firebase:firebase-admin:5.0.0'
}

then ran gradle build which returned BUILD SUCCESSFUL.

The next step in the guide uses multiple Firebase classes which I do not know where to locate. It tells me to initialize the SDK by using:

FirebaseOptions options = new FirebaseOptions.Builder()
    .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
    .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
    .build();

FirebaseApp.initializeApp(options);

The guide doesn't show where to import these Firebase classes from either. I assume the classes were downloaded after I ran build.gradle but I cant seem to locate any of these classes. Does anyone know what location the gradle should've downloaded it to or what import I must use?



Solution 1:[1]

The guide need more precise contents and example.

I have integrated Firebase with my current Spring boot project by following these blog savicprvoslav/Spring-Boot-starter , it was out dated too as using new Firebase sdk is strongly recommended. You can check my question and answer here with sample updated code. Stakoverflow The blog suggested to use this code within a configuration file (with @Configuration annotation ).

Solution 2:[2]

The Java quickstart may be a good place to look.

The Database.java file has some of the imports you're looking for:

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.database.*;

Solution 3:[3]

Simply download the JAR file from mvnrepository and include it in your project. https://mvnrepository.com/artifact/com.google.firebase/firebase-admin/8.0.1

Solution 4:[4]

You have to initialise a bean with this snippet.

How I'm doing it ->

@Configuration
public class FirebaseAppConfig {

    @SneakyThrows
    public FirebaseAppConfig() {
        FileInputStream serviceAccount =
                new FileInputStream("filepath");

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .build();

        FirebaseApp.initializeApp(options);

    }


}

This is the bean that provides the auth for your operations. The classes can be imported directly from the mvn/gradle dependency but they NEED to be combined with auth to work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mnhmilu
Solution 2 Sam Stern
Solution 3 jazeb007
Solution 4 njari