Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Google Analytics with custom FirebaseOptions initilaize method

I have to add simple Firebase Google Analytics for my android application.

I need to select my app's Firebase project at runtime.

"It's very rare, but sometimes an app must be able to select its Firebase project at runtime. This means your app can't use the automatic init provided by the FirebaseInitProvider that's merged into your app. In that case, the solution has two tasks."

Link for reference : https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html

So, In my case I have done as below :

  1. Created an Application class :

    public class MyApplicationClass extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            FirebaseOptions.Builder builder = new FirebaseOptions.Builder().setApplicationId("1:5478125639014:android:054aa87g102b94aa5").setApiKey("ABcdXyzpQrst_8tlItnC5rcbgxkK_PqrstGWsTKo");
            FirebaseApp.initializeApp(this, builder.build());
        }
    
  2. Added required manifest Internet permissions.

  3. Added dependencies :

    implementation 'com.google.firebase:firebase-analytics:17.1.0'

Issue is I can't see any analytics data there and Application is not getting connected in Google Analytic's final step.

What might be issue or something pending there?

like image 377
Jaimin Modi Avatar asked Aug 29 '19 05:08

Jaimin Modi


1 Answers

I know it is not the answer you were expecting but the sad reality is that you can't dynamically initialize your Firebase Analytics.

In official Firebase repository you can see issue https://github.com/firebase/quickstart-android/issues/769 and there the official answer is "this is currently working as intended, the Analytics SDK does not allow configuration of the App ID at runtime". I don't believe it changed since then.

I know that these posts may sound confusing in that context: https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html https://firebase.google.com/docs/projects/multiprojects

But intended use case is having different FirebaseApp configuration (for different databases, for example) and not different FirebaseAnalytics configuration. And Firebase Analytics is just one of many products under Firebase.

So you don't need to use gradle plugin, but you still need to have String resource called google_app_id that contains your Firebase App Id and only that will be used to configure FirebaseAnalytics.

It doesn't mean that dynamic initialization of FirebaseAnalytics can't be done at all, but it would require reverse engineering, hacky solution and it would be fragile - in short, probably not a good idea.

I'm not sure what is your use case but if you only want to delay Firebase Analytics initilization, you could consider disabling it at the beginning: https://firebase.google.com/docs/analytics/configure-data-collection?platform=android

like image 142
michalbrz Avatar answered Nov 04 '22 21:11

michalbrz