Init firebase with FirebaseOptions
Asked Answered
B

2

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.

Bologna answered 8/8, 2017 at 11:0 Comment(6)
Possible duplicate of Use multiple firebase accounts in single android app for google analyticsMireielle
Have you tried with the overload of initializeApp? ´FirebaseApp.initializeApp(getApplicationContext(), options, "someappname");´Windham
Hello @rMozes, Your issue solved or not ?Interfaith
You are using FirebaseOptions and Link you given have FirebaseOptions.Builder..!!!Interfaith
Hey doubt "setApplicationId()" where do i get this id? is it "1:576500000006:android:124ac0dfab06ff730858f3" ?Masto
@Bologna Were you able to solve this issue ? If yes, Please share the solution.Liking
G
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();
    }
    ...
}
Glycerol answered 7/2, 2023 at 15:46 Comment(0)
U
-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().

Unsteel answered 8/8, 2017 at 18:44 Comment(1)
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.Bologna

© 2022 - 2025 — McMap. All rights reserved.