Init firebase with FirebaseOptions

Init firebase with FirebaseOptions

I want to initialise Firebase with FirebaseOptions without google-services.json, I'm following the blog post here https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html. 
I removed the FirebaseInitProvider.


I am trying to set the default FirebaseApp in the Application subclass:
FirebaseOptions options = new FirebaseOptions.Builder()
                .setApplicationId("valid_app_id")
                .setGcmSenderId("valid_gcm_sender_id")
                .setApiKey("valid_api_key")
                .build();

FirebaseApp.initializeApp(getApplicationContext(), options);

It seems everything ok, but when I want to log some events to FirebaseAnalytics, then I get this error: Missing google_app_id. Firebase Analytics disabled.
I have no idea what is the problem.

Solutions/Answers:

Solution 1:

If you are not using the google-services.json file, the information that is present in the JSON needs to be somewhere in order for the plugin/code to read. What you can do here is to create a xml file manually with string resource with the following attributes:

google_app_id:

{YOUR_CLIENT}/client_info/mobilesdk_app_id
gcm_defaultSenderId:

project_info/project_number
default_web_client_id:

{YOUR_CLIENT}/oauth_client/client_id (client_type == 3)
ga_trackingId:

{YOUR_CLIENT}/services/analytics-service/analytics_property/tracking_id
firebase_database_url:

project_info/firebase_url
google_api_key:

{YOUR_CLIENT}/api_key/current_key
google_crash_reporting_api_key:

{YOUR_CLIENT}/api_key/current_key

Take a look here. Make sure to add google_app_id, not having it would lead to the error posted in question.

If you don’t have the “tools” namespace added to your manifest root tag, you’ll have to add that as well:

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="your.package"
    >

Rest of the implementation would be same using FirebaseOptions and Firebase.InitializeApp().

References

Related:  Firebase gets stuck on no INTERNET connection