Search code examples
javaintellij-ideaspring-boot-maven-plugin

Cannot resolve symbol 'FirebaseApp', 'FirebaseOptions', and 'GoogleCredentials'


I am trying to connect my springboot application to firebase and I have these above errors. I creates class and pasted code snipped from firebase, added dependency to prom file.

here is dependency injection from prom file

        <dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>8.2.0</version>
        <scope>runtime</scope>
    </dependency>

Here is my Service class file

    package com.car.spares.personaldetails.services;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;

public class FirebaseInitialize {
    public void initialize() throws FileNotFoundException {
        FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");

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

           FirebaseApp.initializeApp(options);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

Solution

  • This scope:

    <scope>runtime</scope>
    

    is documented as:

    This scope indicates that the dependency is not required for compilation, but is for execution. Maven includes a dependency with this scope in the runtime and test classpaths, but not the compile classpath.

    So it's not surprising the compiler can't find it. Remove the scope and that should fix the FirebaseApp and FirebaseOptions failures. For GoogleCredential, you need to import com.google.api.client.googleapis.auth.oauth2.GoogleCredential.