firebase-android-sdk icon indicating copy to clipboard operation
firebase-android-sdk copied to clipboard

Initialize FirebaseApp without google-services.json

Open paularius opened this issue 6 years ago • 55 comments

  • Android Studio version: 3.2
  • Firebase Component: Core/Analytics(Database, Firestore, Storage, Functions, etc)
  • Component version: 16.0.4

[REQUIRED] Step 3: Describe the problem

I am trying to initialize the FirebaseApp, only by using the FirebaseOptions builder, like this, first thing in my application (I use only applicationId and ApiKey, since I only want the analytics for start) :

FirebaseOptions options = new FirebaseOptions.Builder()
                    .setApplicationId(applicationID) // Required for Analytics.
                    .setApiKey(apiKey) // Required for Auth.
                    .build();
FirebaseApp.initializeApp(context, options);

I do not have a google-services.json file in my project, I have removed the FirebaseInitProvider, like this, in my AndroidManifest.xml:

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

When starting the application, while trying to View events in the Android Studio debug log, like this: adb shell setprop log.tag.FA VERBOSE adb shell setprop log.tag.FA-SVC VERBOSE adb logcat -v time -s FA FA-SVC

I get Missing google_app_id. Firebase Analytics disabled. See https://goo.gl/NAOOOI .

paularius avatar Oct 09 '18 15:10 paularius

@paularius could you try adding a string entry to your project (in strings.xml)

<!-- Use the same value you used for applicationID above -->
<string name="google_app_id">YOUR_APPLICATION_ID</string>

For a more comprehensive guide on not using the json file, see this blog post: https://medium.com/@samstern_58566/how-to-use-firebase-on-android-without-the-google-services-plugin-93ecc7dc6c4

samtstern avatar Oct 09 '18 16:10 samtstern

@samtstern Thanks for your reply. I have already tried this, and seems to work. The thing is that I would like to initialize the FirebaseApp more dynamically, since I would like to have different projects within the same apk (so using different flavors for the xml, would not be useful). I am trying a part of your approach and try to change the resource dynamically, using reflection, before initializeApp, but that doesn't seem to work either. Moreover, what is the use of the FirebaseOptions, if I have to use a file in my project?

paularius avatar Oct 10 '18 09:10 paularius

@paularius you're right that you shouldn't need the XML file at all. Where are you calling InitializeApp() and is it possible you're trying to use FirebaseAnalytics before that happens?

samtstern avatar Oct 10 '18 15:10 samtstern

@samtstern I call it first thing in onCreate, in my application class. Also, before I try to use any firebaseAnalytics call, I check that the FirebaseApp (the "[DEFAULT]" one) exists. But even if I tried to initialize it in some other place (for example when I get a response from my server), even by losing some events, should it be a problem?

paularius avatar Oct 11 '18 16:10 paularius

I have had the same issue since months now. I have a running thread with the support team, and it doesn't look like this issue is going anywhere. In my scenario, I have the manual Firebase initialization in a library, and I have multiple apps that use that library. So based on the application thats using it, I initialize the Firebase app. Everything initializes well, but Firebase analytics, which complains about "Missing google_app_id".

adityashri7 avatar Oct 11 '18 18:10 adityashri7

We're having the same issue, really hope Firebase team moves faster on this as it's impacting many developers and we're considering moving off of Firebase due to it.

kjsingh815 avatar Oct 11 '18 18:10 kjsingh815

Thanks everyone for chiming in! I filed an internal bug (b/117609738) with the analytics team since they're likely not watching this issue as that SDK is still closed source. Hopefully they can help me get to the bottom of this.

If you'll let me take one more blind stab: what happens if you create a bogus google_app_id string resource just to satisfy the SDK, but then initialize the default FirebaseApp yourself at runtime. Does Analytics send events to the right place?

samtstern avatar Oct 11 '18 18:10 samtstern

@samtstern if I just add a google app id, Firebase initializes the app on its own, but ofc doesn't add the other parameters like database url etc. So the manual initialization also fails, because it sees that the app has already been initialized. I even tried to add all the items as generated by firebase to my resources, as mentioned in this post: https://medium.com/@samstern_58566/how-to-use-firebase-on-android-without-the-google-services-plugin-93ecc7dc6c4. It works, but then like @paularius mentioned, I wouldn't be able to switch the app environments dynamically during run time.

adityashri7 avatar Oct 11 '18 19:10 adityashri7

Please, I have same problem with initialization app, is this problem solved yet, or exist any workaround?

dekan1 avatar Nov 26 '18 17:11 dekan1

There's no PR yet for that? How is going on the work to solve that?

I'm with the same problem, I want to load dynamically apps on runtime.

renatobenks-zz avatar Jan 02 '19 14:01 renatobenks-zz

Is this issue fixed...I mean can we initialize firebase without this json file.. Also what is the purpose of firebaseoptions.builder if we can intialize firebase without the builder by simply copying the json file in project.

tonyyyrocks39 avatar Feb 06 '19 11:02 tonyyyrocks39

Yes, we have similar requirement where we would like to be able to switch on startup which Firebase project along with Analytics and Crashlytics should be used. Any feedback from Analytics teams on this?

damianun avatar Feb 27 '19 08:02 damianun

Hi, we also have the same issue, it's stopping us from full migration from GA to Firebase because we need to setup Firebase in runtime with use of data received from our backend. Is there any ongoing work on this bug?

mkarecki avatar Feb 27 '19 12:02 mkarecki

Having the same issue. Will be there any fix soon?

mmert1988 avatar Mar 01 '19 14:03 mmert1988

@samtstern Any news here?

igorbiscanin avatar May 07 '19 09:05 igorbiscanin

