I want to implement Friebase notifications system inside a library that I want to use as SDK in many apps.
Firebase is asking now for an App ID, but I'm implementing it inside a library thus no App Id.
How could I achieve my goal to be able to send notifications to my apps that use my library ?
Thanks in advance.
Yes you can actually do this, on your library
build.gradle
put this inside the defaultConfig fieldThen inside your project's
gradle.properties
firebaseAppKey = <yourFirebaseAppSecret>;
For each project/app you must define this variable on your
gradle.properties
.You'll have to create a firebase app for each project, but your library can now have the Firebase SDK.
When you want to access this environment variable value use
BuildConfig.FIREBASE_APP_KEY
(e.g. instantiate firebase).One option is to have the user of your library create a Firebase project and then pass in the resulting google-services.json file into their application then your library can depend on that.
As far as I know, you can't.
Each Firebase project needs package ids in order to uniquely identify every one of its apps.
You also need to configure every module with its own
google-services.json
, which is generated specifically for each id.In case you could use the same package for all your apps, a device or the very Play Store would not be able to differentiate one from another so not an option.
You could extract all Firebase logic into your library but still you would need to configure every app in order to provide a unique package id, and you would need to send a notification to every id as well.
I know this is an old question with an accepted answer but all the answers have a big disadventage - they require the user of your library to do work besides adding your library to their application. There is a way to do it without troubling the user of your library at all if your library is being downloaded from a Maven repository.
Note: this method is a hack and is not supported by Firebase. When asked Firebase Support, I got the following reply:
Nevertheless, I've found a way to do it and baybe someone will find it usefull so here it is:
This is an example for using Realtime Database but it should work for all the Firebase SDKs.
In your project's main
build.gradle
add mavenCentral repository:In your library project's
build.gradle
, add Google Play Services (as a dependency, not as a plugin):Add the relevant Firebase SDKs (with the same version as Google Play Services):
Register your SDK as a project on Firebas, download it's
google-services.json
and open it with any text editor.In your library's
strings.xml
add the following lines and fill these lines with data fromgoogle-services.json
This is it. You can use Firebase Realtime Database in your libaray, then build it and publish it to Maven (publishing to Maven is essential, otherwise the user of your library will have to add the dependencies manually). When activated from inside an application, your database will be used.
Note that this method may cause exceptions and unexpected behavior if the user of your library will use Google Play Services or Firebase so use at your own risk!
These are all kinda hacky or too much work, here’s a nice n simple example (ironic though, cause it’s a long post -- but worth it).
It is possible to use
FireBase
code in your library project, of course the consuming application will need to register the app and get the app ID /google-services.json
file.But your library doesn’t, and shouldn’t care about about that, it’s the consuming applications job to do that, not your library.
Here’s a brief example using the
firebase-messaging
module inside of a library project.YourLibrary module’s build.gradle:
This is all you need to do in your library project. DON’T apply the gms plug in here, and don’t add the google-services classpath to the libraries
build.gradle
.Now here’s how you set up your consuming app:
MyClientApp’s top-level build.gradle:
Now we need to set up the consuming applications module
build.gradle
, it’s simple. We pretty much just need to apply the plug-in, and depend on the library module that we create that has all theFireBase
code in it.MyClientApp’s module level build.gradle:
Some things to note:
build.gradle
).FireBase
code in it, but exclude it’s version of theFireBase
module, in favor of your own dependency version.FireBase
version.classpath 'com.google.gms:google-services:3.1.1’
only goes in the client app’s top levelbuild.gradle
.google-services.json
in your client app’s project.Firebase
Service
s in your app’s manifest (or use manifest merger and merge them in from your library project)google_play_services_version
meta-data tag to your client app’s Manifest.compileOnly
when declaring theFireBase
dependency.Now you’ll be able to use
FireBase
code in your app that you defined in your library that usesFireBase
. Or you could let your library module do all theFireBase
work!Of course this is typically used for internal libraries, as frameworks like
Firebase
weren’t designed to be implemented in library modules, but sometimes you need to, so this is a simple non-hacky/sane solution to the issue. It can be used on projects that are distributed through maven -- my library uses this, and it’s never caused any issues.Update:
You should use
compileOnly
when declaring the library module'sFirebase
dependency. By doing so the dependency will not be added to the build output. But you will be allowed to use the dependency in your library. If the consuming app wants to use firebase they’ll need to depend on it manually (usingimplementation
). This will help cut down on unneeded dependencies/bloat in applications and the “right” way to declare a dependency like this. Note: You may need to perform runtime checks to make sure the library is available before using it’s code in your module.