Simple example in integrating Flagsmith into an Android Application

Add the SDK

implementation("com.github.Flagsmith:flagsmith-kotlin-android-client:1.5.0")
Code language: CSS (css)

Declare the SDK client and (optionally) the feature you want to use

private lateinit var flagsmith : Flagsmith
private lateinit var colorLabel: TextView
Code language: PHP (php)

Initiate the SDK client

// Initialize the Flagsmith client with the specified environment key
// The environment key is used to identify your Flagsmith environment
flagsmith = Flagsmith(environmentKey = "--your-environment-key--",

  // Set the default flags
  // These flags will be used if the Flagsmith service is unavailable
  // Skip this if you're not using default flags. I highly recommend to use it to avoid problems if Flagsmith service is unavailable
  defaultFlags = defaultFlags,

  // Set the base URL for the Flagsmith API
  // This is the URL of your Flagsmith instance
  // Skip this if you're not using on premise Flagsmith installation
  baseUrl = "--your-base-url--/api/v1/",

  // Configure the cache settings for the Flagsmith client
  // The cache is used to store the state of your feature flags to reduce the number of requests to the Flagsmith service
  cacheConfig = FlagsmithCacheConfig(
    // Enable the cache
    enableCache = true,
    // Set the time-to-live (TTL) for the cache in seconds
    // The TTL is the maximum amount of time that an item is allowed to stay in the cache before it is automatically removed
    cacheTTLSeconds = 3600L,
    // Set the maximum size of the cache in bytes
    cacheSize = 1024L * 1024L),

  // Set the context for the Flagsmith client
  // The context is used to provide additional information about the environment in which the Flagsmith client is running
  context = context,

  // Set the base URL for the Flagsmith event source
  // The event source is used to receive real-time updates about changes to your feature flags

  // Skip this if you're not using on premise Flagsmith installation
  eventSourceBaseUrl = "--your-event-source-url--")

Code language: Kotlin (kotlin)

Retrieve the feature (in this example I’m retrieving text color)

// Define a private function named getColor that takes a userId as a parameter
private fun getColor(userId: String) {

  // Call the getValueForFeature method on the flagsmith object
  // This method retrieves the value of a feature flag for a specific user
  flagsmith.getValueForFeature(identity = userId, featureId = "text-color"){ result ->

  // Get the value of the feature flag from the result
  // If the feature flag is not set, use a default value of "#F50057"
  val color = result.getOrDefault(defaultValue = "#F50057").toString()

  // Try to set the text color of the colorLabel UI element to the value of the feature flag
  try {
            colorLabel.setTextColor(
                // Parse the color string into an Android Color int
                Color.parseColor(color))
  } catch (e: Exception) {
            // If an exception occurs (for example, if the color string is not a valid color), log an error message
            Log.e("Color", result.getOrDefault(defaultValue = "#F50057").toString())
  }
  // Set the text of the kolor UI element to the value of the feature flag
        colorLabel.text = color
  }
}

Code language: Kotlin (kotlin)