Using Firebase Remote Config in Android

Firebase Remote Config is a cloud service provided by Firebase.

In previous article Getting Started With Firebase Android we have seen what are the features Firebase is providing to improve app performance and usability.

Firebase Remote config is the best feature from developer point of view, it allows you to perform some dynamic operation without updating the build. End user will get the latest changes without updating application to new version.

You can use Firebase Remote config to provide promotional offer banners to user without updating new build.

Firebase Remote Config allows you to set some layout behavior based on users language, locality, app version, gender etc.

Setup Firebase Remote Config 

Step 1 : Create a project on Firebase Console .

Remote Config

Step 2 : After successful creation of your first project Click on Add Firebase to your Android app.

Remote Config

Step 3 : Add all the details of your project like Package name, App nickname, SHA – 1 key etc and download google.json file. Put that file in app/ folder

Remote Config

Step 4 : Add rules to your root-level build.gradle file

Step 5 : Add dependency to your module level build.gradle file and add apply plugin at bottom of file

After adding google.json file and all setup you might get this error

Error:Execution failed for task ‘:app:processDebugGoogleServices’.
> Missing api_key/current_key object

It is a bug in google-services:3.0.0, but there is no need to worry about, once you add any service of the Firebase, replace the google.json file with newly created file. You can get new google.json file from Project Settings in Firebase console.

Step 6 : From Firebase Console setup your Remote Config by clicking on Remote Config from left menu.

Remote Config

Step 7 : Click on Add Your First Parameter will prompt a new dialog asking for parameter key and default value. After adding the parameter don’t forget to click publish changes.

Remote Config

Step 8 : Setup default value of config parameter in your android project in form of Map or XML resource File. Store your xml file inside res/xml. Here we have created default_config.xml file

Note : Make sure that parameter name/key name must be the same as we have created in Firebase Console.

Step 9 : Get an instance of FirebaseRemoteConfig and set default values using setDefaults()

Step 10 : Fetch the value of background_color key by using firebaseRemoteConfig object

Now you can build and run the application and you will see black background as it is default value we have provided in default_config.xml file

Now we will fetch the value from our server/console which we had already set to white(#FFFFFF). To fetch the value we need to use fetch() which will get latest value of all the parameters available.

As you can see in above code after fetching values successfully we have used firebaseRemoteConfig.activateFetched(); which will activate all the fetched values and new values will be reflected to our existing configuration.

Not only just background color, you can also use this feature to show some promotional banner, just fetch Boolean value from server and based on that show some dialog which describes offer you are providing.

Remote Config Example

Getting Started With Firebase Android
Firebase Authentication In Android