@samtstern Any update on this issue?

ddukesterman avatar Jun 28 '19 17:06 ddukesterman

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.

https://stackoverflow.com/a/55006495/5862482

vokrut avatar Aug 07 '19 13:08 vokrut

Just checking up on this, status of this has not changed, right?

pepitoria avatar Jan 10 '20 14:01 pepitoria

@pepitoria AFAIK the last time we spoke with google - this issue is not going to be resolved.

HarrisonRenny avatar Jan 10 '20 16:01 HarrisonRenny

bug with Firebase using in Chromium

RustamSitdikov avatar Mar 03 '20 17:03 RustamSitdikov

in Chromium problem was due to string resources obfuscation (so google_app_id was obfuscated). adding resource_type/resource_name#no_obfuscate in aapt2.config solved problem

RustamSitdikov avatar Mar 13 '20 09:03 RustamSitdikov

Copied from https://github.com/firebase/firebase-android-sdk/issues/187#issuecomment-635485459

Thanks for the question and for waiting patiently.

Many Firebase SDKs products provide an overload that allows you to pass in a custom FirebaseApp getInstance(FirebaseApp).

This API will allow you inject these Firebase instances as dependencies into your application code so they continue remaining agnostic to prod,staging,dev versions of your app.

I suspect your question pertains to eagerly initialized SDKs like Analytics, Crashlytics and Firebase Performance that require no developer interaction and start working automagically once included in the app. Note that these SDKs do not support the desired getInstance(FirebaseApp) overload.

The challenge with designing an API like getInstance(FirebaseApp) for these products is that it would have to be wired in super early in the App's lifecycle, to (say) allow Crashlytics to capture crashes that happen early on, or to (say) allow Performance to capture app start metrics early on.

In the absence of this API, you may be able to do this by disabling the default init provider and creating your own equivalent. The key here is to perform the custom initialization super early in your app's lifecycle, like in a Content Provider.

FirebaseOptions.Builder builder = new FirebaseOptions.Builder()
    .setApplicationId("1:0123456789012:android:0123456789abcdef")
    .setApiKey("your_api_key")
    .setDatabaseUrl("https://your-app.firebaseio.com")
    .setStorageBucket("your-app.appspot.com");
FirebaseApp.initializeApp(this, builder.build());

I realize is a suboptimal experience. It does get the job done for now. Feel free to propose ideas you may have to help up build an API to accommodate the nuance I described above.

ashwinraghav avatar May 28 '20 17:05 ashwinraghav

Sadely @ashwinraghav this doesn't seem to solve the issue. We have struggled with this for almost two years. We have a multi-tenant application with dynamic switching of the firebase project and we got that working but analytics has to all be sent to one place by hardcoding "google_app_id" in values/strings.xml - this is the only thing that somewhat works. I just tried a Content Provider and the code runs fine but still see an error "Missing google_app_id." We continue to wait hopefully for one day a getInstance(FirebaseApp) for FirebaseAnalytics. Note this all works for the iOS SDKs.

jruvel avatar Jun 05 '20 21:06 jruvel

Sadely @ashwinraghav this doesn't seem to solve the issue. We have struggled with this for almost two years. We have a multi-tenant application with dynamic switching of the firebase project and we got that working but analytics has to all be sent to one place by hardcoding "google_app_id" in values/strings.xml - this is the only thing that somewhat works. I just tried a Content Provider and the code runs fine but still see an error "Missing google_app_id." We continue to wait hopefully for one day a getInstance(FirebaseApp) for FirebaseAnalytics. Note this all works for the iOS SDKs.

Duly noted. This feedback is helpful for us to prioritize.

ashwinraghav avatar Jun 06 '20 02:06 ashwinraghav

Is it possible to set up Firebase Cloud messaging from the instance of the second app ?

smilehari avatar Jun 29 '20 10:06 smilehari

We are also affected from this issue since we are using a content provider instead of google-services.json. We are trying to migrate to firebase crashlytics with mappingFileUploadEnabled set to true. The build fails with the following error:

Crashlytics could not find the resource file generated by Google Services. You may need to execute the :process<Variant>GoogleServices Task. Please check your Firebase project configuration (https://firebase.google.com/docs/android/setup). 

The issue is two years old and it is the highest voted firebase sdk github issue, shouldn't it have a top priority?

iliaskomp avatar Jun 29 '20 11:06 iliaskomp

We are also affected from this issue since we are using a content provider instead of google-services.json. We are trying to migrate to firebase crashlytics with mappingFileUploadEnabled set to true. The build fails with the following error:

Crashlytics could not find the resource file generated by Google Services. You may need to execute the :process<Variant>GoogleServices Task. Please check your Firebase project configuration (https://firebase.google.com/docs/android/setup). 

The issue is two years old and it is the highest voted firebase sdk github issue, shouldn't it have a top priority?

we're facing exactly the same problem with mappingFileUploadEnabled I totally agree, this should have more priority.

hollmannlu avatar Jun 29 '20 11:06 hollmannlu

Is it possible to set up Firebase Cloud messaging from the instance of the second app ?

We do use messaging successfully from the new app that we switch to.

jruvel avatar Jun 29 '20 15:06 jruvel

The issue is two years old and it is the highest voted firebase sdk github issue, shouldn't it have a top priority?

Certainly is top priority :) We'll be sure to share our plans to address this issue with folks on this issue, to get some early feedback.

ashwinraghav avatar Jun 30 '20 17:06 ashwinraghav

@ashwinraghav - does this mean a fix on the Android side is close? https://github.com/firebase/firebase-ios-sdk/pull/6142 - Not exactly related but going the right direction.

jruvel avatar Aug 26 '20 20:08 jruvel