Use Firebase without the google-services.json file in Android
If you use Firebase on Android you’ve likely added this line to your code:
apply plugin: 'com.google.gms.google-services'
The google-services
plugin is meant to be a convenience, and it has two main functions:
- Turn your
google-services.json
file into astrings.xml
file with resources that Google/Firebase services need to initialize. - Make sure you have the proper dependencies to use Firebase.
There’s no Google magic here, just some helpful scripts. If the plugin is inconvenient to you, don’t use it!
Step 1: Making your own string resources
There is an extensive guide to all of the string resources that the plugin produces and how it makes them, and if you want to read that you can find it here.
I am going to demonstrate a really easy trick to get rid of the plugin after one use:
- Add the
google-services.json
file and the plugin to your project as described in the Add Firebase to your Android Project guide. - Run
./gradlew :app:assembleDebug
to force the plugin to do its job. - Open the generated file
app/build/generated/res/google-services/debug/values/values.xml
and inspect the contents, it should look something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources> <! -- Present in all applications -->
<string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string> <! -- Present in applications with the appropriate services configured -->
<string name="gcm_defaultSenderId" translatable="false">1035469437089</string>
<string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string>
<string name="ga_trackingId" translatable="false">UA-65557217-3</string>
<string name="firebase_database_url" translatable="false">https://example-url.firebaseio.com</string>
<string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
<string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
<string name="project_id" translatable="false">mydemoapp</string></resources>
4. Copy each of the <string>
resources from that file into your app’s own strings.xml
file (or any XML resources file you want).
5. Remove the google-services
plugin from your app and delete the google-services.json
file, you don’t need them anymore!
Step 2: Managing your own dependencies
Dependencies on Android are … complicated to say the least. I won’t attempt to solve every possible problem I will just lay out the two basic cases.
Case 1: You use Firebase/Google dependencies with versions lower than 15.0.0
The answer here is simple: put all of your dependencies at the same exact version. Anything with com.google.firebase
or com.google.android.gms
should be specified at the same version and things will work.
Case 2: You use Firebase/Google dependencies with versions 15.0.0 or higher
Starting with version 15.0.0
, Firebase and Google Play services announced a new versioning scheme. The main difference is that libraries will now be versioned semantically, rather than all having the same version.
For the most part this means you should treat each Firebase and Google SDK like a separate dependency, run your tests after you update them just like you would after updating any other third-party dependency in your app.
If you want to know the latest version for the SDKs you’re using, check out the Firebase release notes for Android. When there are known version conflicts between Firebase libraries, they will be noted there.
Step 3: Freedom!
You’re done! You should now be able to use Firebase and Google APIs without ever needing to touch the google-services
plugin.
It’s important to note that the plugin is still the fastest way to get started with these APIs and should work really well with simple projects. However as your project grows and becomes more complex the plugin may slow you down more than it speeds you up, in which case I hope this guide is helpful.