'Question On Android (Java) FirebaseApp.initializeApp()
After experiencing difficulty with getting Firebase to update to a different Firebase project (google-services.json file in the Android App directory seemed to have no effect when a new google-services.json file was used), we realized it would be better to initialize the Android Firebase credentials directly in the App.
Setting the credentials directly in the code allows for the possibility of obfuscating the credentials instead of leaving them in google-services.json.
I found the following article on medium which addresses this: https://medium.com/nerd-for-tech/secure-your-firebases-google-services-json-file-in-android-16680f8e5fb4
However, trying to implement calling FirebaseApp.initializeApp() results in the app crashing with no apparent errors or exceptions in logcat. I'm doing the following (credentials X'ed out for obvious reasons)
FirebaseApp.initializeApp(context, new FirebaseOptions.Builder()
.setApplicationId( "XXXXXXXXX")
.setGcmSenderId("XXXXXXXXX")
.setApiKey("XXXXXXXXX")
.setStorageBucket("XXXXXXXXX")
.setProjectId("XXXXXXXXX")
.build());
db = FirebaseFirestore.getInstance();
The value in setApplicationId() is set to the mobilesdk_app_id in google-services.json
The value in setGcmSenderId() is set to the project_number in google-services.json
The value in setApiKey() is set to the current_key inside the api_key object in google-services.json
The value in setStorageBucket() is set to the storage_bucket in google-services.json
The value in setProjectId() is set to project_id in google-services.json.
Note that the medium article includes one more function call .setDatabaseUrl(). This was a problem as there is apparent database URL in google-services.json file, I've been told I can get the database URL from the Realtime Database section of the Firebase console, but I'm not using the realtime database, I'm using Firestore database, so I don't include it.
With the call to FirebaseApp.initializeApp() the app crashes, commenting out the call and the app runs.
I also tried FirebaseApp.initializeApp(context); as I was told that that would initialize using the google-services.json file, it doesn't crash but the data still goes to the old Firebase project.
Any guidance would be greatly appreciated! Thanks!
Warren
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
