Getting Started with Firebase RemoteConfig in Android

Getting Started with Firebase RemoteConfig in Android

In this blog, we will learn about the firebase Remote Config feature in Android. By using Firebase Remote Config, we can bring some updates in the App like some text change, color change in the Android App, without publishing any update of the app on the Play Store.

Let's get started.

Adding Firebase Remote Config Dependency

To use the Firebase Remote Config in Android Application, first of all, we need to add the following dependencies in the app build.gradle.

implementation 'com.google.firebase:firebase-config-ktx:{latest-version}'
implementation 'com.google.firebase:firebase-analytics-ktx:{latest-version}'

Note: Do not forget to add google-services.json in your project.

  • Download the "google-service.json" file from Firebase Console.
  • In the project, navigate to the "app" folder and paste the .json file.

Using Remote Config in Android Project

Then, we can create a class RemoteConfigUtils like below:

object RemoteConfigUtils {

    private const val TAG = "RemoteConfigUtils"

    private const val NEXT_BUTTON_TEXT = "next_button_text"
    private const val NEXT_BUTTON_COLOR = "next_button_color"

    private val DEFAULTS: HashMap<String, Any> =
        hashMapOf(
            NEXT_BUTTON_TEXT to "NEXT",
            NEXT_BUTTON_COLOR to "#0091FF"
        )

    private lateinit var remoteConfig: FirebaseRemoteConfig

    fun init() {
        remoteConfig = getFirebaseRemoteConfig()
    }

    private fun getFirebaseRemoteConfig(): FirebaseRemoteConfig {

        val remoteConfig = Firebase.remoteConfig

        val configSettings = remoteConfigSettings {
            if (BuildConfig.DEBUG) {
                minimumFetchIntervalInSeconds = 0 // Kept 0 for quick debug
            } else {
                minimumFetchIntervalInSeconds = 60 * 60 // Change this based on your requirement
            }
        }

        remoteConfig.setConfigSettingsAsync(configSettings)
        remoteConfig.setDefaultsAsync(DEFAULTS)

        remoteConfig.fetchAndActivate().addOnCompleteListener {
            Logger.d(TAG, "addOnCompleteListener")
        }

        return remoteConfig
    }

    fun getNextButtonText(): String = remoteConfig.getString(NEXT_BUTTON_TEXT)

    fun getNextButtonColor(): String = remoteConfig.getString(NEXT_BUTTON_COLOR)

}

Now, let's understand what we have done in RemoteConfigUtils class:

  • DEFAULTS: HashMap to store key and the default value associated with that key.
  • init(): The function to initialize the remoteConfig. It uses the getFirebaseRemoteConfig function.
  • After that, we have created the functions like getNextButtonText(), getNextButtonColor() to access the text and the color wherever required in our Application. We can create similar more functions based on our requirements.

Then, we can call the init function from the Application class like below:

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        RemoteConfigUtils.init()
    }

}

Now, we can use the functions to get the text and color like below:

nextButton.text = RemoteConfigUtils.getNextButtonText()
nextButton.setBackgroundColor(Color.parseColor(RemoteConfigUtils.getNextButtonColor()))

But till now, we are using the default text and the background color.

Now that we have already published the Android App on the play store, and we want to change the text and the background color.

Updating Value from Firebase Remote Config Console

We just need to go to the firebase remote config console, put the key and the new value associated with that key like below in the firebase remote config console:

Getting Started with Firebase RemoteConfig in Android

That's it.

After this change, the next button text will be changed to PROCEED.

We have updated the text of the next button without updating the app on the play store.

Conclusion

So, in this blog, we learned about Firebase Remote Config, which is used to make small updates in your app without publishing the update on the play store. Remote Config is very useful when we have some minor updates in our app. Hope you enjoyed this blog. So, try implementing the Remote Config to your app. You can take reference from the official website of Remote Config.

See you in the next blog, till then, Keep Learning :)

Team MindOrks :)