Firebase Remote Config Android Sample App Walkthrough

This guide walks you through the steps involved in integrating Remote Config into your Android app. To learn more about the Remote Config API for Android, see com.google.firebase.remoteconfig.

Requirements

Set up your project using the instructions provided in Adding Firebase to your Android project.

Next, add the following dependency to your app module's build.gradle:

    compile 'com.google.firebase:firebase-config:9.6.1'

Create a Remote Config project for the quickstart sample

The quickstart sample provides an example of using Remote Config to apply a specified discount if a promotion is in effect. Before running the quickstart sample, you should set up a Remote Config project.

In the Firebase console, click Create New Project, and then follow the instructions to Set up a Firebase Remote Config Project.

with the following parameters:

Parameter Key Default Value Notes:
is_promotion_on true Set to false to disable promotional discounts
discount 20 Promotional discount, defined as the amount of dollars or another currency that is subtracted from the price.

After configuring your project, run the quickstart sample to see a fictional product discounted as part of a sales promotion. You can change the values of these parameters in the project and then re-run the quickstart sample to see how Remote Config lets you change an app's behavior.

Build and run the quickstart sample

  1. Download the quickstart sample code and unzip it.
  2. Open Android Studio.
  3. Select File > Open, browse to where you extracted the sample code, and then open the Config folder.
  4. Build and run the sample.

How it Works

First, the sample gets a Remote Config object instance and enables developer mode to allow for frequent refreshes of the cache:

mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
        .setDeveloperModeEnabled(BuildConfig.DEBUG)
        .build();
mFirebaseRemoteConfig.setConfigSettings(configSettings);

Then, the sample sets in-app default values from an XML file:

mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);

Now, the sample creates a fetch() request to fetch values from the Remote Config Server and calls activateFetched() to make those values available to the app:

// cacheExpirationSeconds is set to cacheExpiration here, indicating that any previously
// fetched and cached config would be considered expired because it would have been fetched
// more than cacheExpiration seconds ago. Thus the next fetch would go to the server unless
// throttling is in progress. The default expiration duration is 43200 (12 hours).
mFirebaseRemoteConfig.fetch(cacheExpiration)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(MainActivity.this, "Fetch Succeeded",
                            Toast.LENGTH_SHORT).show();

                    // Once the config is successfully fetched it must be activated before newly fetched
                    // values are returned.
                    mFirebaseRemoteConfig.activateFetched();
                } else {
                    Toast.makeText(MainActivity.this, "Fetch Failed",
                            Toast.LENGTH_SHORT).show();
                }
                displayPrice();
            }
        });

You can access your config values by calling one of the available get<type> methods (for example, getLong) on the FirebaseRemoteConfig object passed to the callback.

Now that the price has been updated, you can display the price in the app:

private void displayPrice() {
    long initialPrice = mFirebaseRemoteConfig.getLong(PRICE_CONFIG_KEY);
    long finalPrice = initialPrice;
    if (mFirebaseRemoteConfig.getBoolean(IS_PROMOTION_CONFIG_KEY)) {
        finalPrice = initialPrice - mFirebaseRemoteConfig.getLong(DISCOUNT_CONFIG_KEY);
    }
    mPriceTextView.setText(mFirebaseRemoteConfig.getString(PRICE_PREFIX_CONFIG_KEY) + finalPrice);
}

Caching

Remote Config caches values locally after the first successful fetch. By default the cache expires after 12 hours, but you can change the cache expiration for a specific fetch by passing the desired cache expiration to the fetch method. If the values in the cache are older than the desired cache expiration, Remote Config will request fresh config values from the server. If your app requests fresh values using fetch several times, requests are throttled and your app is provided with cached values.

During app development, you might want to refresh the cache very frequently (many times per hour) to let you rapidly iterate as you develop and test your app. To accommodate rapid iteration on a project with up to 10 developers, you can temporarily add a FirebaseRemoteConfigSettings object with isDeveloperModeEnabled set to true to your app, changing the caching settings of the FirebaseRemoteConfig object.

Send feedback about...

Need help? Visit our support page.