'FirebaseApp with name [DEFAULT] doesn't exist. Hosted with App Engine. (Spring boot)
Firebase config class :
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "firebase")
public class FirebaseConfig {
@PostConstruct
public void init() {
try {
FirebaseApp.getInstance();
} catch (IllegalStateException e) {
try {
FileInputStream inputStream = new FileInputStream("json-file-path");
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(inputStream)).build();
FirebaseApp.initializeApp(options);
} catch (IOException ioE) {
ioE.printStackTrace();
}
} catch (NullPointerException nullE) {
nullE.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
I also tried to use a class like this:
public class FirebaseCredentialsHelper {
public String type= "";
public String project_id= "";
public String private_key_id= "";
public String private_key= "";
public String client_email= "";
public String client_id= "";
public String auth_uri= "";
public String token_uri= "";
public String auth_provider_x509_cert_url= "";
public String client_x509_cert_url= "";
}
Unfortunately when I build the app the error " FirebaseApp with name [DEFAULT] doesn't exist." comes out. In local works fine. ps: It's set up flexible
Solution 1:[1]
This may not apply to Android. I'll update this when I get there. if I get there
To rule off some possibilities:
("For Web Apps specifically") As per their documentation, and contrary to many stack overflow responses, the runtimeOnly dependency "com.google.gms:google-services:4.3.10" is not needed as of version 8.x.x...
One thing I noticed, is after specifying a name for the app, and trying to FirebaseApp.getInstance(name), it was still not recognizing that the app was instantiated.
Another caveat I noticed, if you improperly supply the GoogleCredentials to the application using .getApplicationDefault(), the Spring app will still build itself. But trying to fetch the services will break because the Firebase service itself wasn't built. It apparently has its own built-in exception handling even if the service internally goes kaplooe... It's a good safe check to sout your cred's to make sure they're instantiated.
The Fix: @DependsOn https://www.baeldung.com/spring-depends-on
NOTE: How you load your credentials doesn't matter, as long as it finds them
Write a bean that initializes Firebase and yeets it with IoC. Provide the "dataSource" name for the Service Annotation, and a name attribute for the Bean itself.
@Service("dataSource")
class GCP {
@Bean(name = ["Firebase"])
fun initializeFirebase() {
val opts = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(
this::class.java.getResourceAsStream(System.getenv("GCP"))
))
.build()
FirebaseApp.initializeApp(opts)
}
}
Now where ever you use FirestoreClient.getFirestore() (Or a diff service), make sure you take advantage of the @DependsOn Annotation to wait and make sure that Firebase was instantiated.
@Service
@DependsOn(value = ["Firebase"])
class UserService(
private val firestore: Firestore = FirestoreClient.getFirestore()
)
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 | Kwuasimoto |
