Reputation: 1667
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 :
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());
}
Added required manifest Internet permissions.
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?
Upvotes: 10
Views: 2173
Reputation: 1667
According to the point explained here : Unable to make Firebase work for a non Gradle build: Missing google_app_id. Firebase Analytics disabled (Different Question)
Google Analytics for Firebase (GA4F) doesn't support dynamic initialization. Our engineers are checking the possible solutions to support this. It's just that we still haven't found a definite timeline as to when (or if) this will be available.
GA4F will not work without the google-services.json file (or Gradle on your end). Even though you can initialize the FirebaseApp dynamically through code, GA4F will not recognize this and will only result in the error message you are seeing. The scenario you are getting is only specific to Google Analytics for Firebase. However, you can still use other products like Firestore, Realtime Database, Storage even if you are not using Gradle plugin.
Finally, Done it in below way :
What I needed to do :
From One Google account, Notification should work.
Have to use separate(another) Google account for Google Analytics. (Read the question. You will get the Idea.)
What I have done :
FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("1:4578412596352:b47d85se5v2z1a2b").setApiKey("AI4f896IG11gFh5R452FdiYau27sdA7IQ85EdY1").setProjectId("mycuteapp-57490").setGcmSenderId("547841025698").build(); FirebaseApp.initializeApp(getApplicationContext(), options,"pushNotifications");
In Short, Currently guidance from this Official link is not working in case of Google Analytics: https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html
Upvotes: 0
Reputation: 3494
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
Upvotes: 3