OpenFeature Provider for Android SDK
Overview
The Android OpenFeature ProviderProviders act as a bridge between the Evaluation API and the underlying flag management system. They translate API calls into the format required by the provider, which could be a vendor SDK, a custom REST API, or a local data source. allows your Android applications to integrate with Harness FME using a standardized, vendor-agnostic feature flagging API. This provider implements the OpenFeature specification and bridges the OpenFeature SDK with the Harness FME Android SDK.
This page walks you through installing, configuring, and using the Android OpenFeature provider to evaluate feature flagsA feature flag is a conditional toggle in Harness FME that enables or disables specific functionality without deploying new code. It allows for controlled feature rollouts, A/B testing, and quick rollbacks if issues arise. in your Android applications.
Prerequisites
Before you begin, ensure you have the following:
- A valid Harness FME SDK key for your project
- An Android project targeting API Level 21 (Android 5.0 Lollipop) or later
- Access to your project's
build.gradleorbuild.gradle.ktsfile to add the provider dependency
Version compatibility
| Component | Minimum Version |
|---|---|
| Android | API Level 21 (Android 5.0 Lollipop) |
split-openfeature-provider-android | ≥ 1.0.0 |
| OpenFeature Kotlin SDK | ≥ 1.0.0 |
Install the provider and dependencies
Add the Harness FME OpenFeature provider dependency to your application's build.gradle.kts file.
dependencies {
implementation("io.split.openfeature:split-openfeature-android:1.0.0")
}
Or, if you're using the Groovy DSL (build.gradle):
dependencies {
implementation 'io.split.openfeature:split-openfeature-android:1.0.0'
}
Initialize the provider
The FME OpenFeature provider requires an Android Context and your Harness FME SDK Key.
import dev.openfeature.kotlin.sdk.OpenFeatureAPI
import dev.openfeature.kotlin.sdk.ImmutableContext
import io.split.openfeature.android.provider.SplitProvider
// Create provider configuration
val config = SplitProvider.Config(
applicationContext = applicationContext,
sdkKey = "YOUR_SDK_KEY"
)
// Create the FME provider
val provider = SplitProvider(config = config)
// Set the provider with an initial context containing a targeting key
val initialContext = ImmutableContext(targetingKey = "user-123")
OpenFeatureAPI.setProvider(provider, initialContext = initialContext)
// Get a client and evaluate flags
val client = OpenFeatureAPI.getClient()
val showNewFeature = client.getBooleanValue("new-feature", false)
Construct an evaluation context
Use an evaluation contextThe Evaluation Context holds contextual information used during flag evaluation. It can include static data (like application or host identifiers) and dynamic data (such as a client IP address), which can be passed explicitly or propagated automatically. Static and dynamic values can be merged for richer, more targeted evaluations. with a targeting keyA unique identifier used to target specific users or entities when evaluating feature flags. It helps determine which variation of a flag should be served based on predefined rules and conditions. to pass attributes used for flag targeting. You can include identifiers such as user IDs, email addresses, plan types, and more.
For example:
val context = ImmutableContext(
targetingKey = "user-123",
attributes = mapOf(
"email" to Value.String("user@example.com"),
"age" to Value.Integer(30),
"plan" to Value.String("premium")
)
)
val client = OpenFeatureAPI.getClient()
val result = client.getBooleanDetails("premium-feature", false, context)
To set a targeting key during initialization:
val initialContext = ImmutableContext(targetingKey = "user-123")
OpenFeatureAPI.setProvider(provider, initialContext = initialContext)
To change a targeting key at runtime:
val newContext = ImmutableContext(targetingKey = "user-456")
OpenFeatureAPI.setEvaluationContext(newContext)
Observe provider events
The FME OpenFeature provider emits eventsEvents allow your application to respond to changes in provider state or flag configuration, such as readiness changes, errors, or updates to flag values. when provider state changes (for example, when flags update, configuration changes, or errors) occur. You can observe these events to react dynamically in your application.
import dev.openfeature.kotlin.sdk.events.OpenFeatureProviderEvents
import kotlinx.coroutines.launch
// Observe provider events
lifecycleScope.launch {
provider.observe().collect { event ->
when (event) {
is OpenFeatureProviderEvents.ProviderReady -> {
// Provider is ready to evaluate flags
Log.d("Split", "Provider is ready")
}
is OpenFeatureProviderEvents.ProviderConfigurationChanged -> {
// Flag configuration has been updated
Log.d("Split", "Configuration changed")
}
is OpenFeatureProviderEvents.ProviderStale -> {
// Provider is serving cached data
Log.d("Split", "Provider is stale")
}
is OpenFeatureProviderEvents.ProviderError -> {
// An error occurred
Log.e("Split", "Provider error: ${event.error}")
}
}
}
}
Track events
The FME OpenFeature provider supports tracking events such as user actions or conversions. To enable event tracking, set a TrafficType in the evaluation context.
// Set context with trafficType
val context = ImmutableContext(targetingKey = "user-123")
.withTrafficType("user")
OpenFeatureAPI.setEvaluationContext(context)
// Track an event
val client = OpenFeatureAPI.getClient()
client.track("button_clicked")
// Track with a value
client.track(
"purchase_completed",
TrackingEventDetails(value = 99.99)
)
// Track with properties
client.track(
"page_viewed",
TrackingEventDetails(
structure = ImmutableStructure(
mapOf(
"page" to Value.String("home"),
"referrer" to Value.String("google")
)
)
)
)
For more information, see the Harness FME Android OpenFeature Provider GitHub repository.