0
0
Android Kotlinmobile~7 mins

Remote Config in Android Kotlin

Choose your learning style9 modes available
Introduction

Remote Config lets your app change its behavior and appearance without updating the app. You can update settings or features remotely.

You want to change app colors or text without releasing a new version.
You want to enable or disable features for some users remotely.
You want to run A/B tests by changing values for different user groups.
You want to fix small bugs or tweak settings quickly without app store approval.
You want to personalize app content based on user location or preferences.
Syntax
Android Kotlin
val remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
    minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)
remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)

remoteConfig.fetchAndActivate().addOnCompleteListener { task ->
    if (task.isSuccessful) {
        val welcomeMessage = remoteConfig.getString("welcome_message")
    }
}

Use fetchAndActivate() to get and apply remote values.

Set default values in XML to have fallback if fetch fails.

Examples
Simple fetch and activate remote config values.
Android Kotlin
val remoteConfig = Firebase.remoteConfig
remoteConfig.fetchAndActivate()
Set default values programmatically instead of XML.
Android Kotlin
remoteConfig.setDefaultsAsync(mapOf("welcome_message" to "Hello!"))
Get a string value from remote config.
Android Kotlin
val message = remoteConfig.getString("welcome_message")
Sample App

This app shows a message from Remote Config on screen. It sets a default message, fetches remote values, and updates the text.

Android Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val textView = TextView(this)
        setContentView(textView)

        val remoteConfig = Firebase.remoteConfig
        val configSettings = remoteConfigSettings {
            minimumFetchIntervalInSeconds = 3600
        }
        remoteConfig.setConfigSettingsAsync(configSettings)
        remoteConfig.setDefaultsAsync(mapOf("welcome_message" to "Welcome!"))

        remoteConfig.fetchAndActivate().addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val message = remoteConfig.getString("welcome_message")
                textView.text = message
            } else {
                textView.text = "Failed to load message"
            }
        }
    }
}
OutputSuccess
Important Notes

Remote Config values are cached locally for the minimum fetch interval.

Always set default values to avoid empty or null results.

Test remote config changes carefully before releasing to all users.

Summary

Remote Config lets you change app behavior without app updates.

Use fetchAndActivate() to get and apply values.

Always provide default values for safety.