13

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.

<provider
    android:name="com.google.firebase.provider.FirebaseInitProvider"
    android:authorities="${applicationId}.firebaseinitprovider"
    tools:node="remove"/>

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.

rMozes
  • 201
  • 3
  • 9
  • Possible duplicate of [Use multiple firebase accounts in single android app for google analytics](https://stackoverflow.com/questions/45546833/use-multiple-firebase-accounts-in-single-android-app-for-google-analytics) – ceph3us Aug 09 '17 at 12:42
  • Have you tried with the overload of initializeApp? ´FirebaseApp.initializeApp(getApplicationContext(), options, "someappname");´ – Xaren Jun 06 '18 at 18:23
  • Hello @rMozes, Your issue solved or not ? – Jaimin Modi Aug 23 '19 at 10:13
  • You are using FirebaseOptions and Link you given have FirebaseOptions.Builder..!!! – Jaimin Modi Aug 23 '19 at 10:14
  • Hey doubt "setApplicationId()" where do i get this id? is it "1:576500000006:android:124ac0dfab06ff730858f3" ? – nikhil Oct 14 '19 at 10:05
  • @rMozes Were you able to solve this issue ? If yes, Please share the solution. – K Pradeep Kumar Reddy Jul 02 '21 at 01:24

2 Answers2

0

Unfortunately Firebase Analytics requires to have google_app_id in xml. But you can avoid it by creating a context wrapper and provide the key programmatically.

Example:

public class App extends Application {

    private ResourcesWrapper firebaseResources;

    @Override
    public Resources getResources() {
        if (firebaseResources == null)
            firebaseResources = new ResourcesWrapper(super.getResources());
        return firebaseResources;
    }

}

public class ResourcesWrapper extends Resources {

    private final int R_STRING_GOOGLE_APP_ID = 1_999_999_999;
    private final Resources wrapped;

    public FirebaseResourcesWrapper (Resources wrapped) {
        super(wrapped.getAssets(), wrapped.getDisplayMetrics(), wrapped.getConfiguration());
        this.wrapped = wrapped;
    }

    @Override
    public int getIdentifier(String name, String defType, String defPackage) {
        if ("google_app_id".equals(name) && "string".equals(defType)) return R_STRING_GOOGLE_APP_ID;
        return wrapped.getIdentifier(name, defType, defPackage);
    }

    @Override
    public String getString(int id) throws NotFoundException {
        if (id == R_STRING_GOOGLE_APP_ID) return getGoogleAppid();
        return wrapped.getString(id);
    }

    // Provide your key programmatically
    public String getGoogleAppid() {
        return FirebaseApp.getInstance().getOptions().getApplicationId();
    }
    ...
}
Prilaga
  • 818
  • 10
  • 18
-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().

AniV
  • 3,997
  • 1
  • 12
  • 17
  • 2
    Do we really need that resource file? I thought initialization can be done at runtime without that resource. If I will use that resource file, why firebase option is needed? The main idea is to change firebase analytics at runtime. – rMozes Aug 09 '17 at 06:26